Multidimensional array loop not displaying the result for objects - php

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:

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>";
}
}

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;
//...
}
}

Related

Need help parsing JSON decoded grandchildren with PHP

foreach($response_2['items'] as $item) {
$item['track']['name'] = (array)$item['track']['name'];
foreach($item['track']['name'] as $key => $value) {
echo $value . "<br />";
}
$item['track']['artists']['name'] = (array)$item['track']['artists']['name'];
foreach($item['track']['artists']['name'] as $key => $value) {
echo $value . "<br />";
}
$item['track']['duration_ms'] = (array)$item['track']['duration_ms'];
foreach($item['track']['duration_ms'] as $key => $value) {
echo $value . "<br />";
}
}
...to parse this response: http://pastebin.com/uxBF3Kxe# (after I've decoded it).
All the values for ['track']['name'] and ['track']['duration_ms'] are echoed correctly, but nothing is echoed for ['track']['artists']['name'].
What am I doing wrong here?
Thanks!
['track']['artists'] is array :)
Try this ;)
foreach($item['track']['artists'] as $key => $value) {
echo $value['name'] . "<br />";
}
Btw...
This is better solution ;)
$response_2 = json_decode($json, true);
foreach($response_2['items'] as $item) {
echo $item['track']['name'] . "<br />";
foreach($item['track']['artists'] as $key => $value) {
echo $value['name'] . "<br />";
}
echo $item['track']['duration_ms'] . "<br />";
}
/** Added separator */
$response_2 = json_decode($json, true);
foreach($response_2['items'] as $item) {
echo $item['track']['name'] . "<br />";
echo implode(',', array_map(
function ($value) {
return $value['name'];
},
$item['track']['artists']
)
);
echo "<br />";
echo $item['track']['duration_ms'] . "<br />";
}

php json not showing Wunderground station list

I'm trying to show a list of all nearby weather stations.
I have the code:
$json_string = file_get_contents("http://api.wunderground.com/api/8b19ccf6a06c0826/geolookup/conditions/q/Netherlands/Rotterdam.json");
$parsed_json = json_decode($json_string);
$stations = $parsed_json->{'location'}->{'nearby_weather_stations'}->{'pws'}->{'station'};
$count = count($stations);
for($i = 0; $i < $count; $i++)
{
$station = $stations[$i];
if (count($station) > 1)
{
echo "City: " . $station->{'city'} . "\n";
echo "State: " . $station->{'state'} . "\n";
echo "Latitude: " . $station->{'lat'} . "\n";
echo "Longitude: " . $station->{'lon'} . "\n";
}
}
But currently it's not showing anything, i have searched for examples but i couldn't find any solution fot this problem.
Alternatively, you could use a simple foreach to iterate those values. Consider this example:
$json_string = file_get_contents("http://api.wunderground.com/api/8b19ccf6a06c0826/geolookup/conditions/q/Netherlands/Rotterdam.json");
$parsed_json = json_decode($json_string, true); // <-- second parameter to TRUE to use it as an array
$desired_values = $parsed_json['location']['nearby_weather_stations']['pws']['station'];
foreach($desired_values as $key => $value) {
echo "<hr/>";
echo "City: " . $value['city'] . "<br/>";
echo "State: " . $value['state'] . "<br/>";
echo "Latitude: " . $value['lat'] . "<br/>";
echo "Longitude: " . $value['lon'] . "<br/>";
echo "<hr/>";
}
Sample Fiddle

Php Code (Get/Fetch Kongregate Games)

