Forum Moderators: coopster

Message Too Old, No Replies

How would I parse this particular array?

         

JS_Harris

10:53 pm on Aug 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's from the eBay api ItemSpecifics data returned in GetSingleItem calls.

Here is a sample of one of the lines in the array (Using JSON)

[10]=> object(stdClass)#15 (2) { ["Name"]=> string(8) "Warranty" ["Value"]=> array(1) { [0]=> string(42) "Vehicle does NOT have an existing warranty" } }

About that array: Value is easy to deal with because [0] always returns the Value's value. Name on the other hand is frustrating me. In this example [10] is the array object for Warranty however on a different auction [10] may mean Mileage or Body type so I can't specify a number in the array and always get the same value.

Best effort, this works to display a list of all names and values in the array

foreach($resp->Item->ItemSpecifics->NameValueList as $itemspec) {
$specific_name = $itemspec->Name;
$specific_value = $itemspec->Value[0];
$results .= "$specific_name: $specific_value<br />";
}

I can grab the specific name I want like this

if ($specific_name='Mileage') {
echo WHAT GOES HERE TO GRAB SPECIFIC MILEAGE VALUE;}

and as you can see I'm just not seeing how to extract the value for a specific name. It seems simple but remember that you cannot use the [10] because it does not stay constant across all auctions and you cannot use Name because every line in the array uses it.

Any ideas?

eelixduppy

2:47 am on Aug 5, 2009 (gmt 0)



Loop through the array checking to see if the "Name" matches the one you want, then display the value. Here's pseudocode for a function:

foreach($array AS $part)
{
if($part['name'] == $specific_name)
return $part['value'];
}
/* if we can't find it return null */
return NULL;

See what you can do.

JS_Harris

3:29 am on Aug 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks eelixduppy. I've tried what you suggest but the problem I keep running into is that 'Name' IS the object and "Warranty" for example isn't an object.

Using

foreach($resp->Item->ItemSpecifics->NameValueList as $itemspec) {
$specific_name = $itemspec->Name;
$specific_value = $itemspec->Value[0];
if ($specific_name == "Mileage")
$results .= "$specific_name: $specific_value<br />";
}

I am able to extract the data I want so my problem is solved BUT there has to be a more efficient way of handling the data.