分解数据并构建新数组

我需要分解数组的键值并构建一个具有分解结果的新数组。下面的代码适用于一个子数组,我想我缺少一个 for 循环来处理数组值迭代。


该解决方案还应处理“金融”子数组数据,以便在新数组中展开并显示。


我在后期会有 9 个子数组,因此需要分解数据并将结果移动到新数组中。


我的代码


<?php


$array = [

  'company_info' => [

    'country_period_0'  => 10,

    'currency_period_0' => 20

  ],

  'finance'      => [

    'values_period_0' => 30

  ]

];


$newArray = [];


for ($i=0; $i <= 1 ; $i++) {


  $array_1     = $array['company_info'];

  $arrayKeys   = array_keys($array_1);

  $arrayValues = array_values($array_1);


  $keySplits   = explode("_", $arrayKeys[$i]);


  for ($i=0; $i <= 2 ; $i++) {

    $newArray[] = $keySplits[$i];

  }

    $newArray[3] = $arrayValues[0];


}


print_r($newArray);

结果


Array(

    [0] => country

    [1] => period

    [2] => 0

    [3] => 10

)

想要的结果


['company_info]

Array(

    [0] => country

    [1] => period

    [2] => 0

    [3] => 10

)

Array(

    [0] => currency

    [1] => period

    [2] => 0

    [3] => 20

)

['finance']

Array(

    [0] => values

    [1] => period

    [2] => 0

    [3] => 30

)


肥皂起泡泡
浏览 98回答 2
2回答

慕仙森

您可以使用循环大大简化它foreach,尤其是每次获取键和值以帮助构建数组。这也使用第一级的键explode()将结果添加到using ,但也只是将值添加到数组的末尾 using ... $newArray$newArray[$mainKey][][]foreach ( $array as $mainKey => $elements )&nbsp; {&nbsp; &nbsp; foreach ( $elements as $subKey => $value ){&nbsp; &nbsp; &nbsp; &nbsp; $newData = explode("_", $subKey);&nbsp; &nbsp; &nbsp; &nbsp; $newData[] = $value;&nbsp; &nbsp; &nbsp; &nbsp; $newArray[$mainKey][] = $newData;&nbsp; &nbsp; }}用你的测试数据给出......Array(&nbsp; &nbsp; [company_info] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => country&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => period&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [3] => 10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => currency&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => period&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [3] => 20&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [finance] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => period&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [3] => 30&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ))我只是注意到我丢失了第二个company_info数据,所以这意味着值将始终是数组,除非你真的只在需要时才需要它们是数组。

30秒到达战场

$new_array=[];foreach($array as $category => $tmp ){&nbsp; foreach($tmp as $key => $value){&nbsp; &nbsp; $exp = explode('_', $key);&nbsp; &nbsp; $exp[] = $value;&nbsp; &nbsp; $new_array[ $category ][] = $exp;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP