I have the following PHP array
[1] => Array ( [0] => 9780881220766 [1] => 0881220760 ) [2] => Array ( [0] => 9780141374284 [1] => 0141374284 ) [3] => Array ( [0] => 057305133X [1] => 9780573051333 ))
I would like the output to be as follows:
[0] => 9780881220766 [1] => 0881220760 [2] => 9780141374284 [3] => 0141374284 [4] => 057305133X [5] => 9780573051333
I tried using array_values
but I’m not getting any output.
I also tried to extract the values using a foreach loop:
foreach ($rawIsbns as $isbnKey => $isbnValue){ $formattedIsbns[] = $isbnValue; }
However, I’m not managing.
Advertisement
Answer
I managed to solve the problem by initializing an array and adding values of subsequent nested arrays using array_push
function isbns($rawIsbns) { $prevArrayValues = null; foreach ($rawIsbns as $value) { if (!isset($prevArrayValues)) { $prevArrayValues = []; //initiallise first value unless initialized } if (empty($value)) { $return = []; //disregard empty values } else { $return = $value; // add arrays to existing array } $prevArrayValues = array_merge($prevArrayValues, $return); } return $prevArrayValues; }