But in case, you don’t have a collection to go through, so you can’t go through it, to access the data after converting into array would just do this:
echo $array["Clientedata"]["nome"] . '\n<br>' .
$array["Clientedata"]["email"] . '\n<br>' .
$array["Clientedata"]["celular"];
To access directly from the object:
echo $objeto->Clientedata->nome . '\n<br>' .
$objeto->Clientedata->email . '\n<br>' .
$objeto->Clientedata->celular;
If Clientedata
were a collection, you could make these two forms:
a) You wouldn’t necessarily need to convert it to array to go through it, but if you prefer to go through it in array mode, just do so:
$array = (array) $objeto;
if (count($array['Clientedata'])) {
foreach($array['Clientedata'] as $key => $value){
echo $value['nome'] . '<br>\n';
echo $value['email'] . '<br>\n';
echo $value['celular'];
}
}
b) In the case of object, it is only to make a Countable implement in its class:
//No php 7, essa função deve existir, mas se não existir ela é criada abaixo:
if (!function_exists('is_countable')) {
function is_countable($value): bool
{
return is_array($value) || (is_object($value) && $value instanceof Countable);
}
}
//então vc checa se o elemento possui uma coleção:
if (is_contable($objeto->Clientedata)) {
//e percorre ele...
foreach( $objeto->Clientedata as $key => $value){
echo $value->nome . '<br>\n';
echo $value->email. '<br>\n';
echo $value->celular;
}
}
}
How are you traversing this array? If possible [Edit] your question and add your code.
– Valdeir Psr
From what I saw and multidimensional. The foreach will go through the first dimension. If you want to go through the second dimension you must place foreach inside foreach. It’s so on. Or specify the dimension you want to navigate by informing the key to the dimension.
– Willian Coqueiro
What I don’t understand is why, since if I create an object with stdClass and turn it into an array I can run it normally.
– Daniel Neto
What happens in this error is that the first dimension does not contain values to be printed as string. You must now enter the second dimension. There you asked to print the $key it even prints. More $value is another array. That is the second dimension. For you to view it you must do so print_r($value) or var_dump($value).
– Willian Coqueiro
But regardless of why, I managed to traverse the "second dimension" with another foreach.
– Daniel Neto
But because this first dimension is created?
– Daniel Neto
I don’t know where you’re getting this data from. But this is used a lot when you want to separate data by dimensions. Type in first dimension has number of work orders. There in the second dimension can have N array containing the identification of each customer data, order data, company data and in the third dimension can have the data of each of the items of the second dimension as customer name, Cpf, etc. understood?
– Willian Coqueiro
I get it, thank you.
– Daniel Neto
Now that I’ve seen it. Access the data as follows. echo $value->name; echo $value->email; And you can foreach without key as well. Type foreach($array as $a){ echo $a->name; }
– Willian Coqueiro
This is not a collection for you to browse!
– Ivan Ferrer