user3844830
user3844830

Reputation: 44

Multidimensional array loop not displaying the result for objects

I am getting the result from the SOAP client as an response. I just have to analyze the structure of the parameter and display the results accordingly. I have the following srucutre for SerialEquipment and i am getting the results peoperly for all the parameters except the Esaco parameter. The Esaco parameter is an array object and it resides inside the SerialEquipment array. I am trying to fetch the response from Esaco array object but getting an error as Invalid arguments supplied for foreach. I am not understanding how to get the results for Esaco Parameter by looping properly.Just a small mistake i am doing in looping the array.

Code:

foreach($Serial as $key => $obj)
   {
        echo "<b>"."Serial Equipment=>" . $key . "</b>"."<br>";
        echo "Code=>". $obj->Code . "<br>";
        echo "Desc Short=>". $obj->Desc_Short . "<br>";
        echo "Desc Long=>". $obj->Desc_Long . "<br>";


        foreach($obj->Esaco as $key2 => $obj2)  
        {  
            if($obj2 === null){
        // doesn't contain Esaco
        break;
            }
            else{
              echo "<b>"."Esaco=>" . $key2 . "</b>"."<br>";                 
            echo "EsacoMainGroupCode=>". $obj2->EsacoMainGroupCode . "<br>";
            echo "EsacoMainGroupDesc=>". $obj2->EsacoMainGroupDesc . "<br>";
            echo "EsacoSubGroupCode=>".  $obj2->EsacoSubGroupCode . "<br>";
            echo "EsacoSubGroupCode=>".  $obj2->EsacoSubGroupDesc . "<br>";
            echo "EsacoSubGroupCode=>".  $obj2->EsacoGroupCode . "<br>";
            echo "EsacoSubGroupCode=>".  $obj2->EsacoGroupDesc . "<br>";  
        }       
        }           
     }      

if($parameter['aktion'] == 'getVehicle') 
{   
     $vehicle = getVehicleValuation();
     $Serial=$vehicle['SerialEquipment'];        
     $VehicleFuel=$vehicle['VehicleFuel'];

        foreach($VehicleFuel as $key => $obj2)
        {           
            echo "Fuel Type=>". $obj2->Fuel_Type . "<br>";
            echo "Fuel Type Code=>". $obj2->Fuel_Type_Code . "<br>";
            echo "ECE_Unit=>". $obj2->ECE_Unit . "<br>"; 
            echo "ECE_In=>". $obj2->ECE_In . "<br>";     
            echo "ECE_Out=>". $obj2->ECE_Out . "<br>";
            echo "ECE_All=>". $obj2->ECE_All . "<br>";
            echo "ECE_CO2=>". $obj2->ECE_CO2 . "<br>";                       
        }   
}

This is my structure for SerialEquipment: enter image description here

Upvotes: 0

Views: 48

Answers (2)

Alex
Alex

Reputation: 1573

if($parameter['aktion'] == 'getVehicle') 
{
    $vehicle = getVehicleValuation();
    if(($serials = $vehicle['SerialEquipment']) === null){
        // doesn't contain SerialEquipment
        break;
    }

    foreach($serials as $serial){
        print "Code =>" . $serial->Code . "<br>";
        print "Desc Short =>" . $serial->Desc_Short . "<br>";

        //...

        foreach($serial->Esaco as $esaco){
            print "EsacoMainGroupCode =>" . $esaco->EsacoMainGroupCode. "<br>";
            print "EsacoMainGroupDesc =>" . $esaco->EsacoMainGroupDesc. "<br>";

            //...
        }
    }
}  

And for the VehicleFuel:

if($parameter['aktion'] == 'getVehicle') 
{   
 $vehicle = getVehicleValuation();
 $Serial=$vehicle['SerialEquipment'];        
 $VehicleFuel=$vehicle['VehicleFuel'];

 $fuelType = $VehicleFuel->Fuel_Type;

 // if there is only going to be one VehicleFuel object Vehicle, then just do..
 echo "Fuel Type =>". fuelType->Fuel_Type . "<br>";
 echo "Fuel Type Code =>". $fuelType->Fuel_Type_Code . "<br>";

 // if there will be more than one, you will want to use a loop...

    foreach($fuelType as $obj)
    {           
        echo "Fuel Type=>". $obj->Fuel_Type . "<br>";
        echo "Fuel Type Code=>". $obj->Fuel_Type_Code . "<br>";
        echo "ECE_Unit=>". $obj->ECE_Unit . "<br>"; 
        echo "ECE_In=>". $obj->ECE_In . "<br>";     
        echo "ECE_Out=>". $obj->ECE_Out . "<br>";
        echo "ECE_All=>". $obj->ECE_All . "<br>";
        echo "ECE_CO2=>". $obj->ECE_CO2 . "<br>";                       
    }   
} 

Upvotes: 1

Debflav
Debflav

Reputation: 1151

The key Esaco is an object not an array. You should change your second foreach.

foreach($Serial as $key => $obj) {       
    echo "Serial Equipment=>" . $key . "<br>";
    echo "Code=>". $obj->Code . "<br>";
    echo "Desc Short=>". $obj->Desc_Short . "<br>";
    echo "Desc Long=>". $obj->Desc_Long . "<br>";  
    foreach($obj->Esaco as $key2 => $obj2) {
        echo $obj2;
        //...
    }           
}    

Upvotes: 1

Related Questions