Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional Array: How to get all values of a specific key? [duplicate]

I have an multidimensional array which includes IDs and URLs. I would like to output the URLs only.

$abstract_details = array(
                        array(
                            'id' => 68769782222,
                            'url' => 'http://yourclick.ch'
                        ),
                        array(
                            'id' => 111,
                            'url' => 'http://google.com'
                        )
                    );

foreach ($abstract_details as $abstract_detail) {
    foreach ($abstract_detail as $get_abstract_detail) {
        $result .= $get_abstract_detail . '<br>';
    }
}

When I run my code I get both information. How is it possible to take control of what I want to show?

like image 307
Reza Avatar asked Jul 14 '16 13:07

Reza


People also ask

How to get particular key value from multidimensional array in PHP?

To solve this, we will use the array_column() and function of array_column. The simple code to search the value in multidimensional array is described as follows: array_search($value['id'], array_column($studentsAddress, 'user_id'))

Can array have two same keys?

Code Inspection: Duplicate array keysIf multiple elements in the array declaration use the same key, only the last one will be used, and all others will be overwritten.

How do you check if a key exists in an array PHP?

PHP array_key_exists() Function The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.


1 Answers

Use array_column that will prevent you from foreach loop

$url = array_column($abstract_details, 'url');

echo implode('<br/>', $url); 
like image 92
Saty Avatar answered Sep 29 '22 15:09

Saty