foreach array loop does not always work on Windows - php

I encounter this very odd scenario. The PHP is below:
$complete_categories_list=array();
foreach ($modules_library_processed as $key=>$modules_categories_rendered) {
foreach ($modules_categories_rendered['modulecategories'] as $module_category_key=>$module_category_values) {
foreach ((array) $module_category_values as $module_key_inner=>$module_category_value) {
$complete_categories_list[]=trim($module_category_value);
}
}
}
This is working perfectly in Unix/Linux localhost and in some instances will work in Windows localhost. But not all of them. The problem is that at the end of the loop, $complete_categories_list is empty even though $modules_library_processed has some data on it. $modules_library_processed is a very large array, so I cannot post the entire data here. I hope someone can shed light on this.
UPDATE: The source is an XML file
simplexml_load_string($modules_xml_exported); which I converted to an array. The result is $modules_library_processed.
print_r($module_category_values) results to SimpleXMLElement Object ()
But I typecasted it to an array..so that should work right?

I think I have fixed the problem The root cause is the script processing SimpleXML which does not play when the script is run under Windows even though with all the Type casting, etc. I ended up rewriting my scripts so it won't output any SimpleXML element but all in array/string format. I hope this helps someone. Thanks.

Related

How to get an xml element by index

I have an xml file that contains a list of elements. I want to retrieve a specific element by index. I looked around for a while and didn't find anything that worked for me.
I would have thought this would work, but it doesn't:
echo($xml->children()[0]);
I'm not very experienced in php, coming from a C# background, so any help is appreciated.
Array dereferencing was added in PHP 5.4 so your code will (probably) work with that version.
If you're stuck with an older version, simply try this
$children = $xml->children();
echo $children[0];

php xml read access xml object always 0

This code behaves different on my system as on for example codepad and i don't understand why.
http://codepad.org/jBmXRgyY
The output on my system is only 0.
Does anyone know where to look?
found a workaround by myself.
foreach($chstr->channel->children() as $test)
echo (string)$test."\n";
works fine for me.

Accessing XML attributes data