Can you help me there is a problem that is detect by php when execute but I think my code is okay.
The error say "Catchable fatal error: Object of class stdClass could not be converted to string in C:\Program Files\wamp\www\getkongregate.php on line 46"
Website of kongregate xml: here
This is the code I have:
<?php
$xml = simplexml_load_string(file_get_contents('http://www.kongregate.com/games_for_your_site.xml'));
$json = json_encode($xml);
$games = json_decode($json);
$count = 0;
foreach ($games->game as $game) {
echo $game->id . "\n";
echo $game->title . "\n";
echo $game->thumbnail . "\n";
echo $game->launch_date . "\n";
echo $game->category . "\n";
if (array_key_exists('screenshot',$game)) {
for($i=0;$i<=(count($game->screenshot)-1);$i++) {
echo $game->screenshot[$i] . "\n";
}
}
echo $game->flash_file . "\n";
echo $game->width . "\n";
echo $game->height . "\n";
echo $game->description . "\n";
echo $game->instructions . "\n";
echo $game->gameplays . "\n";
echo $game->rating . "\n\n------\n\n";
if ($count == 100) {break;}
$count++;
}
?>
You are retreiving xml.
Why are you parsing it as json?
Use SimpleXmlElement instead and then iterate through xml elements to retreive your data.
$xml = file_get_contents($map_url);
$simpleXmlElement = simplexml_load_string($xml);
var_dump($simpleXmlElement);
Here: :)
$url = 'http://www.kongregate.com/games_for_your_site.xml';
$content = file_get_contents($url)
$xml = simplexml_load_string($content);
$count = 0;
foreach ($xml as $game) {
echo strval($game->id) . PHP_EOL;
echo strval($game->title) . PHP_EOL;
echo strval($game->thumbnail) . PHP_EOL;
echo strval($game->launch_date) . PHP_EOL;
echo strval($game->category) . PHP_EOL;
if (isset($game->screenshot) && is_array($game->screenshot)) {
foreach ($game->screenshot as $screenshot) {
echo strval($screenshot) . "\n";
}
}
echo strval($game->flash_file) . PHP_EOL;
echo strval($game->width) . PHP_EOL;
echo strval($game->height) . PHP_EOL;
echo strval($game->description) . PHP_EOL;
echo strval($game->instructions) . PHP_EOL;
echo strval($game->gameplays) . PHP_EOL;
echo strval($game->rating) . PHP_EOL;
if ($count >= 100) {
break;
}
$count++;
}

displaying multidimensional array

How do you display the output of a 2 dimensional arrays
Here's the code:
I know how to display both of them separately doing.
foreach($Father as $value){
echo $value[0] . " " . $value[1] . "<br />";
}
foreach($Son as $value){
echo $value[0] . " " . $value[1] . "<br />";
}
try this - it assumes that each father has exactly 1 son though
$i = 0;
foreach($Father as $value){
echo $value[0] . " " . $value[1] . "<br />";
echo "-" . $Son[$i][0] . " " . $Son[$i][1] . "<br />";
$i += 1;
}

PHP function, returning (in Windows) Rx and Tx for each adapter?

