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"
    },
    {..}
]
Friso Hoekstra
  • 845
  • 2
  • 9
  • 24
  • 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. – killstreet Jul 31 '18 at 11:38
  • 2
    Possible duplicate of [How to rename array keys in PHP?](https://stackoverflow.com/questions/9605143/how-to-rename-array-keys-in-php) – Ethan Jul 31 '18 at 11:38
  • It looks like you got this data as json so you could simply `str_replace('"link"', '"url"', $json);` – Nick Jul 31 '18 at 11:40
  • Using select() w/ Eloquent did the trick. Thank you – Friso Hoekstra Jul 31 '18 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. – MonkeyZeus Jul 31 '18 at 11:53
  • changed the title for clarification purposes – Friso Hoekstra Jul 31 '18 at 11:59

2 Answers2

6

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

killstreet
  • 1,251
  • 2
  • 15
  • 37
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.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • 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. – Friso Hoekstra Jul 31 '18 at 11:52
  • 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. – killstreet Jul 31 '18 at 11:58
  • 1
    @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. – MonkeyZeus Jul 31 '18 at 12:00
  • @killstreet Agreed, I wasn't aware that this was a DB result. Should have checked the tags! – MonkeyZeus Jul 31 '18 at 12:01