从一维数组必须基于键获得多维数组

时间:2014-11-10 08:15:13

标签: php arrays multidimensional-array

有这样的数组

Array
(
[0] => Array
    (
        [CategoriesName_1] => Happy Birthday
        [CategoriesName_2] => Flowers
        [CategoriesName_3] => Fruit baskets
        [CategoriesDescription_1] => 
        [CategoriesDescription_2] => 
        [CategoriesDescription_3] => Fruit baskets descr
        [CategoriesUrl_1] => happy-birthday
        [CategoriesUrl_2] => flowers
        [CategoriesUrl_3] => fruit-baskets
    )

)

想要将它转换为这样的数组(需要得到这个结果)

Array
(
[0] => Array
    (
        [CategoriesName] => Happy Birthday
        [CategoriesDescription] => 
        [CategoriesUrl] => happy-birthday
    )
[1] => Array
    (
        [CategoriesName] => Flowers
        [CategoriesDescription] => 
        [CategoriesUrl] => flowers
    )
[2] => Array
    (
        [CategoriesName] => Fruit baskets
        [CategoriesDescription] => Fruit baskets descr
        [CategoriesUrl] => fruit-baskets
    )       

)

初始(一维数组)包含键CategoriesName_1CategoriesUrl_1CategoriesDescription_1。对于每个数字(_1_2_3),希望将名称,网址和说明放在一起并创建单独的子数组。

首先创建了3个独立的数组

foreach ( $array_with_breadcrumbs[0] as $key_brcr => $val_brcr ) {

if( 'CategoriesUrl_' == substr($key_brcr,0,14) ){
$array_url_brcr[] = array( 'CategoriesUrl' => $val_brcr );
}

if( 'CategoriesName_' == substr($key_brcr,0,15) ){
$array_name_brcr[] = array( 'CategoriesName' => $val_brcr );
}

if( 'CategoriesDescription_' == substr($key_brcr,0,22) ){
$array_description_brcr[] = array( 'CategoriesDescription' => $val_brcr );
}

}//foreach ( $array_with_breadcrumbs[0] as $key_brcr => $val_brcr ) {

然后像这样循环遍历所有三个数组

foreach( $array_url_brcr as $i_url => $val_url ){

  foreach( $array_name_brcr as $i_name => $val_name ){

    foreach( $array_description_brcr as $i_description => $val_description ){

      if( $i_url == $i_name ) {

      $combined_arr_brcr[] = array( 
      'CategoriesUrl' => $val_url['CategoriesUrl'], 
      'CategoriesName' => $val_name['CategoriesName'], 
      'CategoriesDescription' => $val_description['CategoriesDescription']
      );

      }

    }

  }

}

我得到九个(3个阵列* 3个循环)子阵列,但没有预期的结果。从结果我必须编写额外的代码,我采取(保持)第一,第五和第九子阵列。

代码显得很长。有没有更好(更短,更简单)的方法来获得结果?

2 个答案:

答案 0 :(得分:1)

$final_array=array();
for ($i=0; $i < 3; $i++) { 
    $k=$i+1;
    $final_array[$i]=array(
        'CategoriesName'=>$array_with_breadcrumbs[0]['CategoriesName_'.$k],
        'CategoriesDescription'=>$array_with_breadcrumbs[0]['CategoriesDescription_'.$k],
        'CategoriesUrl'=>$array_with_breadcrumbs[0]['CategoriesUrl_'.$k],
        );
}

print_r($final_array);

如果键数超过3,则可以使用此

$final_array=array();
$no_of_keys=3; //change the value of number of keys
$vars=array('CategoriesName','CategoriesDescription','CategoriesUrl'); // add variables

for ($i=0; $i < $no_of_keys; $i++) { 
    $k=$i+1;
    $final_array[$i]=array();
    foreach ($vars as $var) {
        $final_array[$i][$var]=$array_with_breadcrumbs[0][$var."_".$k]
    }
}

print_r($final_array);

答案 1 :(得分:0)

$source = $array_with_breadcrumbs[0];
$result = [];

for ($i = 1; $i <= 3; $i++)
{
    array_push($result,
    [
        'CategoriesName' => $source['CategoriesName_' . $i],
        'CategoriesDescription' => $source['CategoriesDescription_' . $i],
        'CategoriesUrl' => $source['CategoriesUrl_' . $i]
    ]);
}

var_dump($result);