-2

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.

ACDC
  • 119
  • 1
  • 7
  • 1
    You have a multidimensional array, meaning you need nested loops. In your attempted loop, `$isbnValue` is an array, not an individual ISBN. – El_Vanja May 11 '21 at 21:39
  • Does this answer your question? [How to Flatten a Multidimensional Array?](https://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array) – catcon May 11 '21 at 22:22

3 Answers3

0
$a = array();
foreach ($array as $tmp) {
 if (is_array($tmp))  $a = array_merge($a, $tmp);
}
Kinglish
  • 23,358
  • 3
  • 22
  • 43
0

Created this function that allows you to merge 1 or more arrays together into the format you asked for:

function sortarr(...$arr){
    $tmparr = [];
    foreach($arr as $aa) foreach($aa as $a) $tmparr = array_merge($tmparr, $a);
    return $tmparr;
}

Example Usage:

<?php 
    # First array provided
    $array = array(
        [
            "9780881220766",
            "0881220760"
        ], 
        [
            "9780141374284",
            "0141374284"
        ],
        [
            "057305133X",
            "9780573051333"
        ]
    );
    
    # Second array just to show how you can input more than 1 array into the function
    $array2 = array(
        [
            "6234808972234",
            "67834757"
        ], 
        [
            "0287568924344234",
            "234690543"
        ],
        [
            "34565786978099x",
            "3569876546"
        ]
    );

    function sortarr(...$arr){
        # Declare our temp array
        $tmparr = [];

        # Iterates through each of the arrays inside the array(s) provided
        foreach($arr as $aa) foreach($aa as $a) $tmparr = array_merge($tmparr, $a);

        # Return the results
        return $tmparr;
    }
    
    print_r( sortarr($array, $array2) );

Results:

Array
(
    [0] => 9780881220766
    [1] => 0881220760
    [2] => 9780141374284
    [3] => 0141374284
    [4] => 057305133X
    [5] => 9780573051333
    [6] => 6234808972234
    [7] => 67834757
    [8] => 0287568924344234
    [9] => 234690543
    [10] => 34565786978099X
    [11] => 3569876546
)

Live Demo: http://sandbox.onlinephpfunctions.com/code/838a108498f446ae4a5f8e42fa441ec11941c567

Crimin4L
  • 610
  • 2
  • 8
  • 23
0

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;
    }
ACDC
  • 119
  • 1
  • 7