I have two lines of XML data that are attributes but also contain data inside then and they are repeating fields. They are being stored in a SimpleXML variable.
<inputField Type="Name">John Doe</inputField>
<inputField Type="DateOfHire">Tomorrow</inputField>
(Clearly this isnt real data but the syntax is actually in my data and I'm just using string data in them)
Everything that I've seen says to access the data like this, ,which I have tried and it worked perfectly. But my data is dynamic so the data isn't always going to be in the same place, so it doesn't fit my needs.
$xmlFile->inputField[0];
$xmlFile->inputField[1];
This works fine until one of the lines is missing, and I can have anywhere from 0 to 5 lines. So what I was wondering was is there any way that I can access the data by attribute name? So potentially like this.
$xmlFile->inputField['Name'];
or
$xmlFile->inputField->Name;
I use these as examples strictly to illustrate what I'm trying to do, I am aware that neither of the above lines of code are syntactically correct.
Just a note this information is being generated externally so I cannot change the format.
If anyone needs clarification feel free to let me know and would be happy to elaborate.
Maybe like this?
echo $xmlFile->inputField->attributest()->Name;
And what you're using? DOMDocument or simplexml?
You don't say, but I assume you're using SimpleXMLElement?
If you want to access every item, just iterate:
foreach ($xmlFile->inputField as $inputField) { ... }
If you want to access an attribute use array notation:
$inputField['Type']
If you want to access only one specific element, use xpath:
$xmlFile->xpath('inputField[#Type="Name"]');
Perhaps you should read through the basic examples of usage in the SimpleXMLElement documentation?
For example you can a grab a data:
$xmlFile = simplexml_load_file($file);
foreach($xmlFile->inputField as $res) {
echo $res["Name"];
}

Trouble with XML vs JSON data

Using PHP, I'm trying to grab data from the Goodreads API, which returns XML. I've been having a hell of a time trying to figure out how to pull data out of it.
At some point in the adventure, someone suggested I do a json decode( json encode ($blah)) of the whole thing and use JSON instead of XML.
That brings me to my current situation. Everything works as it should, up to the point where I'm pulling data out of the returned array. I probably should have spent more time reading and learning about arrays, but after more than two days of doing every Google search I could think of, I came here.
Here's the entirety of what gets returned by Goodreads:
a:2:{s:7:"Request";a:3:{s:14:"authentication";s:4:"true";s:3:"key";a:0:{}s:6:"method";a:0:{}}s:6:"search";a:7:{s:5:"query";a:0:{}s:13:"results-start";s:1:"1";s:11:"results-end";s:1:"1";s:13:"total-results";s:1:"1";s:6:"source";s:9:"Goodreads";s:18:"query-time-seconds";s:4:"0.06";s:7:"results";a:1:{s:4:"work";a:9:{s:11:"books_count";s:1:"7";s:2:"id";s:7:"5024045";s:24:"original_publication_day";s:2:"16";s:26:"original_publication_month";s:1:"9";s:25:"original_publication_year";s:4:"2008";s:13:"ratings_count";s:3:"227";s:18:"text_reviews_count";s:2:"53";s:14:"average_rating";s:4:"4.33";s:9:"best_book";a:5:{s:2:"id";s:7:"4958245";s:5:"title";s:37:"7 Habits of Happy Kids [With Earbuds]";s:6:"author";a:2:{s:2:"id";s:5:"38343";s:4:"name";s:10:"Sean Covey";}s:9:"image_url";s:56:"http://photo.goodreads.com/books/1343744353m/4958245.jpg";s:15:"small_image_url";s:56:"http://photo.goodreads.com/books/1343744353s/4958245.jpg";}}}}}
What I want from this array is the "id" variable appears under "best_book". For that, I'm using these lines of code:
$goodreads_results = json_decode(json_encode((array) simplexml_load_string($goodreads_data_a)), 1);
$goodreads_id = $goodreads_results->search->results->work->best_book->id;
I should point out that the array I posted (that began "a:2:{s:7") is what's contained in $goodreads_results after the above two lines of code. So I know everything UP TO that point works as it should.
For whatever reason, I'm not getting the ID. The $goodreads_id variable is empty.
Can somebody help me figure out why? Even though I know it's likely something basic, I'm lost, and everything is starting to look the same to me.
Try:
<?php
$goodreads_data_a = 'a:2:{s:7:"Request";a:3:{s:14:"authentication";s:4:"true";s:3:"key";a:0:{}s:6:"method";a:0:{}}s:6:"search";a:7:{s:5:"query";a:0:{}s:13:"results-start";s:1:"1";s:11:"results-end";s:1:"1";s:13:"total-results";s:1:"1";s:6:"source";s:9:"Goodreads";s:18:"query-time-seconds";s:4:"0.06";s:7:"results";a:1:{s:4:"work";a:9:{s:11:"books_count";s:1:"7";s:2:"id";s:7:"5024045";s:24:"original_publication_day";s:2:"16";s:26:"original_publication_month";s:1:"9";s:25:"original_publication_year";s:4:"2008";s:13:"ratings_count";s:3:"227";s:18:"text_reviews_count";s:2:"53";s:14:"average_rating";s:4:"4.33";s:9:"best_book";a:5:{s:2:"id";s:7:"4958245";s:5:"title";s:37:"7 Habits of Happy Kids [With Earbuds]";s:6:"author";a:2:{s:2:"id";s:5:"38343";s:4:"name";s:10:"Sean Covey";}s:9:"image_url";s:56:"http://photo.goodreads.com/books/1343744353m/4958245.jpg";s:15:"small_image_url";s:56:"http://photo.goodreads.com/books/1343744353s/4958245.jpg";}}}}}
';
$goodreads_results = unserialize($goodreads_data_a);
echo $goodreads_id = $goodreads_results['search']['results']['work']['best_book']['id'];
?>

parsing JSON data with PHP....large dataset

below is the dataset I am trying to parse and put into db... I am using this code
$json = json_decode($string);
$json[0]['segments']['layover'] gives access error...
also sizeof($json) returns nothing...
$json doesn't return null,is checked, prints with echo var_dump($json)
I have attempted to foreach through this...doesn't work.. tried an array type and is saying that I am illegal access stdIN object...is there anything as easy as DOMDocument() to walk through json?
---json
[{"legs":[{"segments":[{"layover":{"hours":1,"minutes":28},"totalTravelingTime":{"hours":1,"minutes":10},"departureAirport":{"airportCode":"ABE","airportCity":"Allentown","airportName":"Allentown (ABE)","airportCityState":"Allentown, PA"},"arrivalAirport":{"airportCode":"IAD","airportCity":"Washington","airportName":"Washington (IAD)","airportCityState":"Washington, DC"},"departureDate":1328811900000,"arrivalDate":1328816100000,"flightNumber":"3866","elapsedNumberOfDays":0,"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-05:00","day":9,"second":0,"minute":25,"hour":10,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T10:25:00-05:00"},"segmentLegSequenceIndex":1,"airCarrierCode":"UA","aircraftType":"Saab 340/340B","aircraftCode":"SF3","airline":{"activeState":"ACTIVE","airlineCode":"UA","airlineName":"UNITED","majorAirline":true,"imageFilename":"UA.gif","preferredAirline":false},"bookingCode":"S","cabinClass":"3","seatMapAvailable":true,"segmentSequenceIndex":1,"formattedDepartureTime":"10:25am","formattedArrivalTime":"11:35am","operatedBy":"/UNITED EXPRESS/COLGAN AIR","formattedDepartureDate":"2/9/12","formattedArrivalDate":"2/9/12","operatingAirlineCode":"","wiFiAvailable":false},{"layover":{"hours":1,"minutes":5},"totalTravelingTime":{"hours":3,"minutes":27},"departureAirport":{"airportCode":"IAD","airportCity":"Washington","airportName":"Washington (IAD)","airportCityState":"Washington, DC"},"arrivalAirport":{"airportCode":"DFW","airportCity":"Dallas","airportName":"Dallas (Dallas-Fort Worth Intl.)","airportCityState":"Dallas, TX"},"departureDate":1328821380000,"arrivalDate":1328830200000,"flightNumber":"3801","elapsedNumberOfDays":0,"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-05:00","day":9,"second":0,"minute":3,"hour":13,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T13:03:00-05:00"},"segmentLegSequenceIndex":1,"airCarrierCode":"UA","aircraftType":"Canadian Regional Jet 700","aircraftCode":"CR7","airline":{"activeState":"ACTIVE","airlineCode":"UA","airlineName":"UNITED","majorAirline":true,"imageFilename":"UA.gif","preferredAirline":false},"bookingCode":"S","cabinClass":"3","seatMapAvailable":true,"segmentSequenceIndex":2,"formattedDepartureTime":"1:03pm","formattedArrivalTime":"3:30pm","operatedBy":"/UNITED EXPRESS/MESA AIRLINES","formattedDepartureDate":"2/9/12","formattedArrivalDate":"2/9/12","operatingAirlineCode":"","wiFiAvailable":false},{"totalTravelingTime":{"hours":0,"minutes":50},"departureAirport":{"airportCode":"DFW","airportCity":"Dallas","airportName":"Dallas (Dallas-Fort Worth Intl.)","airportCityState":"Dallas, TX"},"arrivalAirport":{"airportCode":"ABI","airportCity":"Abilene","airportName":"Abilene (ABI)","airportCityState":"Abilene, TX"},"departureDate":1328834100000,"arrivalDate":1328837100000,"flightNumber":"2975","elapsedNumberOfDays":0,"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-06:00","day":9,"second":0,"minute":35,"hour":16,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T16:35:00-06:00"},"segmentLegSequenceIndex":1,"airCarrierCode":"AA","aircraftType":"Embraer RJ145","aircraftCode":"ER4","airline":{"activeState":"ACTIVE","airlineCode":"AA","airlineName":"American Airlines","majorAirline":true,"imageFilename":"AA.gif","preferredAirline":false},"bookingCode":"L","cabinClass":"3","seatMapAvailable":true,"segmentSequenceIndex":3,"formattedDepartureTime":"4:35pm","formattedArrivalTime":"5:25pm","operatedBy":"/AMERICAN EAGLE","formattedDepartureDate":"2/9/12","formattedArrivalDate":"2/9/12","operatingAirlineCode":"","wiFiAvailable":false}],"carriers":["UA","UA","AA"],"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-05:00","day":9,"second":0,"minute":25,"hour":10,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T10:25:00-05:00"},"arrivalDateTime":{"month":2,"year":2012,"timeZone":"GMT-06:00","day":9,"second":0,"minute":25,"hour":17,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T17:25:00-06:00"},"airLineImageFilePath":"","elapsedDays":0,"departureTime":"2012/02/09 10:25:00 PST","arrivalTime":"2012/02/09 17:25:00 PST","departureDate":1328811900000,"arrivalDate":1328837100000,"arrivalAirport":{"airportCode":"ABI","airportCity":"Abilene","airportName":"Abilene (ABI)","airportCityState":"Abilene, TX"},"legSequenceIndex":1,"departureAirport":{"airportCode":"ABE","airportCity":"Allentown","airportName":"Allentown (ABE)","airportCityState":"Allentown, PA"},"departureDateFormat":"2012/02/09","arrivalDateFormat":"Feb. 9","formattedDepartureTime":"10:25am","formattedArrivalTime":"5:25pm","hasTrainLeg":false,"stops":2,"totalTravelingHours":{"hours":8,"minutes":0},"formattedMiles":"1,495","hasBusLeg":false,"formattedDepartureDate":"2/9/12","formattedDepartureDateTime":"2/9/12","departureDateFormat1":"Thu. Feb. 9","formattedArrivalDate":"2/9/12","durationTimeInMins":480,"locationDistanceCount":1495,"locationDistanceUnit":"miles","seatMapAvailableForAnySegment":true,"firstBagFreeAirlines":false,"showOBFeeMessageForLeg":false,"airlineWithBagFeeMsg":false,"airlineWithNoBagFeeMsg":false,"legIndex":1}],"inboundLegsInfo":[{"taxes":43.22,"basePrice":808.01,"taxesAndFees":50.22,"ceilTotalPrice":859.0,"localeFormattedTaxAndFees":"$50","localeFormatBasePrice":"$808","bookingFee":7.0,"t":858.23,"ft":"$858","fab":"$808","fat":"$858","faf":"$50","tripId":30,"s":2,"c":"AA"},{"taxes":43.22,"basePrice":1028.01,"taxesAndFees":50.22,"ceilTotalPrice":1079.0,"localeFormattedTaxAndFees":"$50","localeFormatBasePrice":"$1,028","bookingFee":7.0,"t":1078.23,"ft":"$1,078","fab":"$1,028","fat":"$1,078","faf":"$50","tripId":50,"s":2,"c":"UA"}],"airFareBasisCode":"SA0MN","flightFareType":"PUBLISHED","seatCount":1,"totalPrice":858.23,"fare":{"maxLccPrice":0.0,"minLccPrice":0.0,"totalFlightFare":858.23,"currencyCode":"USD","fees":{"amount":7.00,"currency":"USD","double":7.0,"currencyCode":"USD"},"taxes":{"amount":43.22,"currency":"USD","double":43.22,"currencyCode":"USD"},"baseFare":{"amount":808.01,"currency":"USD","double":808.01,"currencyCode":"USD"},"totalFare":{"amount":858.23,"currency":"USD","double":858.23,"currencyCode":"USD"},"totalBaseFare":808.01,"localeTotalBaseFare":"$808","localeTotalFlightTaxes":"$43","totalFlightTaxes":43.22,"localeTotalFlightFare":"$858","localeTotalFees":"$7","localeTotalFlightTaxesAndFees":"$50","totalFees":7.0,"totalTaxesAndFees":{"amount":50.22,"currency":"USD","double":50.22,"currencyCode":"USD"},"flightFare":{"currencyCode":"USD","basePrice":808.01,"taxes":43.22}},"flightId":"30","showFareDisplayName":false,"fareDisplayName":"Published","firstBagFreeAirline":false,"operatedByFeeDisplayFeatureEnabled":false,"hasFare":true,"localeFormatAvgBasePrice":"$808","localeFormatAvgTotalPrice":"$858","stops":2,"bargainFare":false,"fareName":"published","selectedFlightPara":"30:30:FLTIStr","fareTypeCode":"P","airProviderId":2,"ccfeeAirline":"","displayLCCFareName":false,"noOfTicketsLeft":7,"seatPreview":true,"localeFormatAvgTotalTaxesAndFees":"$50","priceIllegal":false,"fareTypeValue":"1","showOBFeeMessageForModule":false,"completeTrip":false,"hasAirlineWithBagFee":false,"hasAirlineWithNoBagFee":false,"isABestTrip":true,"carrierList":["AA"],"isFirstBagFreePos":true},{"legs":[{"segments":[{"layover":{"hours":1,"minutes":41},"totalTravelingTime":{"hours":2,"minutes":22},"departureAirport":{"airportCode":"ABE","airportCity":"Allentown","airportName":"Allentown (ABE)","airportCityState":"Allentown, PA"},"arrivalAirport":{"airportCode":"ATL","airportCity":"Atlanta","airportName":"Atlanta (Hartsfield-Jackson Atlanta Intl.)","airportCityState":"Atlanta, GA"},"departureDate":1328818020000,"arrivalDate":1328826540000,"flightNumber":"4355","elapsedNumberOfDays":0,"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-05:00","day":9,"second":0,"minute":7,"hour":12,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T12:07:00-05:00"},"segmentLegSequenceIndex":1,"airCarrierCode":"DL","aircraftType":"Canadair RJ","aircraftCode":"CRJ","airline":{"activeState":"ACTIVE","airlineCode":"DL","airlineName":"Delta","majorAirline":true,"imageFilename":"DL.gif","preferredAirline":false},"bookingCode":"Q","cabinClass":"3","seatMapAvailable":true,"segmentSequenceIndex":1,"formattedDepartureTime":"12:07pm","formattedArrivalTime":"2:29pm","operatedBy":"/PINNACLE DBA DELTA CONNECTION","formattedDepartureDate":"2/9/12","formattedArrivalDate":"2/9/12","operatingAirlineCode":"9E","wiFiAvailable":false},{"layover":{"hours":1,"minutes":5},"totalTravelingTime":{"hours":2,"minutes":30},"departureAirport":{"airportCode":"ATL","airportCity":"Atlanta","airportName":"Atlanta (Hartsfield-Jackson Atlanta Intl.)","airportCityState":"Atlanta, GA"},"arrivalAirport":{"airportCode":"DFW","airportCity":"Dallas","airportName":"Dallas (Dallas-Fort Worth Intl.)","airportCityState":"Dallas, TX"},"departureDate":1328832600000,"arrivalDate":1328838000000,"flightNumber":"1810","elapsedNumberOfDays":0,"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-05:00","day":9,"second":0,"minute":10,"hour":16,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T16:10:00-05:00"},"segmentLegSequenceIndex":1,"airCarrierCode":"DL","aircraftType":"Boeing 757-200","aircraftCode":"752","airline":{"activeState":"ACTIVE","airlineCode":"DL","airlineName":"Delta","majorAirline":true,"imageFilename":"DL.gif","preferredAirline":false},"bookingCode":"Q","cabinClass":"3","seatMapAvailable":true,"segmentSequenceIndex":2,"formattedDepartureTime":"4:10pm","formattedArrivalTime":"5:40pm","operatedBy":"","formattedDepartureDate":"2/9/12","formattedArrivalDate":"2/9/12","operatingAirlineCode":"","wiFiAvailable":false},{"totalTravelingTime":{"hours":0,"minutes":50},"departureAirport":{"airportCode":"DFW","airportCity":"Dallas","airportName":"Dallas (Dallas-Fort Worth Intl.)","airportCityState":"Dallas, TX"},"arrivalAirport":{"airportCode":"ABI","airportCity":"Abilene","airportName":"Abilene (ABI)","airportCityState":"Abilene, TX"},"departureDate":1328841900000,"arrivalDate":1328844900000,"flightNumber":"2773","elapsedNumberOfDays":0,"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-06:00","day":9,"second":0,"minute":45,"hour":18,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T18:45:00-06:00"},"segmentLegSequenceIndex":1,"airCarrierCode":"AA","aircraftType":"Embraer EMB-140","aircraftCode":"ERD","airline":{"activeState":"ACTIVE","airlineCode":"AA","airlineName":"American Airlines","majorAirline":true,"imageFilename":"AA.gif","preferredAirline":false},"bookingCode":"L","cabinClass":"3","seatMapAvailable":true,"segmentSequenceIndex":3,"formattedDepartureTime":"6:45pm","formattedArrivalTime":"7:35pm","operatedBy":"/AMERICAN EAGLE","formattedDepartureDate":"2/9/12","formattedArrivalDate":"2/9/12","operatingAirlineCode":"","wiFiAvailable":false}],"carriers":["DL","DL","AA"],"departureDateTime":{"month":2,"year":2012,"timeZone":"GMT-05:00","day":9,"second":0,"minute":7,"hour":12,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T12:07:00-05:00"},"arrivalDateTime":{"month":2,"year":2012,"timeZone":"GMT-06:00","day":9,"second":0,"minute":35,"hour":19,"milliSecond":0,"xsdTimeType":"DATETIME_WITH_TIMEZONE","dateTimeString":"2012-02-09T19:35:00-06:00"},"airLineImageFilePath":"","elapsedDays":0,"departureTime":"2012/02/09 12:07:00 PST","arrivalTime":"2012/02/09 19:35:00 PST","departureDate":1328818020000,"arrivalDate":1328844900000,"arrivalAirport":{"airportCode":"ABI","airportCity":"Abilene","airportName":"Abilene (ABI)","airportCityState":"Abilene, TX"},"legSequenceIndex":10,"departureAirport":{"airportCode":"ABE","airportCity":"Allentown","airportName":"Allentown (ABE)","airportCityState":"Allentown, PA"},"departureDateFormat":"2012/02/09","arrivalDateFormat":"Feb. 9","formattedDepartureTime":"12:07pm","formattedArrivalTime":"7:35pm","hasTrainLeg":false,"stops":2,"totalTravelingHours":{"hours":8,"minutes":28},"formattedMiles":"1,581","hasBusLeg":false,"formattedDepartureDate":"2/9/12","formattedDepartureDateTime":"2/9/12","departureDateFormat1":"Thu. Feb. 9","formattedArrivalDate":"2/9/12","durationTimeInMins":508,"locationDistanceCount":1581,"locationDistanceUnit":"miles","seatMapAvailableForAnySegment":true,"firstBagFreeAirlines":false,"showOBFeeMessageForLeg":false,"airlineWithBagFeeMsg":false,"airlineWithNoBagFeeMsg":false,"legIndex":1}],"inboundLegsInfo":[{"taxes":43.22,"basePrice":808.01,"taxesAndFees":50.22,"ceilTotalPrice":859.0,"localeFormattedTaxAndFees":"$50","localeFormatBasePrice":"$808","bookingFee":7.0,"t":858.23,"ft":"$858","fab":"$808","fat":"$858","faf":"$50","tripId":39,"s":2,"c":"DL"},{"taxes":43.22,"basePrice":808.01,"taxesAndFees":50.22,"ceilTotalPrice":859.0,"localeFormattedTaxAndFees":"$50","localeFormatBasePrice":"$808","bookingFee":7.0,"t":858.23,"ft":"$858","fab":"$808","fat":"$858","faf":"$50","tripId":39,"s":2,"c":"AA"}],"airFareBasisCode":"QA00A0NJ","flightFareType":"PUBLISHED","seatCount":1,"totalPrice":858.23,"fare":{"maxLccPrice":0.0,"minLccPrice":0.0,"totalFlightFare":858.23,"currencyCode":"USD","fees":{"amount":7.00,"currency":"USD","double":7.0,"currencyCode":"USD"},"taxes":{"amount":43.22,"currency":"USD","double":43.22,"currencyCode":"USD"},"baseFare":{"amount":808.01,"currency":"USD","double":808.01,"currencyCode":"USD"},"totalFare":{"amount":858.23,"currency":"USD","double":858.23,"currencyCode":"USD"},"totalBaseFare":808.01,"localeTotalBaseFare":"$808","localeTotalFlightTaxes":"$43","totalFlightTaxes":43.22,"localeTotalFlightFare":"$858","localeTotalFees":"$7","localeTotalFlightTaxesAndFees":"$50","totalFees":7.0,"totalTaxesAndFees":{"amount":50.22,"currency":"USD","double":50.22,"currencyCode":"USD"},"flightFare":{"currencyCode":"USD","basePrice":808.01,"taxes":43.22}},"flightId":"39","showFareDisplayName":false,"fareDisplayName":"Published","firstBagFreeAirline":false,"operatedByFeeDisplayFeatureEnabled":false,"hasFare":true,"localeFormatAvgBasePrice":"$808","localeFormatAvgTotalPrice":"$858","stops":2,"bargainFare":false,"fareName":"published","selectedFlightPara":"39:39:FLTIStr","fareTypeCode":"P","airProviderId":2,"ccfeeAirline":"","displayLCCFareName":false,"noOfTicketsLeft":1,"seatPreview":true,"localeFormatAvgTotalTaxesAndFees":"$50","priceIllegal":false,"fareTypeValue":"1","showOBFeeMessageForModule":false,"completeTrip":false,"hasAirlineWithBagFee":false,"hasAirlineWithNoBagFee":false,"isABestTrip":true,"carrierList":["DL","AA"],"isFirstBagFreePos":true},
...so on
json_decode takes a second parameter to return data as an associative array instead of objects. This will solve your problem.
$json_array = json_decode($string,true); // returns 'normal' php array.
$json = json_decode($string); // returns php object hierarchy
http://json.parser.online.fr/ reports that you data ends unexpectedly. Do you actually have the real data? Or if is it wrong, how did you generate it? There might be errors in the generation script cause it is not complete...
There seems to be a misplaced character at the end. Instead of a comma ",", it should be a closing array "]"
You did not say WHY json_decode isn't working. If the JSON is too big to post here just use pastebin or something alike.
I assume the reason it's not working is because you are exceeding the memory allowance for PHP, you can work around this by increasing the max memory PHP can use, eg:
ini_set('memory_limit','16M');
However I wouldn't recommend doing this if this code is going to be ran by a large amount of users. Maybe if you give us some context we can give you an alternative solution which bypasses the need of json_decode.

Categories