PHP - 通过id聚合数组

时间:2014-03-12 12:46:19

标签: php arrays

我有这样的数据

id       color  
102      blue  
102      red  
102      pink  
114      green  
114      red
114      yellow  
114      gray  
118      white  
118      blue  

我希望通过id来聚集颜色..我想要这样的结果:

Array
(
    [0] => Array
        (
            [0] => blue
            [1] => red
            [2] => pink
        )

    [1] => Array
        (
            [0] => green
            [1] => red
            [2] => yellow
            [3] => gray
        )

    [2] => Array
        (
            [0] => white 
            [1] => blue
        )

)

我是怎么做到的......我还是新手,我有点迷惑吗

4 个答案:

答案 0 :(得分:1)

你可以这样做:

unset($array);
$array = array(
0 => array("id"=>102,"color"=>"blue"),
1 => array("id"=>102,"color"=>"red"),
2 => array("id"=>102,"color"=>"pink"),
3 => array("id"=>114,"color"=>"green"),
4 => array("id"=>114,"color"=>"red"),
5 => array("id"=>114,"color"=>"yellow"),
6 => array("id"=>114,"color"=>"gray"),
7 => array("id"=>118,"color"=>"white"),
8 => array("id"=>118,"color"=>"blue")
);

unset($output);
if (is_array($array) && count($array) > 0) {
   foreach ($array as $value) {
      $output[$value["id"]][] = $value["color"];
   }
}
//[EDIT] ADD this:
$output=array_values($output);

答案 1 :(得分:0)

在给定示例中保存,只需在数组中使用提供上述输出的array_values函数。

    $color[102]=array('blue','red','pink');
$color[114]=array('green','red','yellow','gray');
$color[118]=array('white','blue');
$color=array_values($color);
echo '<pre>';
print_r($color);

答案 2 :(得分:0)

对于jfremblay解决方案,您可以在末尾添加两个字符串,输出数组将完全相同:

ksort($output);
$output = array_values($output);

或者这个:

ksort($output);
$output = array_values($output);

最终代码是:

$colorArray = array(
    array('id'    => 102, 'color' => 'blue'),
    array('id'    => 102, 'color' => 'red'),
    array('id'    => 102, 'color' => 'pink'),
    array('id'    => 114, 'color' => 'green'),
    array('id'    => 114, 'color' => 'red'),
    array('id'    => 114, 'color' => 'yellow'),
    array('id'    => 114, 'color' => 'gray'),
    array('id'    => 118, 'color' => 'white'),
    array('id'    => 118, 'color' => 'blue')
);

$output = array();
if ( is_array($colorArray) && count($colorArray) > 0 ) {
    foreach ( $colorArray as $value ) {
        $output[$value["id"]][] = $value["color"];
    }
}

ksort($output);
$output = array_values($output);
var_dump($output);

答案 3 :(得分:0)

此代码是您问题的解决方案:

$colors = array();

$colors[] = array("id" => 102, "color" => "blue");
$colors[] = array("id" => 102, "color" => "red");
$colors[] = array("id" => 102, "color" => "pink");
$colors[] = array("id" => 114, "color" => "green");
$colors[] = array("id" => 114, "color" => "red");
$colors[] = array("id" => 114, "color" => "yellow");
$colors[] = array("id" => 118, "color" => "gray");
$colors[] = array("id" => 118, "color" => "white");
$colors[] = array("id" => 118, "color" => "blue");

$lastId = null;
$temp = array();
$arrays = array();
foreach ($colors as $color) {
    if ($lastId != $color['id'] && $lastId != null) {
        $arrays[] = $temp;
        $temp = array();
    }
    $lastId = $color['id'];
    $temp[] = $color['color'];
}

$arrays[] = $temp;

echo '<pre>';
print_r($arrays);
echo '</pre>';

此代码的输出是(截图):

Output screenshot

希望这会有所帮助: - )