Ajvanho's avatar
Level 14

Collection replace ?

I have an array

 36 => array:3 [
          "key" => "7beqbabdHqdd3NMZ"
          "attributes" => array:2 [
            "url" => "GitHub"
            "name" => "https://github.com/foo"
          ]
        ]

How to use replace collection to be:

"url" =>  "https://github.com/foo",
"name" => "GitHub"
0 likes
3 replies
tisuchi's avatar

@ajvanho I guess you don't need to use collection here if it is an array. You can try this-

    $lists = [
        "key" => "7beqbabdHqdd3NMZ",
        "attributes" => [
            "url" => "GitHub",
            "name" => "https://github.com/foo"
        ]
    ];

    $replacedValues = [
        "name" => "GitHub",
        "url" => "https://github.com/foo",
    ];

    $finalOutput = array_replace($lists, $replacedValues);

1 like

Please or to participate in this conversation.