Array - How to retrieve value - php

Example 1:
$string = stdClass Object ( [product_id] => 25 )
$result= $string->product_id
$result will get 25
Example 2:
$string = Array ( [30] => stdClass Object ( [product_id] => 44 ))
$result= $string[30]->product_id
$result will get 44
How about this one? I want to retrieve first name.
$string = [addresses] => Array ( [shipping] =>
AddressesAddress Object (
[addressBook:AddressesAddress:private] => AddressesAddressBook Object (
[performanceHint:AddressesAddressBook:private] => 1
[uid:AddressesAddressBook:private] => 1
[addresses:AddressesAddressBook:private] => Array (
[-1] => AddressesAddress Object *RECURSION*
[-8] => AddressesAddress Object (
[addressBook:AddressesAddress:private] => AddressesAddressBook Object *RECURSION*
[schemaAddress:AddressesSchemaAddress:private] => stdClass Object (
[first_name] => Sir))))
I only know this:
$result= $string["shipping"]
and the result is
AddressesAddress Object (
[addressBook:AddressesAddress:private] => AddressesAddressBook Object (
[performanceHint:AddressesAddressBook:private] => 1
[uid:AddressesAddressBook:private] => 1
[addresses:AddressesAddressBook:private] => Array (
[-1] => AddressesAddress Object *RECURSION*
[-8] => AddressesAddress Object (
[addressBook:AddressesAddress:private] => AddressesAddressBook Object *RECURSION*
[schemaAddress:AddressesSchemaAddress:private] => stdClass Object (
[first_name] => Sir))))
I need help.

Related

PHP - How to access this complex nbt node object?

Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 10 [name] => )
[parent:Nbt\Node:private] => [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object (
[parent:Nbt\Node:private] => [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object (
[value:Nbt\Node:private] => Array ( [type] => 9 [name] => i [payloadType] => 10 )
[parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private]
=> Array ( [0] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 2 [name] => id
[value] => 367 ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION*
[children:Nbt\Node:private] => Array ( ) ) [1] => Nbt\Node Object ( [value:Nbt\Node:private] =>
Array ( [type] => 1 [name] => Count [value] => 64 ) <-- How to access this??[parent:Nbt\Node:private] => Nbt\Node Object
*RECURSION* [children:Nbt\Node:private] => Array ( ) ) [2] => Nbt\Node Object (
[value:Nbt\Node:private] => Array ( [type] => 10 [name] => tag ) [parent:Nbt\Node:private] =>
Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 9 [name] => ench [payloadType] => 0 ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( ) ) [1] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 3 [name] => HideFlags [value] => 254 ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( ) ) [2] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 10 [name] => display ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 9 [name] => Lore [payloadType] => 8 ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [value] => �f�lCOMMON ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( ) ) ) ) [1] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 8 [name] => Name [value] => �fEnchanted Rotten Flesh ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( ) ) ) ) [3] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 10 [name] => ExtraAttributes ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( [0] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 8 [name] => id [value] => ENCHANTED_ROTTEN_FLESH ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( ) ) ) ) ) ) [3] => Nbt\Node Object ( [value:Nbt\Node:private] => Array ( [type] => 2 [name] => Damage [value] => 0 ) [parent:Nbt\Node:private] => Nbt\Node Object *RECURSION* [children:Nbt\Node:private] => Array ( ) ) ) ) ) ) ) )
Alright I know this may look very messy and not organised but right now I need some help to access the Count value from this complicated nbt object as an echo-able string using PHP.
if you want a cleaner look of this here you go:
I'm so sorry if this looks messy but I can't find a way to format this crazy node nbt object thing I tried a lot of different ways but none of them work, Would really appreciate it if anyone helped.
Thanks.
$str = "...your big string... paste it here";
$needle = "Count [value] =>";
if(($pos = strpos($str, $needle)) !== false){
//echo "Count".substr($str,$pos+strlen($needle), )
// For precise numeric return -digit- might be very large, like, 100000000
// then
// find Count [value] => then find the first occurance of )
$newStr = substr($str, $pos+strlen($needle));
if(($posEnd = strpos($newStr, ")")) !== false){
echo "Count Value is ".trim(substr($newStr, 0, $posEnd));
}
}
output: Count Value is 64

