Home > Net >  How to get the values of this array of objects in php
How to get the values of this array of objects in php

Time:02-15

Array ( [0] => Array ( [color] => #5fa4c4 )

[1] => Array
    (
        [color] => #147b5c
    )

[2] => Array
    (
        [color] => #ce0384
    )

)

CodePudding user response:

There’s a few ways you could do it. For, While, Foreach Loop etc. I’d personally use foreach as it requires less code.

<?php
// Array Data
$array = [["#5fa4c4"], ["#147b5c"], ["#ce0384"]];

// Loop Through Array Data
foreach($array as $data){
  $colour = $data[0];
  echo $colour;
}

CodePudding user response:

You can easily do this with foreach loop as below example

$colors = array( 
            array("color" => "#5fa4c4"),
            array("color" => "#147b5c"),
            array("color" => "#ce0384"),            
        );

foreach($colors as $color):
    echo $color['color'].'<br>'; //final colors values
endforeach;

output is

#5fa4c4
#147b5c
#ce0384
  •  Tags:  
  • php
  • Related