2

I have an array as follows

array(2) { ["name"]=> string(36) "newyork" ["categoryid"]=> string(1) "4" }                                                                                                                                                                                                            
array(2) { ["name"]=> string(24) "michigun" ["categoryid"]=> string(1) "4" } 
array(2) { ["name"]=> string(27) "canada" ["categoryid"]=> string(1) "5" }             
array(2) { ["name"]=> string(35) "manhatten" ["categoryid"]=> string(1) "5"   
}

i want to have a new array where i will have

 {"4" => newyork,michigun}
 {"5" => canada,manhatten}

.. how can it be done? pls help.

3
  • i tried foreach($key => $value) but i m not sure about the logic. Commented Jul 29, 2015 at 5:59
  • you want to add in this array or something else? please paste your desired outcome.Thanks. Commented Jul 29, 2015 at 6:00
  • i want a new array which will print {"4" => newyork,michigun} {"5" => canada,manhatten} Commented Jul 29, 2015 at 6:01

3 Answers 3

4

You can use a loop and build the array -

$new = array();
foreach($your_array as $array) {
   if(array_key_exists($array['categoryid'], $new)) { // if categoryid is already set the concatenate the name
       $new[$array['categoryid']] = $new[$array['categoryid']] . ',' . $array['name'];
   } else { // set the name
       $new[$array['categoryid']] = $array['name'];
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

can u explain the $new[$array['categoryid']] = $new[$array['categoryid']] . ',' . $array['name']; statement ?
It will concatenate the name with the value which has index of the categoryid.
4

If don't mind using external lib, it is pretty straightforward with ouzo-goodies:

Arrays::groupBy($array, Functions::extractField('categoryid'));

You can go one step further to join names with commas:

$result = Arrays::groupBy($array, Functions::extractField('categoryid'));
Arrays::map($result, function($value) {
    return Joiner::on(',')->mapValues(Functions::extractField('name'))->join($value);
});

Comments

1

Uses the PHP 5.4 array syntax: [] instead of array()

Data

$arr = [
  [
    "name" => "newyork",
    "categoryid" => "4"
  ],
  [
    "name" => "michigun",
    "categoryid" => "4"
  ],
  [
    "name" => "canada",
    "categoryid" => "5"
  ],
    [
    "name" => "manhatten",
    "categoryid" => "5"
  ],
];

Parse

$result = [];

foreach ($arr as $el) {
  if (array_key_exists($el['categoryid'], $result)) {
    $result[$el['categoryid']] = $result[$el['categoryid']] . ',' . $el['name'];
  } else {
   $result[$el['categoryid']] = $el['name'];
  }
}


print_r($result);

Result

Array ( [4] => newyork,michigun [5] => canada,manhatten ) 

3 Comments

This is simply copy and paste of this answer
Settle down everybody. @b0s3 just beat me to the punch is all.
@KetkiNimdeo then how does this answer make difference with this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.