I am attempting to fetch a JSON file from a separate website, read that file and then decode it into a multidimensional array. So far at that point it seems to work. However, when I try and break it all down into variables to echo, it goes wrong when its 4 levels deep. You can see the actual JSON file from the link in the PHP code and below is the PHP Code I am using. The specific variables that have problems are $prices in 2 areas and $rulings. Any help someone can provide with this would be appreciated.
<?php
$path = 'https://www.mtgjson.com/json/LEA.json';
$FileContents = file_get_contents($path);
$cards = json_decode($FileContents, true);
foreach ($cards['cards'] as $mtgcard){
$Artist = $mtgcard['artist'];
$borderColor = $mtgcard['borderColor'];
$colorIdentity = $mtgcard['colorIdentity'][0];
$Colors = $mtgcard['colors'][0];
$convertedManaCost = $mtgcard['convertedManaCost'];
$edhrecRank = $mtgcard['edhrecRank'];
$flavorText = $mtgcard['flavorText'];
$cards_foreignData = implode($mtgcard['foreignData'],",");
$foreignData = explode(',', $cards_foreignData);
$frameVersion = $mtgcard['frameVersion'];
$hasFoil = $mtgcard['hasFoil'];
$hasNonFoil = $mtgcard['hasNonFoil'];
$isPaper = $mtgcard['isPaper'];
$Layout = $mtgcard['layout'];
$cards_legalities = implode($mtgcard['legalities'],",");
$legalities = explode(',', $cards_legalities);
$manaCost = $mtgcard['manaCost'];
$mcmId = $mtgcard['mcmId'];
$mcmMetaId = $mtgcard['mcmMetaId'];
$mtgstocksId = $mtgcard['mtgstocksId'];
$multiverseId = $mtgcard['multiverseId'];
$name = $mtgcard['name'];
$number = $mtgcard['number'];
$originalText = $mtgcard['originalText'];
$originalType = $mtgcard['originalType'];
$power = $mtgcard['power'];
$cards_prices = implode($mtgcard['prices'],",");
$prices = explode(',', $cards_prices);
$cards_prices_2 = implode($cards_prices['paper'],",");
$prices_2 = explode(',', $cards_prices_2);
$cards_printings = implode($mtgcard['printings'],",");
$printings = explode(',', $cards_printings);
$cards_purchaseUrls = implode($mtgcard['purchaseUrls'],",");
$purchaseUrls = explode(',', $cards_purchaseUrls);
$rarity = $mtgcard['rarity'];
$cards_rulings = implode($mtgcard['rulings'],",");
$rulings = explode(',', $cards_rulings);
$scryfallId = $mtgcard['scryfallId'];
$scryfallIllustrationId = $mtgcard['scryfallIllustrationId'];
$scryfallOracleId = $mtgcard['scryfallOracleId'];
$cards_subtypes = implode($mtgcard['subtypes'],",");
$subtypes = explode(',', $cards_subtypes);
$cards_supertypes = implode($mtgcard['supertypes'],",");
$supertypes = explode(',', $cards_supertypes);
$tcgplayerProductId = $mtgcard['tcgplayerProductId'];
$text = $mtgcard['text'];
$toughness = $mtgcard['toughness'];
$type = $mtgcard['type'];
$types = $mtgcard['types'][0];
$uuid = $mtgcard['uuid'];
$a = substr($scryfallId, 0, 1);
$b = substr($scryfallId, 1, 1);
$convert = "<img src='https://img.scryfall.com/cards/large/front/" .$a. "/" .$b. "/" .$scryfallId. ".jpg' />";
echo $convert;
echo "Artist Name: " .$Artist. "<br/>";
echo "Border Color: " .$borderColor. "<br/>";
echo "colorIdentity: " .$colorIdentity. "<br/>";
echo "Colors: " .$Colors. "<br/>";
echo "convertedManaCost: " .$convertedManaCost. "<br/>";
echo "edhrecRank: " .$edhrecRank. "<br/>";
echo "flavorText: " .$flavorText. "<br/>";
echo "foreignData: " .$foreignData[0]. "<br/>";
echo "frameVersion: " .$frameVersion. "<br/>";
echo "hasFoil: " .$hasFoil. "<br/>";
echo "hasNonFoil: " .$hasNonFoil. "<br/>";
echo "isPaper: " .$isPaper. "<br/>";
echo "Layout: " .$Layout. "<br/>";
echo "Brawl: " .$legalities[0]. "<br/>";
echo "Commander: " .$legalities[1]. "<br/>";
echo "Duel: " .$legalities[2]. "<br/>";
echo "Future: " .$legalities[3]. "<br/>";
echo "Historic: " .$legalities[4]. "<br/>";
echo "Legacy: " .$legalities[5]. "<br/>";
echo "Modern: " .$legalities[6]. "<br/>";
echo "Oldschool: " .$legalities[7]. "<br/>";
echo "Penny: " .$legalities[8]. "<br/>";
echo "Pioneer: " .$legalities[9]. "<br/>";
echo "Standard: " .$legalities[10]. "<br/>";
echo "Vintage: " .$legalities[11]. "<br/>";
echo "Manacost: " .$manaCost. "<br/>";
echo "mcmId: " .$mcmId. "<br/>";
echo "mcmMetaId: " .$mcmMetaId. "<br/>";
echo "mtgstocksId: " .$mtgstocksId. "<br/>";
echo "multiverseId: " .$multiverseId. "<br/>";
echo "name: " .$name. "<br/>";
echo "number: " .$number. "<br/>";
echo "originaltext: " .$originalText. "<br/>";
echo "originaltype: " .$originalType. "<br/>";
echo "power: " .$power. "<br/>";
echo "MTGO prices: " .$prices[0]. "<br/>";
echo "MTGO Foil Prices: " .$prices[1]. "<br/>";
echo "Paper Prices: " .prices[2] "<br/>"; <- 4 levels deep doesnt work
echo "Paper Foil Prices: " .$prices[3]. "<br/>"; <- 4 levels deep doesnt work
echo "purchaseUrls: " .$purchaseUrls[0]. "<br/>";
echo "purchaseUrls: " .$purchaseUrls[1]. "<br/>";
echo "purchaseUrls: " .$purchaseUrls[2]. "<br/>";
echo "rarity: " .$rarity. "<br/>";
echo "ruling: " .$rulings['date'].$rulings['text'] "<br/>"; <-- 4 levels deep doesnt work
echo "scryfallId: " .$scryfallId. "<br/>";
echo "scryfallIllustrationId: " .$scryfallIllustrationId. "<br/>";
echo "scryfallOracleId: " .$scryfallOracleId. "<br/>";
echo "subtypes: " .$subtypes[0]. "<br/>";
echo "supertypes: " .$supertypes[0]. "<br/>";
echo "tcgplayerProductId: " .$tcgplayerProductId. "<br/>";
echo "text: " .$text. "<br/>";
echo "toughness: " .$toughness. "<br/>";
echo "type: " .$type. "<br/>";
echo "types: " .$types. "<br/>";
echo "uuid: " .$uuid. "<br/>";
}
?>
You should mention the array name to fetch the content inside array, here is the example code and artist is with small "a" not capital
Edit as per comment request
Get the array of 'legalities' a string with , separated using implode and than explode with , and convert into an array to print those
Note: only the values will be imported using implode not the keys
<?php
// error_reporting(0);
// header('Content-type: application/json');
$path = 'https://www.mtgjson.com/json/LEA.json';
$FileContents = file_get_contents($path);
$cards = json_decode($FileContents, true);
foreach($cards['cards'] as $mtgcard) {
echo '<br>' .$course_data1 = $mtgcard['artist'];
$cards_legalities = implode($mtgcard['legalities'],",");
$myArray = explode(',', $cards_legalities);
echo $myArray[0];// 1st array
//Multidimensional Array for your updated question
$cards_prices_paper = implode($mtgcard['prices']['paper'],",");
$prices_paper_array = explode(',', $cards_prices_paper);
echo $prices_paper_array[0];
$rulings_date = implode(', ', array_column($mtgcard['rulings'], 'date'));
$rulings_date_array = explode(",", $rulings_date);
echo $rulings_date_array[0];
}
?>
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;
//...
}
}
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++;
}
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;
}
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>';
}
?>