0

I have this array:

[docs] => Array
(
    [indexone] => Array    ( [0] => P008062518   )
    [indextwo] => Array    ( [0] =>              )
    [indexthree] => Array  ( [0] => 0000141334   )
    [indexfour] => Array   ( [0] => P006871638   )
    [indexfive] => Array   ( [0] => 0000910067   )
    [indexsix] => Array    ( [0] =>              )
)

I need to end with this one, extracting all values from the given key:

[docValues] => Array
(
    [indexone] => Array    ( P008062518, 0000141334, P006871638, 0000910067    )
)

I try this loop but i end with the same array structure :

foreach($values as $key => $data)
 {
    if(array_key_exists('docs', $data) )
    {
        $filtered = array_filter($data['docs'], function($var) { return !empty($var);});
        $numDocs = array_values($filtered);
        $values[$key]['docValues'] = $numDocs;
    }
}

How can it be done ?

4
  • 2
    What are you doing here? Array ( P008062518<br> 0000141334<br> P006871638<br> 0000910067 )? did you mean : Array ( P008062518, 0000141334, P006871638, 0000910067 ) because <br> makes no sense.... that is html and you are talking about a php array Commented Apr 27, 2016 at 17:45
  • Please show some more of your array structure, so we see exactly in which dimension your shown array is. Commented Apr 27, 2016 at 17:45
  • @Webeng you are right i will need to add br tag after Commented Apr 27, 2016 at 17:55
  • I see you edited your I need to end with this one. Answer updated. Commented Apr 27, 2016 at 17:59

5 Answers 5

3

To get that exact array output:

$result['docValues'][key($values['docs'])] =
    array_filter(array_column($values['docs'], 0));
  • Get the first key to use it as your new key with key()
  • Get an array of all values in the 0 indexes with array_column()
  • Remove empty elements using array_filter()
Sign up to request clarification or add additional context in comments.

1 Comment

I don't know what you mean. This gives the exact output given the input from your question.
1

If your first array is called $docArray, then you can do the following:

$docValuesArray = array();//declaring the result array
$indexoneArray = array();//declaring the array you will add values
//to in the foreach loop

foreach ($docArray as $value)
{
  $indexoneArray[] = $value[0];//giving each of the values
  //found in $docArray to the $indexoneArray
}

$docValueArray[] = $indexoneArray;//adding the $indexoneArray
//to the $docsValueArray

Let me know if that worked for you.

Comments

1

This should do the trick for you:

$docs = [
    'indexone' => ['P008062518'],
    'indextwo' => [ ],
    'indexthree' => ['0000141334'],
    'indexfour' => ['P006871638'],
    'indexfive' => ['0000910067'],
    'indexsix' => [ ],
];

$allDocs = array();

foreach($docs as $key => $doc) {
    $docString = implode("<br>",$doc);
    if (empty($docString)) {
        continue;
    }
    $allDocs[] = $docString;
}
$allDocsString = implode("<br>",$allDocs);

echo($allDocsString);

P008062518
0000141334
P006871638
0000910067

Comments

1

Simply do this:

Your array

$arr = array("docs" => 
             array(
                 'indexone' => array('P008062518'),
                 'indextwo' => array(''),
                 'indexthree' => array('0000141334'),
                 'indexfour' => array('P006871638'),
                 'indexfive' => array('0000910067'),
                 'indexsix' => array('')
             )
        );

Process:

echo '<pre>';
$index = key($arr["docs"]);

$output['docValues'][$index] = implode('&lt;br/&gt;', array_filter(array_column($arr['docs'], 0)));
print_r($output);

Explanation:

  • key = key function Returns the first index.

  • implode = collapse all the array items with the delimiter of <br/>

  • array_filter = filters the values of an array using a callback function.

  • array_column = returns the values from a single column in the input array.

Result:

Array
(
    [docValues] => Array
        (
            [indexone] => P008062518<br/>0000141334<br/>P006871638<br/>0000910067
        )

)

1 Comment

Thank you @Fraine! all anwers help me but i use this one.
0

use array_filter() function . if you pass array in array_filter then remove all empty and NULL data record

1 Comment

I erase empty values with this line $filtered = array_filter(array_map('array_filter', $data['docs']));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.