PHP - agglomerate arrray by id

i have data like this

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

i want agglomerate color by id.. and i want result like this:

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
        )

)

how i make it.. i'm still newbie, i'm little confusing make it

Answers


You can do like this:

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);

Save like in given example and simply use array_values function in your array which provide above output.

    $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);

To jfremblay solution you can add two strings to the end and the output array will be exact the same:

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

or this:

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

And final code is:

$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);

This code is a solution to your problem:

$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 of this code is (screenshot):

Hope this helps :-)


Need Your Help

Problems when executing services within cron in a Docker container?

linux shell cron sh docker

I have some cronjobs that I always used and worked fine. But now, trying to move everything to Docker containers, I'm running into these errors:

AngularJS ajax form submit with submit input

javascript ajax angularjs angularjs-directive angularjs-scope

I have a form which having stepy wizard.For working stepy wizard,it is must to use input submit button not a button.So I am using submit button,but while clicking submit button it is redirecting t...