将多个数组合并为一个具有关联行的二维数组

问题描述 投票:0回答:3

这就是我想要达到的目标:

array (
  'category_0' => 
  array (
    'count' => 12,
    'avg' => 11,
  ),
  'category_1' => 
  array (
    'count' => 14,
    'avg' => 13,
  ),
  'category_2' => 
  array (
    'count' => 16,
    'avg' => 15,
  ),
  'category_3' => 
  array (
    'count' => 18,
    'avg' => 17,
  ),
)

来自以下:

$category = 
array (
  0 => '9',
  1 => '10',
  2 => '11',
  3 => '12',
)

和:

$count =
array (
  0 => '12',
  1 => '14',
  2 => '16',
  3 => '18',
)

和:

$avg = 
array (
  0 => '11',
  1 => '13',
  2 => '15',
  3 => '17',
)

我通过在

$count
$avg
函数上使用
foreach
执行一些其他方法,得到了
$category
array_map
变量。现在我需要按照初始示例重新加入所有内容。我已经尝试过
array_merge()
但无法让它正常工作。 “category_x”的键名并不重要。我什至不确定是否可以按照我需要的顺序获得最终结果。任何帮助,将不胜感激。

php arrays multidimensional-array mapping merging-data
3个回答
2
投票

结合使用

array_keys
foreach
循环,您可以轻松完成此操作。

示例:

<?php

$category = array (
    0 => '9',
    1 => '10',
    2 => '11',
    3 => '12'
);
$count = array (
    0 => '12',
    1 => '14',
    2 => '16',
    3 => '18'
);
$avg = array (
    0 => '11',
    1 => '13',
    2 => '15',
    3 => '17'
);

$out = array();
$key = array_keys($category);
foreach ($key as $key) {
    $out["category_{$key}"] = array(
        'count' => isset($count[$key]) ? $count[$key] : 0,
        'avg' => isset($avg[$key]) ? $avg[$key] : 0
    );
}
print_r($out);

输出:

Array
(
    [category_0] => Array
        (
            [count] => 12
            [avg] => 11
        )

    [category_1] => Array
        (
            [count] => 14
            [avg] => 13
        )

    [category_2] => Array
        (
            [count] => 16
            [avg] => 15
        )

    [category_3] => Array
        (
            [count] => 18
            [avg] => 17
        )

)

1
投票

我可能会误解,但我认为你想得太多了。只需使用 for 循环即可构建主数组:

<?php
$category = array (
  0 => '9',
  1 => '10',
  2 => '11',
  3 => '12'
);


$count = array (
  0 => '12',
  1 => '14',
  2 => '16',
  3 => '18'
);

$avg =  array (
  0 => '11',
  1 => '13',
  2 => '15',
  3 => '17'
);

$final_array = array();
for($i=0;$i<count($category);$i++){
    $final_array['category_'.$i] = array(
        'count' =>  $count[$i],
        'avg'   =>  $avg[i]
    );
}

print_r($final_array);

?>

另外,请记住,您的代码将引发语法错误,因为最后一个元素后面不能跟逗号。


1
投票

无法通过函数调用执行类似的操作,您需要手动循环。如果这可能因为记录数量而导致问题,那么您可能希望重新思考和重构。

$res = array();

// category values are irrelevant, but it is easier than using array_keys
// (and never use for loops when you can use foreach: foreach can be 20% faster
foreach($category as $key=>$val)
{
    // use manual concatenation and it is generally about 1.5% faster
    // and clearer to your compatriots
    $res['Category_'.$key] = array(
          // since key maps to the same key for all, then simply use that 
          // for the lookup
          /*
            if you KNOW that there will always be a key in $count and $avg
            then use the following:
          */ 
          'count' => $count[$key],
          'avg' => $avg[$key]
           /*
             otherwise you will need to use this:
          'count' => isset($count[$key])?$count[$key]:0,
          'avg' => isset($avg[$key])?$avg[$key]:0
           */
    );
}
// $res now has everything you're looking for.
© www.soinside.com 2019 - 2024. All rights reserved.