Perhaps it is a small lack of attention, the secret is to remember that it is a multidimensional object, which has several layers containing arrays or other obejtos:
public 'det' =>
object(stdClass)[2]
public 'attributes' =>
array (size=1)
'nItem' => string '1' (length=1)
public 'prod' =>
object(stdClass)[3]
public 'cProd' => string '382597' (length=6)
public 'cEAN' => string '7899882306668' (length=13)
public 'xProd' => string 'VENT COL MOND NV06 6P 140W BR/AZ 110V' (length=37)
public 'NCM' => string '84145990' (length=8)
public 'CEST' => string '2108900' (length=7)
public 'CFOP' => string '5405' (length=4)
public 'uCom' => string 'PC' (length=2)
public 'qCom' => string '1.0000' (length=6)
public 'vUnCom' => string '189.0000000000' (length=14)
public 'vProd' => string '189.00' (length=6)
(I reproduced the return you sent in direct object in php, instead of xml, for the explanation, but in your return it will work the same way)
So, using the foreach()
just as you were doing, would accuse an error, because there is no object that you were trying to print.
Like the $ver
is composed of a object 'det'
, which in turn consists of a array 'attributes'
and another object 'prod'
, the tie foreach()
tries to print from inside the array 'attributes'
as if it were an object and accuses an error.
So instead of using the foreach()
, in that case, print directly:
echo $ver->det->prod->xPrdo;
Or set a new variable for the root of object 'prod'
:
$produto=$ver->det->prod;
To facilitate access to information, may be options for you:
echo $produto->xPred;
Edit:
In the case of the file .xml
, using the function simplexml_load_file, as quoted by @Virgilionovic in the first reply, the representation does not include the det
as in the example I gave, so the impression would be:
//leitura do arquivo
$xml=simplexml_load_file("nfe.xml");
//verifica se o arquivo abriu
if (!$xml) {
echo "Erro ao abrir arquivo!";
exit;
}
//imprime os componentes
echo $xml->prod->xProd;
For ease of use, here can also be used a variable for the root:
$produto=$xml->prod;
echo $produto->xPred;
of the one
var_dump($ver)
before the foreach to confirm– Wees Smith
returned ["det"]=> Object(Simplexmlelement)#10 (3) { ["@Attributes"]=> array(1) { ["nItem"]=> string(1) "1" } ["Prod"]=> Object(Simplexmlelement)#15 (15) { ["cProd"]=> string(6) "382597" ["cEAN"]=> string(13) "7899882306668" ["xProd"]=> string(37) "VENT COL MOND NV06 6P 140W BR/AZ 110V" ["NCM"]=> string(8) "84145990" ["CEST"]=> string(7) "2108900" ["CFOP"]=> string(4) "5405" ["uCom"]=> string(2) "PC" ["qCom"]=> string(6) "1.0000" ["vUnWith"]=> string(14) "189.0000000000" ["vProd"]=> string(6) "189.00"
– Junior
trial
echo $valor->det->prod->xProd;
, you have to follow the identical nomenclature, you don’t need the foreach in this case, just to test– Wees Smith