简体   繁体   中英

How to create 1D array from 2D array or multi array in PHP?

I have an array which looks like this

Array
(
[0] => Array
    (
        [0] => RANO17
        [1] => RANO99
    )

[1] => Array
    (
        [0] => Test Product Ranosys
        [1] => Rano test example
    )

[2] => Array
    (
        [0] => 7
        [1] => 2
    )

[3] => Array
    (
        [0] => 
        [1] => 
    )

[4] => Array
    (
        [0] => 200.0000
        [1] => 100.0000
    )

[5] => Array
    (
        [0] => 
        [1] => 
    )

[6] => Array
    (
        [0] => 1400
        [1] => 200
    )

)

I want to make 1D array from above array. 1D array looks like.

Array(
[0] => Array
(
  [0] => RANO17
  [1] => Test Product Ranosys
  [2] => 7
  [3] => 
  [4] => 200.0000
  [5] => 
  [6] => 1400
)

[1] =>Array 
(
  [0] => RANO99
  [1] => Rano test example
  [2] => 2
  [3] => 
  [4] => 100.0000
  [5] => 
  [6] => 200
)
)

I don't have any idea how can we do this I was googling from last two hours but didn't get any solution yet. How can we do this either using some array functions or any programming logic please help.

$newArr1 = array();
$newArr2 = array();

foreach ( $arr AS $element ) {
  $newArr1[] = $element[ 0 ];
  $newArr2[] = $element[ 1 ];
}

I solved it myself. It will work if multiarray has dynamic values. output of $orders_rows

Array
(
  [0] => Array
  (
    [0] => RANO17
    [1] => RANO99
  )

 [1] => Array
 (
    [0] => Test Product Ranosys
    [1] => Rano test example
 )

 [2] => Array
 (
    [0] => 7
    [1] => 2
 )

 [3] => Array
 (
    [0] => 
    [1] => 
 )

 [4] => Array
 (
    [0] => 200.0000
    [1] => 100.0000
 )

 [5] => Array
 (
    [0] => 
    [1] => 
 )

 [6] => Array
 (
    [0] => 1400
    [1] => 200
 )

)

There's code to solve my problem

foreach ($orders_rows as $singlerows) {
        for ($k = 0; $k < count($singlerows[0]); $k++) {
            for ($i = 0; $i < count($singlerows); $i++) {
                $output_array[] = $singlerows[$i][$k];
            }
        }
    }
    $orders_row = array_chunk($output_array, 7);

output of $orders_row

Array
(
  [0] => RANO17
  [1] => Test Product Ranosys
  [2] => 7
  [3] => 
  [4] => 200.0000
  [5] => 
  [6] => 1400
)
Array
(
  [0] => RANO99
  [1] => Rano test example
  [2] => 2
  [3] => 
  [4] => 100.0000
  [5] => 
  [6] => 200
)

Cycle through the array and dump the element of each array into separate arrays.

$myArray = array (array(1, "testing 1"), array(2, "testing 2"), array(3, "testing 3"));

$arrayOne = array();
$arrayTwo = array();

For each element of the main array, echo the two elements of the sub array into separate arrays of their own.

foreach ($myArray as $a) {
    $arrayOne[] = $a[0];
    $arrayTwo[] = $a[1];
}

print the results

print_r($arrayOne);
echo "\n\n";
print_r($arrayTwo);

Results in:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)


Array
(
    [0] => testing 1
    [1] => testing 2
    [2] => testing 3
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM