Browse Array with Object inside, PHP

2

I have the following Array in PHP:

array(4) { 
    ["moduleName"]=> string(8) "Products" 
    ["id"]=> string(6) "14x937" 
    ["user"]=> object(Users)#523 (99) {} 
    ["data"]=> array(40) {} 
}

I try to go through it in all possible ways, like:

foreach ($Arraytotal as $key => $value) {
   echo $key.'-'.$value.'<br>';
   var_dump($key);
}

But I only get the data until the second component of the array, the data I need is in Arraytotal ['data'], I do not know how to get there.

Thank you.

    
asked by userNNNN 15.10.2018 в 16:37
source

1 answer

3

According to the var_dump that you show, in the key data of $Arraytotal there is an array.

So, you can access that array by looking for that key directly, for example:

foreach ($Arraytotal["data"] as $key => $value) {
     echo $key.'-'.$value.'<br>';
     var_dump($key);
}

Or you can take it apart and go through it:

$arrDatos=$Arraytotal["data"];
var_dump($arrDatos);

foreach ($arrDatos as $key => $value) {
     echo $key.'-'.$value.PHP_EOL;
     var_dump($key);
}

EDIT:

To access the object that is in user , you can create a reference, just like you did before:

$objUser=$Arraytotal["user"];
var_dump($objUser);

And then read it according to the properties it has, the difference here is that, the properties of the objects are accessed using ->propiedad , not like in the arrays that access ["propiedad"] .

That is, if your object has a property nombre , you only do this:

echo $objUser->nombre;

They can also be converted to an array, but if it's just for reading, it's not worth it.

    
answered by 15.10.2018 / 16:51
source