如何将数组转换为索引数组

时间:2019-01-06 21:11:08

标签: php

我尝试将数组转换为索引数组,但是发现的所有数组函数都无法解决我的问题

我有这个数组

Array(
  [no_discount] => 0
  [manufacturers_id] => 2
  [id] => 3
)
Array(
  [no_discount] => 1
  [manufacturers_id] => 1
  [id] => 1
)

,我想将此数组转换为

Array(
  [0] => Array(
    [no_discount] => 0
    [manufacturers_id] => 2
    [id] => 3
  )
  [1] => Array(
    [no_discount] => 1
    [manufacturers_id] => 1
    [id] => 1
  )
)

有一个简单的数组函数还是我使用了循环?

2 个答案:

答案 0 :(得分:0)

循环-或写自己的..

$tgt = [];
foreach ( $src as $t) { $tgt[] = $t; }

答案 1 :(得分:0)

我不确定您如何获得阵列,但这对您有用吗?

        $main_array = array();

        $one = Array(
          "no_discount" => 0,
          "manufacturers_id" => 2,
          "id" => 3,
        );

        $two = Array(
          "no_discount" => 1,
          "manufacturers_id" => 1,
          "id" => 1,
        );


        array_push($main_array, $one);
        array_push($main_array, $two);


        print_r($main_array);