PHP: How to get the value of an array by its numerical offset, if it is an associative array?

I have an associative array, which when var dumped looks like this:

Array ( [tumblr] => Array ( [type] => tumblr [url] => http://tumblr.com/ ) [twitter] => Array ( [type] => twitter [url] => https://twitter.com/ ) ) 

As you can see, the keys are custom tumblr and twitter, not the numeric 0 and 1.

Several times I need to get values ​​using custom keys, and sometimes I need to get values ​​using numeric keys.

Is there a way I can get $myarray[0] for output:

 ( [type] => tumblr [url] => http://tumblr.com/ ) 
+6
source share
2 answers

You can run an array through array_values() :

 $myarray = array_values( $myarray); 

Your array now looks like this:

 array(2) { [0]=> array(2) { ["type"]=> string(6) "tumblr" ["url"]=> string(18) "http://tumblr.com/" } ... 

This is because array_values() will only capture values ​​from the array and reset / reorder / rekey the array as a numeric array.

+9
source

You can use array_values to get a copy of an array with numeric indices.

0
source

Source: https://habr.com/ru/post/919179/


All Articles