2

I have an array of objects coming from an Eloquent query:

[
    {
        "id": 1,
        "user_id": 1,
        "name": null,
        "link": "storage/images/foo.png"       
    },
    {
        "id": 2,
        "user_id": 1,
        "name": null,
        "link": "storage/images/foo.png"
    },
    {..}
]

In PHP, what would be the most efficient way to rename the "link" key to "url" in every object in the array? Keeping the same order of elements would be a bonus.

I'm hopeful to achieve this:

[
    {
        "id": 1,
        "user_id": 1,
        "name": null,
        "url": "storage/images/foo.png"       
    },
    {
        "id": 2,
        "user_id": 1,
        "name": null,
        "url": "storage/images/foo.png"
    },
    {..}
]
6
  • 3
    guessing you are using eloquent to get ur data, like User::all() or something alike, try making it a select query as follow: User::select('id, user_id, link as url')->get(); to have this fixed for you. Commented Jul 31, 2018 at 11:38
  • 2
    Possible duplicate of How to rename array keys in PHP? Commented Jul 31, 2018 at 11:38
  • It looks like you got this data as json so you could simply str_replace('"link"', '"url"', $json); Commented Jul 31, 2018 at 11:40
  • Using select() w/ Eloquent did the trick. Thank you Commented Jul 31, 2018 at 11:51
  • 1
    @killstreet All my hard work for nothing! Ha, j/k, you should post your comment as an answer; I would upvote it for the simple fact that you are clearly a mind reader lol. Commented Jul 31, 2018 at 11:53

2 Answers 2

7

As my comment seemed to be the correct way, and for sure faster than looping over an already generated array ill post it as an answer here.

guessing you are using eloquent to get ur data

User::all() 

or something alike, try making it a select query as follow:

User::select('id, user_id, link as url')->get(); 

to have this fixed for you

Sign up to request clarification or add additional context in comments.

Comments

4

For future visitors.

This answer is based on OP's original request of how to rename object keys using PHP. Upon further clarification, OP mentioned that they have access to change the DB query.

However, this answer is a very viable way to alter keys if you do not have access to the source data such as pulling JSON from someone else's server.


It's quite simple. You need to loop, set, and unset:

// Loop the array and get each object into $v by reference
// Objects pass-by-reference by default since PHP 5.0 so no need for &$v in this situation
foreach( $arr as $v )
{
    // Since $v is an object reference, manipulate it directly by setting a url
    $v->url = $v->link;

    // We can unset link
    unset( $v->link );
}

Because link was last anyways, we are able to simply add url to the end and remove link.

If link was in the middle somewhere then this solution would have been a bit trickier because we would have needed to maintain two arrays.

4 Comments

I ended up doing it within Eloquent (see killstreet's comment), because I'm not sure how a big array would affect performance, but this also worked well.
As your answer will work, this will become quite slow when trying this with too many records. Doing this on database level will save code and time.
@FrisoHoekstra Your concerns are justified. Even though 100,000 iterations takes 0.19 seconds in PHP5 and 0.01 in PHP7 it is definitely better to just have the database alias link into url so that you're not wasting CPU cycles.
@killstreet Agreed, I wasn't aware that this was a DB result. Should have checked the tags!

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.