array_merge和array_map

时间:2012-01-23 21:09:29

标签: php

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

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执行其他一些方法,来到了$categoryarray_map个变量。现在我需要根据初始示例重新加入所有内容。我已经尝试array_merge(),但无法让它正常工作。 'category_x'的关键名称并不重要。我甚至不确定是否有可能按照我需要的顺序得到最终结果。任何帮助,将不胜感激。

3 个答案:

答案 0 :(得分:2)

使用array_keysforeach循环的组合,您可以轻松完成此操作。

示例:

<?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 :(得分: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);

?>

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

答案 2 :(得分: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.