Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidemension array to single array by combianing values

I have an array with following format.

Array
(
    [0] => Array
            (
                [keyword] => thumbnail_follow
                [content] => width
            )

    [1] => Array
            (
                [keyword] => thumbnail_resize
                [content] => yes
            )

)

My desire output is following

Array
(
    [thumbnail_follow] => width
    [thumbnail_resize] => yes
)

What I have tried so far?

  1. array_shift() but this remove duplicate keys and output become

    Array
    (
        [keyword] => thumbnail_follow
        [content] => width
    )
    
  2. array_column I am able to get only single type of values. For example array_column($array, 'keyword')

    Array
    (
        [0] => thumbnail_follow
        [1] => thumbnail_resize
    )
    

Using array_column and loop, I can get desire output but there must be some more efficient way to do it but I am known from this

Can you please help me guys?

like image 941
Dev Hub Avatar asked Apr 11 '26 05:04

Dev Hub


1 Answers

Using array_column 's third parameter index_key you can do it. Please look at 2nd example of function.

Use it like this

print_r( array_column($array, 'content', 'keyword') );

You should get your desire output by this.

like image 81
Hassaan Avatar answered Apr 12 '26 21:04

Hassaan