0

I have an array like this:

[1] => Array
        (
            [id] => 7
            [ticket_id] => 12
            [client_id] => 174
            [thread_name] => 
            [added_by] => 2
            [created_at] => 2015-07-28 23:24:07
            [updated_at] => 2015-07-28 23:24:07
            [notes] => Array
                (
                    [0] => Array
                        (
                            [id] => 21
                            [user_id] => 2
                            [notes] => Not all those who wander are lost!
                            [notes_attachment] => 
                            [deleted_at] => 
                            [created_at] => 2015-07-28 23:34:31
                            [updated_at] => 2015-07-28 23:34:31
                            [thread_id] => 7
                            [users] => Array
                                (
                                    [0] => Array
                                        (
                                            [user_id] => 1
                                        )

                                    [1] => Array
                                        (
                                            [user_id] => 2
                                        )

                                )

                        )

                )

        )

And I need to manipulate the users array in the notes array to something like this.

[users] => Array
(
[0] => 1
[1] => 2
)

So, basically, I need an array for ids in the users array.

I tried doing it, but it takes three loop to get into users.

How can I do that more efficiently ??

NOTE: This is just one of the structure from multiple arrays. Like, I have 4 threads and multiple notes in each of them.

Also, I need to manipulate the existing array and not just take the data from it.

My attempt:

$tmp = array();
        foreach($data as $value) {
            foreach($value['notes'] as $notes) {
                if(!empty($notes['users'])){
                    foreach($notes['users'] as $users) {
                        $tmp[] = $users['user_id'];
                        unset($notes['users']);
                        $notes['users'] = $tmp;
                    }
                }
            }
        }
2
  • There isn't really any faster way to do this unless you format your data differently or you know the id of the notes array you want. Commented Jul 29, 2015 at 4:59
  • This is the array I get using Eloquent in Laravel. It serves the purpose, but there's this little tweak I need to do for users. Commented Jul 29, 2015 at 5:08

1 Answer 1

1

Use array_column -

$new = array_column($a[notes][0]['users'], 'user_id');
var_dump($new);

Output

array(2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}

DEMO

Update

$new = array();
foreach($a as $value) {
    $new = array_merge($new, array_column($value[notes][0]['users'], 'user_id'));
}
var_dump($new);
Sign up to request clarification or add additional context in comments.

10 Comments

From the question it sounds like he doesn't know the id of the users array, thus the need to loop through them
@Ethan22 If the array structure is this then no loop needed. You can check the demo.
I don't this will work. I mean there are some 100's of entries having users in it.
True, if it is like this. I just assumed from the ` [notes] => Array ( [0] => Array` that there was a possibility of a [1] etc.
Ah, good point @b0s3. He could loop through using $a[notes][$i]
|

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.