1

I have an array like this , which I am getting as a result of a database query (Google BigQuery) :

Array
(
    [0] => Array
        (
            [points] => 95
            [user] => 434
            [type] => 20
            [identifier] => tv
            [date] => 2016-11-01
        )

    [1] => Array
        (
            [points] => 349
            [id] => 2989631
            [type] => 20
            [identifier] => app
            [date] => 2016-11-01
        )
) 

and another one for identifiers :

Array
(
    [tv] => 1
    [app] => 2
)

What I need is to do is to transform the array as the identifier key has corresponding value from identifiers array. So it will look like :

Array
(
    [0] => Array
        (
            [points] => 95
            [user] => 434
            [type] => 20
            [identifier] => 1
            [date] => 2016-11-01
        )

    [1] => Array
        (
            [points] => 349
            [id] => 2989631
            [type] => 20
            [identifier] => 2
            [date] => 2016-11-01
        )
) 

How can I do this using Laravel collections ?

I have been trying with transform function, but not getting an idea to return specific column from the multidimensional array.

2 Answers 2

1

You can use array_map():

$identifiers = [
    'tv' => 1,
    'app' => 2
];

$result = array_map(function($item) use ($identifiers) {
    $item['identifier'] = $identifiers[$item['identifier']];
    return $item;
}, $itemsArray);
Sign up to request clarification or add additional context in comments.

Comments

0

Probably the best solution here would be using an acessor:

public function getIdentifier($value)
{
    return $identifiersArray[$value];
}

If you do this, you will not need to rebuild your collection.

1 Comment

One thing is I am using Google Bigquery as the database and so using their API to execute query. They are basically returning an array.

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.