stdClass Object
(
[string] => Array
(
[0] => EXL
[1] => TEMPS
)
)
stdClass Object
(
[string] => IP
)
How to access to EXL, TEMPS and IP values with a loop for ?
stdClass Object
(
[string] => Array
(
[0] => EXL
[1] => TEMPS
)
)
stdClass Object
(
[string] => IP
)
How to access to EXL, TEMPS and IP values with a loop for ?
You have to access the parent array as Object, but the child's are normal array.
SO try this.
$array->string[0]; //get the EXL
Example:
$array = array(
"string" => array("EXL", "TEMPS"),
"string2" => array("EXL 2", "TEMPS 2"),
);
$obj_arr = (Object) $array;
echo "<pre>";
print_r($obj_arr);
echo "</pre>";
echo $obj_arr->string[0]."<br/>".$obj_arr->string[1];
Output:
EXL
TEMPS
In your case, this is an instance of a Class which gives you an Object.
There are two commonly used methods to target an Array within an Object:
// Loop through each stored data
foreach($Object->string as $_string)
{
echo $_string;
}
Or you can access the Array directly:
echo $Object->string[0];
The ->
in PHP is how we use Objects (map).
Both work fine.
EDIT: Reading comments
To access the Array held in the Object within a for
loop:
// $i starts at 0 since array index's start at 0
for($i = 0; $i < count($Obj->string); $i++)
{
echo $Obj->string[$i];
// TODO: Add your code...
}