如何用PHP从2D数组或多数组创建1D数组?

时间:2022-06-16 21:35:08

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.

我不知道怎么做,我用谷歌搜索了两个小时,但是还没有找到任何解决办法。如何使用数组函数或任何编程逻辑来实现这一点,请帮忙。

3 个解决方案

#1


3  

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

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

#2


1  

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

我自己解决了。如果多数组具有动态值,它将会工作。输出美元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

输出美元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
)

#3


0  

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
)

#1


3  

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

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

#2


1  

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

我自己解决了。如果多数组具有动态值,它将会工作。输出美元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

输出美元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
)

#3


0  

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
)