PHP array to take out array value only one field

I have an array where i want only the one Field text from all array which is text only. i want all text from there.
stdClass Object
(
[language] => en
[textAngle] => 0
[orientation] => Up
[regions] => Array
(
[0] => stdClass Object
(
[boundingBox] => 81,63,1340,1055
[lines] => Array
(
[0] => stdClass Object
(
[boundingBox] => 321,63,855,117
[words] => Array
(
[0] => stdClass Object
(
[boundingBox] => 321,63,174,94
[text] => Set
)
[1] => stdClass Object
(
[boundingBox] => 529,87,126,69
[text] => an
)
[2] => stdClass Object
(
[boundingBox] => 693,65,483,115
[text] => example.
)
)
)
[1] => stdClass Object
(
[boundingBox] => 218,182,1059,116
[words] => Array
(
[0] => stdClass Object
(
[boundingBox] => 218,182,271,92
[text] => Treat
)
[1] => stdClass Object
(
[boundingBox] => 521,203,504,95
[text] => everyOne
)
[2] => stdClass Object
(
[boundingBox] => 1054,182,223,91
[text] => With
)
)
)
I want take out from here like [text]=>Set,[text]=>an,[text]=>example.
eg set an example.
Output should be eg. only like set an example
Given your example class above, I would try something like this.
$text = '';
foreach ($class->regions[0]->lines as $line){
foreach ($line->words as $word){
$text = $text." ".$word->text;
}
}
print $text;

Loop through xml array

Below is my code and I want to extract information of[field] => Array of index [1].It means [1] => 1030.670044.
This is xml file,and I want to store in my database,but unable to loop this in php.
Please Help me.
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 1.0
)
[meta] => SimpleXMLElement Object
(
[type] => resource-list
)
[resources] => SimpleXMLElement Object
(
[#attributes] => Array
(
[start] => 0
[count] => 168
)
[resource] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[classname] => Quote
)
[field] => Array
(
[0] => USD/KRW
[1] => 1030.670044
[2] => KRW=X
[3] => 1398752590
[4] => currency
[5] => 2014-04-29T06:23:10+0000
[6] => 0
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[classname] => Quote
)
[field] => Array
(
[0] => SILVER 1 OZ 999 NY
[1] => 0.051059
[2] => XAG=X
[3] => 1398721323
[4] => currency
[5] => 2014-04-28T21:42:03+0000
[6] => 400
)
)
This is a SimpleXML object. If the variable is called $xml you can access the field[1] like this:
foreach ($xml->resources->resource as $res) {
var_dump( $res->field[1] );
}

PHP - Objects and Arrays - How to access an stdClass object "name" "value" pair inside an array?

I would like to know how to return a value of an object with a name value pair that's inside an array. I've been trying all sorts of methods and frankly I realized I may be way over my head on this. I'd like some assistance trying to get the AirportsInformation_DataExtension value inside the property array.
stdClass Object
(
[OverallStatus] => OK
[RequestID] => 19e41b46-df68-47ba-8858-d728f3a92036
[Results] => stdClass Object
(
[PartnerKey] =>
[ObjectID] =>
[Type] => DataExtensionObject
[Properties] => stdClass Object
(
[Property] => Array
(
[0] => stdClass Object
(
[Name] => CampaignName
[Value] => 20130107_FlightDealsHotelPricePoints
)
[1] => stdClass Object
(
[Name] => StartDate
[Value] => 1/7/2013 12:00:00 AM
)
[2] => stdClass Object
(
[Name] => EndDate
[Value] => 1/15/2013 5:59:59 AM
)
[3] => stdClass Object
(
[Name] => CampaignType
[Value] => FlightDeals
)
[4] => stdClass Object
(
[Name] => LandingPage_ExpireDate
[Value] => 1/15/2013 5:59:59 AM
)
[5] => stdClass Object
(
[Name] => LandingPage_AutoRedirectOnExpire
[Value] => True
)
[6] => stdClass Object
(
[Name] => LandingPage_ExpireTargetURL
[Value] => test
)
[7] => stdClass Object
(
[Name] => BookByDate
[Value] => 1/22/2013 12:00:00 AM
)
[8] => stdClass Object
(
[Name] => TravelStartDate
[Value] =>
)
[9] => stdClass Object
(
[Name] => TravelEndDate
[Value] =>
)
[10] => stdClass Object
(
[Name] => FlightDeals_DataExtension
[Value] => 20130107_DestinationFlightDeals
)
[11] => stdClass Object
(
[Name] => FlightDeals_SortOrder_DataExtension
[Value] => FlightDeals_DestinationSortOrder
)
[12] => stdClass Object
(
[Name] => HotelDeals_DataExtension
[Value] => 20130107_FlightDealsHotelPricePoints
)
[13] => stdClass Object
(
[Name] => HotelDeals_All_DataExtension
[Value] => 20130107_HotelPackageDeals_ALL
)
[14] => stdClass Object
(
[Name] => HotelInformation_DataExtension
[Value] => EmailHotelInformation
)
[15] => stdClass Object
(
[Name] => AirportsInformation_DataExtension
[Value] => Airports
)
[16] => stdClass Object
(
[Name] => RoutesInformation_DataExtension
[Value] => Routes
)
[17] => stdClass Object
(
[Name] => DFP_DataExtension
[Value] => ET_DestinationIframeSrc
)
[18] => stdClass Object
(
[Name] => DeepLinkConnectorURL
[Value] => http://www.somewebsite/BookingConnector.html?mode=run
)
[19] => stdClass Object
(
[Name] => DefaultDestinationScenery
[Value] => LAS
)
[20] => stdClass Object
(
[Name] => DefaultHomeAirportCode
[Value] =>
)
[21] => stdClass Object
(
[Name] => FailSafeHomeAiportCode
[Value] =>
)
[22] => stdClass Object
(
[Name] => DFP_Campaign_Banner
[Value] => True
)
[23] => stdClass Object
(
[Name] => EmailID
[Value] => 44388
)
)
)
)
)
Using a foreach loop I was able to print out all lines with name/value sets
foreach ($results->Results->Properties->Property as $CurrentProp){
print('<br>');
print('Name: '.$CurrentProp->Name. ' Value: '.$CurrentProp->Value.'<br>');
};
Sadly I can't get passed that. I just need to retrieve the value. Thanks in advance.
In order to get the value, you could loop over them and test for the name matching AirportsInformation_DataExtension:
foreach ($results->Results->Properties->Property as $CurrentProp){
if($CurrentProp->Name == 'AirportsInformation_DataExtension')
{
echo 'The value is: ' . $CurrentProp->Value;
}
}
If you would need to be able to get all the values based on their name, it could be useful to turn it in to an associative array, like this
$results->Results->Properties->PropertyArray = array();
foreach($results->Results->Properties->Property as $arrCurrentProperty) {
$results->Results->Properties->PropertyArray[$arrCurrentProperty->Name] = $arrCurrentProperty->Value;
};
Then you can get the values later by indexing them directly, i.e.
echo 'The value is: ' . $results->Results->Properties->PropertyArray['AirportsInformation_DataExtension'];
Use the above solution by MrCode or, simply just use $results->Results->Properties->Property[15]->Value if the index of AirportsInformation_DataExtension is always 15. Since an array is an ordered list, it is very probably that the index does not change unless some of the items are removed/added from the array/
foreach ($results->Results->Properties->Property as $CurrentProp){
$tempArr[$CurrentProp->Name] = $CurrentProp->Value;
}
echo $tempArr['AirportsInformation_DataExtension'];
By this you can access any other key of that object.

How can I parse this result?

Array (
[0] => stdClass Object (
[name] => query1
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 226 ) ) )
[1] => stdClass Object (
[name] => query2
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 247 ) ) )
[2] => stdClass Object (
[name] => query3
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 580 ) ) )
[3] => stdClass Object (
[name] => query4
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 10 ) ) )
[4] => stdClass Object (
[name] => query5
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 508 ) ) )
)
How I can parse this result ?
I searched, but I didn't found out how I could do it.
Assuming your array variable is called $data:
foreach ($data as $object) {
$name = $object->name;
$result_set = $object->fql_result_set;
$fan_count = $result_set[0]->fan_count;
// Do what you need to with the data here
}
Hope that helps

Categories