-4

I have a php array that looks like this...

(
 [name] => Tester
 [colors] => Array
 (
 [blue] => Array
 (
 [count] => 1
 [status] => hold
 )
 )
)

I am trying to get the first array from colors but have not been able to. I have tried...

echo $array['colors'][0];
echo $array->colors[0];

Neither of which have given me any results. Where am I going wrong?

asked Mar 18, 2021 at 11:59
5
  • I suppose you named your variable $array? Commented Mar 18, 2021 at 12:01
  • 1
    $array['colors']['blue'] Commented Mar 18, 2021 at 12:04
  • Does this answer your question? How to get the first item from an associative PHP array? Commented Mar 18, 2021 at 12:12
  • "Where am I going wrong?" - if we were to assume that you did not need to make an effort to learn any basics ever, because you can always just coming running to this community and have it fix whatever trivial problem you have now again for you, then nowhere ... But I personally would strongly disagree on that premise to begin with. Commented Mar 18, 2021 at 12:26
  • I would recommend reading through the manual page about arrays. It's pretty extensive. Commented Mar 18, 2021 at 12:29

1 Answer 1

1

The colors array has associative keys(eg. blue, etc...).

In order to access first element with $array['colors'][0],

need to convert array keys to numeric using array_values() function.

Either, access elements with associate keys like:

echo $array['colors']['blue'];
echo $array->colors['blue'];

Whichever best suits.

OR,

$colors = array_values($array['colors']);
echo $colors[0];
answered Mar 18, 2021 at 12:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.