This is supposed to return raw values for Rx and Tx for each adapter (output is in pairs) ONLY of the adapter has transferred more than zero data.
<?php
//windows network usage testing
function win_netinfo(){
ob_start();
$wmi = new COM("Winmgmts://");
$nets = $wmi->execquery("SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface WHERE BytesSentPersec > 1");
foreach ($nets as $net)
{
$net_txbytes = $net->BytesSentPersec;
$net_rxbytes = $net->BytesReceivedPersec;
if ($net_txbytes < 0) {
$net_txbytes = $net->BytesTotalPersec - $net->BytesReceivedPersec;
}
if ($net_rxbytes < 0) {
$net_rxbytes = $net->BytesTotalPersec - $net->BytesSentPersec;
}
return $net_txbytes . "</br>"; //When it RETURNS it only shows the FIRST value... :/ WHAT DO?
}
}
echo win_netinfo();
?>
Output should be like this:
21936313136
12345163517
13647613
87653467
546254
246247
87653642
24583462
(Every other line is Rx or Tx, for each adapter).
Current code that works:
<?php
# Peport All Errors
error_reporting(E_ALL);
# Create Object
$wmi = new COM("Winmgmts://");
# Get net info
$oss = $wmi->execquery("SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface");
# Show net info
foreach($oss as $os)
{
echo "BytesReceivedPerSec: " . $os->BytesReceivedPerSec . "<br />";
echo "BytesSentPerSec: " . $os->BytesSentPerSec . "<br />";
echo "BytesTotalPerSec: " . $os->BytesTotalPerSec . "<br />";
echo "Caption: " . $os->Caption . "<br />";
echo "CurrentBandwidth: " . $os->CurrentBandwidth . "<br />";
echo "Description: " . $os->Description . "<br />";
echo "Frequency_Object: " . $os->Frequency_Object . " kb<br />";
echo "Frequency_PerfTime: " . $os->Frequency_PerfTime . "<br />";
echo "Frequency_Sys100NS: " . $os->Frequency_Sys100NS . "<br />";
echo "Name: " . $os->Name . "<br />";
echo "OutputQueueLength: " . $os->OutputQueueLength . "<br />";
echo "PacketsOutboundDiscarded: " . $os->PacketsOutboundDiscarded . "<br />";
echo "PacketsOutboundErrors: " . $os->PacketsOutboundErrors . "<br />";
echo "PacketsReceivedNonUnicastPerSec: " . $os->PacketsReceivedNonUnicastPerSec . "<br />";
echo "PacketsReceivedPerSec: " . $os->PacketsReceivedPerSec . "<br />";
echo "PacketsReceivedUnicastPerSec: " . $os->PacketsReceivedUnicastPerSec . "<br />";
echo "PacketsReceivedUnknown: " . $os->PacketsReceivedUnknown . "<br />";
echo "PacketsSentNonUnicastPerSec: " . $os->PacketsSentNonUnicastPerSec . "<br />";
echo "PacketsSentPerSec: " . $os->PacketsSentPerSec . "<br />";
echo "PacketsSentUnicastPerSec: " . $os->PacketsSentUnicastPerSec . "<br />";
echo "Timestamp_Object: " . $os->Timestamp_Object . "<br />";
echo "Timestamp_PerfTime: " . $os->Timestamp_PerfTime . "<br />";
echo "Timestamp_Sys100NS: " . $os->Timestamp_Sys100NS . "<br />";
echo "<br /><br /><br />";
}
?>
Your returning after the first iteration hence only 1 result, below is how i would do it by creating an array and returning that instead:
<?php
//windows network usage testing
function win_netinfo(){
$wmi = new COM("Winmgmts://");
$nets = $wmi->execquery("SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface WHERE BytesSentPersec > 1");
$return=array();
$i=0;
foreach ($nets as $net){
$return[$i]['name']=$net->Name;
if ($net->BytesSentPersec < 0) {
$return[$i]['net_txbytes'] = $net->BytesTotalPersec - $net->BytesReceivedPersec;
}else{
$return[$i]['net_txbytes'] = $net->BytesSentPersec;
}
if ($net->BytesReceivedPersec < 0) {
$return[$i]['net_rxbytes'] = $net->BytesTotalPersec - $net->BytesSentPersec;
}else{
$return[$i]['net_rxbytes'] = $net->BytesReceivedPersec;
}
$i++;
}
return $return;
}
$adapters=win_netinfo();
/*Array
Array
(
[0] => Array
(
[name] => VirtualBox Host-Only Ethernet Adapter
[net_txbytes] => 5095956
[net_rxbytes] => 0
)
[1] => Array
(
[name] => Belkin 54g Wireless USB Network Adapter
[net_txbytes] => 532337967
[net_rxbytes] => 5252455518
)
)
*/
?>
And to output the array, something like this:
<?php
foreach($adapters as $adapter){
echo '<p>'.$adapter['name'].' - Sent: '.$adapter['net_txbytes'].' bytes - Received: '.$adapter['net_rxbytes'].' bytes</p>';
}
?>

Categories