0

I have an array called $data which each comprises of an array containing "listid" and "status" as illustrated below when looping through and var_dumping each element in $data:

array(2) { ["listid"]=> string(1) "0" ["status"]=> string(1) "0" } 
array(2) { ["listid"]=> string(2) "17" ["status"]=> string(1) "2" } 
array(2) { ["listid"]=> string(2) "17" ["status"]=> string(1) "4" } 
array(2) { ["listid"]=> string(2) "17" ["status"]=> string(1) "0" } 
array(2) { ["listid"]=> string(1) "0" ["status"]=> string(1) "6" } 
array(2) { ["listid"]=> string(2) "17" ["status"]=> string(1) "1" } 
array(2) { ["listid"]=> string(2) "17" ["status"]=> string(1) "0" } 
array(2) { ["listid"]=> string(2) "17" ["status"]=> string(1) "2" } 

I want to re-arrange this array to group by "listid". So the new array should look something like:

$new_array[0]["listid"]==17
$new_array[0]["status"]==array(2,4,0,1,0,2)
$new_array[1]["listid"]==0
$new_array[1]["status"]==array(0,6)

How can I re-arrange the original array to this format and is there a php function which could do this?

3
  • 1
    Construct a for/foreach loop on $data, assign values as required into your new array structure. Commented Dec 3, 2015 at 23:39
  • 1
    Typically you would loop over your array and assign the values to a new array using the value you want to group on as the key in the new array. Commented Dec 3, 2015 at 23:40
  • And to make life easier, use the listid as your array index on $new_array so you can target the assignment with ease. Using array_values() if you need to return to a 0,1,2 array index. Commented Dec 3, 2015 at 23:41

3 Answers 3

1

Typically you would loop over your array and assign the values to a new array using the value you want to group on as the key in the new array. Something like this:

$newArray = array();

foreach($data as $v){
    $newArray[$v['listid']]['status'][] = $v['status'];
    $newArray[$v['listid']]['listid'] = $v['listid'];
}
//to reset the keys back to 0-N, use array_values
$newArray = array_values($newArray);
Sign up to request clarification or add additional context in comments.

Comments

1

What happens when You call array_values on $new_array it self?

$new_array = array_values($new_array);

array_values() will reset numerical keys. From what I see, all You need is numerical sorting of primary key, listid will come first, status will follow as secondary, naturally.

Comments

1

Just simply loop over your $data and use the isset() and listid to trace each $new_array_item.

$new_array_data = array();

foreach($data as $data_key => $data_item) {

    $listid = $data_item['listid'];

    if($new_array_data && !empty($new_array_data) && isset($new_array_data[$listid])) {
        $new_array_item = $new_array_data[$listid];
    } else {
        $new_array_item = array();
    }

    $new_array_item['listid'] = $listid;
    $new_array_item['status'][] = $data_item['status'];
    $new_array_item['status'] = array_unique($new_array_item['status']);

    $new_array_data[$listid] = $new_array_item;

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.