将单个数组转换为多数组

时间:2019-04-18 04:09:08

标签: php

我有一个如下所示的数组:

Array ( [:status0] => 1 [:status1] => 2 ) 

我想将其转换为如下形式:

Array ( [:status0] => Array ( [0] => 1 [1] => 1 ) [:status1] => Array ( [0] => 2 [1] => 1 ) ) 

我想灵活地做到这一点,因为数组的数目和名称都是随机的。我当时正在考虑使用如下这样的for循环:

foreach ($newParam as $row){
    $newArray[$row['Continent']][$row['Country']][] = $row['City'];
}

但是我不能使用此功能,请帮忙

1 个答案:

答案 0 :(得分:1)

此代码应执行您想要的操作:

$newParam = Array ( ':status0' => 1, ':status1' => 2 ) ;
foreach ($newParam as $key => $value) {
    $newArray[$key] = array($value, 1);
}
print_r($newArray);

输出:

Array (
  [:status0] => Array (
    [0] => 1
    [1] => 1
  )
  [:status1] => Array (
    [0] => 2
    [1] => 1 
  )
)

Demo on 3v4l.org