3

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?

2 Answers 2

5

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.

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

1 Comment

Thank you @Hassaan it works. I did not notice the third parameter
0

Try this

$newArr = [];
foreach($yourArr as $row) {
   $newArr[$row['keyword']] = $row['content'];
}

1 Comment

Thanks, I was looking for the way to do it without using loop

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.