0

I've got an multi-array (currently with objects) that I want to reorder based on a specific key/value.

Array
(
[0] => stdClass Object
    (
        [task_id] => 1
        [task_title] => Title
        [users_username] => John
    )
[1] => stdClass Object
    (
        [task_id] => 2
        [task_title] => Title
        [users_username] => John
    )
[2] => stdClass Object
    (
        [task_id] => 3
        [task_title] => Title
        [users_username] => Mike
    )
)

I'd like to reorder it to get multi-arrays by user_name, so I can cycle through the task by username.

Array
(
    [John] => Array
        (
            [0] => Array
                (
                    [task_id] => 1
                    [title] => Title
                )

            [1] => Array
                (
                    [task_id] => 2
                    [title] => Title
                )

        )

    [Mike] => Array
        (
            [0] => Array
                (
                    [task_id] => 3
                    [title] => Title
                )

        )

)

Is it possible to recreate my array to an array like that above?

4 Answers 4

1

Updated version of the code

<?php
$it0 = (object) array('task_id' => 1,'task_title' => 'Title','users_username' => 'John');
$it1 = (object) array('task_id' => 2,'task_title' => 'Title','users_username' => 'John');
$it2 = (object) array('task_id' => 3,'task_title' => 'Title','users_username' => 'Mike');

$array = array($it0,$it1,$it2);

$return = array();
foreach($array as $id => $value){
  $return[$value->users_username][] = array('task_id' => $value->task_id,'title' => $value->task_title);
}

var_dump($return);
2
  • 1
    Thanks, thats pretty short and works. Didn't know it's such simple :-) Commented Jan 8, 2012 at 12:25
  • Not really sure how you got this to work as it's invalid syntax in PHP. Commented Jan 9, 2012 at 3:34
1

Yes, it is possible. You'll have to loop through your current array and create a new array to do it.

example:

$new_array = array();
foreach ($array as $row)
{
    $new_row = array(
        'task_id' => $row->task_id,
        'title' => $row->task_title,
    );
    $name = $row->users_username;

    if (isset($new_array[$name]))
    {
        $new_array[$name][] = $new_row;
    }
    else
    {
        $new_array[$name] = array($new_row);
    }
}

Now $new_array contains the new array exactly like the one you're asking for.

Then you can sort it with

ksort($new_array);
0
1

There may be another way to do this, with some built-in function, but sometimes I'd rather just do it myself, and know how it is working, without having to look up the documentation.

The approach:

Iterate through all of the first array, looking at [users_username] and putting them into a new array.

Code:

$dst_array = array();
foreach ($src_array as $val)
{
     $user = $val->users_username;

     // TODO: This check may be unnecessary. Have to test to find out.
     // If this username doesn't already have an array in the destination...
     if (!array_key_exists($user, $dst_array))
         $dst_array[$user] = array();  // Create a new array for that username

     // Now add a new task_id and title entry in that username's array
     $dst_array[$user][] = array(
         'task_id' => $val->task_id
         'title' => $val->title
     );
}
0

Just something like this (maybe not 100% PHP code):

foreach ( $obj : $oldArray ) {
  $newTask['task_id'] = $obj->task_id;
  $newTask['title'] = $obj->title;

  $newArray[$oldName][] = $newTask;
}

If you want to order it; you can just call a order function afterwards.

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.