geting values from arrays in php - php

I have the following from my code:
stdClass Object ( [orders] => Array ( [0] => stdClass Object ( [shipping_address] => stdClass Object ( [first_name] => John [last_name] => Doe [company] => [address_1] => 3927 Walnut Grove [address_2] => [city] => Rosemead [state] => CA [postcode] => 90001 [country] => US ) ) [1] => stdClass Object ( [shipping_address] => stdClass Object ( [first_name] => chris [last_name] => koh [company] => [address_1] => 745 bow [address_2] => [city] => diamond [state] => CA [postcode] => 90015[country] => US ) ) ) )
how would i extract just the first_name from both elements?
John and chris

The "Array" you get back is a mishmash of Arrays and Objects, objects are accessed differently.
Array values are accessed by
$an_array = array('apple','banana');
$an_array[0]; //will return apple
While Object values area accessed by
$an_object->key;
Take a look at your return object
stdClass Object ( //<-- Object
[orders] => Array ( //<-- Array
[0] => stdClass Object ( //<-- Object
[shipping_address] => stdClass Object ( //<-- Object
[first_name] => John
etc..
So to get for example the first name you can access it with:
$arrayAndObjects->orders[0]->shipping_address->first_name
Hope that helps you understand it a bit, here are links to the php documentation for Arrays & Objects
http://php.net/manual/en/language.types.object.php
http://php.net/manual/en/language.types.array.php

Related

php parse array in object detail

I got stuck on parsing array in my php object
stdClass Object
(
[result] => stdClass Object
(
[details] => Array
(
[0] => stdClass Object
(
[account] =>
[address] => add1
[category] => receive
[amount] => 1100
[label] =>
[vout] => 1
)
[1] => stdClass Object
(
[account] =>
[address] => add2
[category] => receive
[amount] => 11600
[label] =>
[vout] => 2
)
[2] => stdClass Object
(
[account] =>
[address] => add3
[category] => receive
[amount] => 1000
[label] =>
[vout] => 4
)
)
)
)
So how I can fetch all details indexes 0,1,2 etc
So, you could just iterate over details as below:
foreach($your_variable->result->details as $current_detail){
echo $current_detail->account;
// other code here
}
Your data is object if you converto to array u can simply do it
$array=json_decode(json_encode($data),true);
print_r($array);
If just one details key
$array=json_decode(json_encode($data),true)
print_r($array['result']['details']);

Access values in PHP object

I have the following PHP object, which I want to access specific values from it but I have difficulties in understanding how to start:
kamranahmedse\Geocode Object (
[service_url:kamranahmedse\Geocode:private] => http://maps.googleapis.com/maps/api/geocode/json?sensor=false
[service_results:kamranahmedse\Geocode:private] => stdClass Object (
[results] => Array (
[0] => stdClass Object (
[address_components] => Array (
[0] => stdClass Object (
[long_name] => 112
[short_name] => 112
[types] => Array (
[0] => street_number
)
)
[1] => stdClass Object (
[long_name] => Imittou
[short_name] => Imittou
[types] => Array (
[0] => route
)
)
[2] => stdClass Object (
[long_name] => Cholargos
[short_name] => Cholargos
[types] => Array (
[0] => locality
[1] => political
)
)
[3] => stdClass Object (
[long_name] => Cholargos
[short_name] => Cholargos
[types] => Array (
[0] => administrative_area_level_5
[1] => political
)
) ...
For example how can I access:
[long_name] => Cholargos
The variable you're trying to access is private and therefore you're not going to be able to access it directly.
A better way is to use the methods provided by the class you're using.
Looking at the code you've provided I can see that you're using https://github.com/kamranahmedse/php-geocode
Here's an example of how it can be used to get the locality:
<?php
require __DIR__."/vendor/autoload.php";
use kamranahmedse\Geocode;
$address = "Imittou 112 Cholargos 155 61 Greece";
$geocode = new Geocode($address);
echo $geocode->getLocality();
Also, looking at the class source code we can see that when the address information is retrieved and local variables populated the long_name is used to populate the locality, which is what you want:
$this->locality = $component->long_name;
So the above code example should answer your question.

How to print nested stdclass object array

I am trying to print values in this nested stdClass Object but I'm having trouble accessing them. How do I print the ID?
stdClass Object (
[AddUserWithLimitResult] => stdClass Object (
[Header] => stdClass Object (
[Code] => UserAdded
[Description] => User created
)
[ID] => 2243272
[UserName] => gmail.com
[FirstName] => sar
[LastName] => Sea
[Email] => gmail.com
[SubDomain] => olo
)
)
I tried this:
$object->AddUserWithLimitResult->Header->Code->ID;
If you tab out the output it becomes clear that ID is a property of AddUserWithLimitResult
stdClass Object (
[AddUserWithLimitResult] => stdClass Object (
[Header] => stdClass Object (
[Code] => UserAdded
[Description] => User created
)
[ID] => 2243272
[UserName] => gmail.com
[FirstName] => sar
[LastName] => Sea
[Email] => gmail.com
[SubDomain] => olo
)
)
$object->AddUserWithLimitResult->ID
Edit: It looks like you updated the question with the tabbed code. When I saw it it was difficult to tell which properties belonged to which objects.

PHP SimpleXML Object Property Exists as Array but Turns into Object when Accessed Directly

I'm experiencing a strange issue when attempting to parse a response from Authorize.Net's CIM API. PHP uses the API's response to create a SimpleXML object. A property of the object reports as being an array (and is, as it contains 2 unique indexes,) however when the property is accessed directly, print_r(), gettype(), and is_array() all report/treat it as an object, not as an array.
The following PHP code:
print_r($parsedresponse);
print_r($parsedresponse->profile);
print_r($parsedresponse->profile->paymentProfiles);
Generates the following output:
SimpleXMLElement Object
(
[messages] => SimpleXMLElement Object
(
[resultCode] => Ok
[message] => SimpleXMLElement Object
(
[code] => I00001
[text] => Successful.
)
)
[profile] => SimpleXMLElement Object
(
[merchantCustomerId] => 1
[description] => Customer Profile for Store Code MC Created 11/20/13
[email] => email#email.com
[customerProfileId] => 11111111
[paymentProfiles] => Array
(
[0] => SimpleXMLElement Object
(
[customerType] => individual
[billTo] => SimpleXMLElement Object
(
[firstName] => Test
[lastName] => Test
[company] => Company Name
[address] => 1234 Test St
[city] => Testlyvania
[state] => OH
[zip] => 44720
[country] => United States
[phoneNumber] => 123-456-7890
[faxNumber] => SimpleXMLElement Object
(
)
)
[customerPaymentProfileId] => 22222222
[payment] => SimpleXMLElement Object
(
[creditCard] => SimpleXMLElement Object
(
[cardNumber] => XXXX1111
[expirationDate] => XXXX
)
)
)
[1] => SimpleXMLElement Object
(
[customerType] => individual
[billTo] => SimpleXMLElement Object
(
[firstName] => Test
[lastName] => Test
[company] => SimpleXMLElement Object
(
)
[address] => 1234 Test St
[city] => Testlyvania
[state] => OH
[zip] => 44720
[country] => United States
[phoneNumber] => 1234567890
[faxNumber] => 123-456-7890
)
[customerPaymentProfileId] => 33333333
[payment] => SimpleXMLElement Object
(
[creditCard] => SimpleXMLElement Object
(
[cardNumber] => XXXX0002
[expirationDate] => XXXX
)
)
)
)
[shipToList] => Array
(
[0] => SimpleXMLElement Object
(
[firstName] => Test
[lastName] => Test
[company] => Company Name
[address] => 1234 Test St
[city] => Testlyvania
[state] => OH
[zip] => 44720
[country] => United States
[phoneNumber] => 123-456-7890
[faxNumber] => SimpleXMLElement Object
(
)
[customerAddressId] => 44444444
)
[1] => SimpleXMLElement Object
(
[firstName] => Test
[lastName] => Test
[company] => SimpleXMLElement Object
(
)
[address] => 1234 Test St
[city] => Testlyvania
[state] => OH
[zip] => 44720
[country] => United States
[phoneNumber] => 1234567890
[faxNumber] => 123-456-7890
[customerAddressId] => 55555555
)
)
)
)
SimpleXMLElement Object
(
[merchantCustomerId] => 1
[description] => Customer Profile for Store Code MC Created 11/20/13
[email] => email#email.com
[customerProfileId] => 11111111
[paymentProfiles] => Array
(
[0] => SimpleXMLElement Object
(
[customerType] => individual
[billTo] => SimpleXMLElement Object
(
[firstName] => Test
[lastName] => Test
[company] => Company Name
[address] => 1234 Test St
[city] => Testlyvania
[state] => OH
[zip] => 44720
[country] => United States
[phoneNumber] => 123-456-7890
[faxNumber] => SimpleXMLElement Object
(
)
)
[customerPaymentProfileId] => 22222222
[payment] => SimpleXMLElement Object
(
[creditCard] => SimpleXMLElement Object
(
[cardNumber] => XXXX1111
[expirationDate] => XXXX
)
)
)
[1] => SimpleXMLElement Object
(
[customerType] => individual
[billTo] => SimpleXMLElement Object
(
[firstName] => Test
[lastName] => Test
[company] => SimpleXMLElement Object
(
)
[address] => 1234 Test St
[city] => Testlyvania
[state] => OH
[zip] => 44720
[country] => United States
[phoneNumber] => 1234567890
[faxNumber] => 123-456-7890
)
[customerPaymentProfileId] => 33333333
[payment] => SimpleXMLElement Object
(
[creditCard] => SimpleXMLElement Object
(
[cardNumber] => XXXX0002
[expirationDate] => XXXX
)
)
)
)
[shipToList] => Array
(
[0] => SimpleXMLElement Object
(
[firstName] => Test
[lastName] => Test
[company] => Company Name
[address] => 1234 Test St
[city] => Testlyvania
[state] => OH
[zip] => 44720
[country] => United States
[phoneNumber] => 123-456-7890
[faxNumber] => SimpleXMLElement Object
(
)
[customerAddressId] => 44444444
)
[1] => SimpleXMLElement Object
(
[firstName] => Test
[lastName] => Test
[company] => SimpleXMLElement Object
(
)
[address] => 1234 Test St
[city] => Testlyvania
[state] => OH
[zip] => 44720
[country] => United States
[phoneNumber] => 1234567890
[faxNumber] => 123-456-7890
[customerAddressId] => 55555555
)
)
)
SimpleXMLElement Object
(
[customerType] => individual
[billTo] => SimpleXMLElement Object
(
[firstName] => Test
[lastName] => Test
[company] => Company Name
[address] => 1234 Test St
[city] => Testlyvania
[state] => OH
[zip] => 44720
[country] => United States
[phoneNumber] => 123-456-7890
[faxNumber] => SimpleXMLElement Object
(
)
)
[customerPaymentProfileId] => 22222222
[payment] => SimpleXMLElement Object
(
[creditCard] => SimpleXMLElement Object
(
[cardNumber] => XXXX1111
[expirationDate] => XXXX
)
)
)
As you can see, once the "paymentProfiles" property is accessed directly, it goes from being of type Array to type Object. However if I were to try to access it's second index directly, that works just fine.
The PHP code:
print_r($parsedresponse->profile->paymentProfiles[1])
Produces the following output:
SimpleXMLElement Object
(
[customerType] => individual
[billTo] => SimpleXMLElement Object
(
[firstName] => Test
[lastName] => Test
[company] => SimpleXMLElement Object
(
)
[address] => 1234 Test St
[city] => Testlyvania
[state] => OH
[zip] => 44720
[country] => United States
[phoneNumber] => 1234567890
[faxNumber] => 123-456-7890
)
[customerPaymentProfileId] => 33333333
[payment] => SimpleXMLElement Object
(
[creditCard] => SimpleXMLElement Object
(
[cardNumber] => XXXX0002
[expirationDate] => XXXX
)
)
)
Why when access directly does $parsedresponse->profile->paymentProfiles behave like an object rather than an array as expected?
This other answer seems to have most of the information you need. It helped me: Why does SimpleXML change my array to the array's first element when I use it?. The foreach loop worked well for me, though a standard for loop works as well.
So you should be able to do the following, even though $parsedresponse->profile in not an array according to print_r. It should also work if $parsedresponse->profile is an array.
print_r($parsedresponse);
foreach($profile as $prof){
print_r($prof);
foreach($prof->paymentProfiles as $pProf){
print_r($pProf);
}
}

How to access object data and display the comments

How do I access the comments and display them?
stdClass Object
(
[stream] => stdClass Object
(
[id] => 521734635_403433313001586
[from] => stdClass Object
(
[name] => Jonathan Rosario
[id] => 521734635
)
[message] => What if we do the same action in our country?...i just hope they be fair. Its just unfair that they also ban our Bibles while we never ban their Qu'rans in our country.
[comments] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[id] => 521734635_403433313001586_90696065
[from] => stdClass Object
(
[name] => ''
[id] => 570761262
)
[message] => test
[created_time] => 2012-03-29T10:01:49+0000
)
[1] => stdClass Object
(
[id] => 521734635_403433313001586_90696070
[from] => stdClass Object
(
[name] => Alwaynne Kevin
[id] => 100000364267158
)
[message] => Oo nga...bakit ganyan sila...hahah.!
[created_time] => 2012-03-29T10:07:52+0000
)
[2] => stdClass Object
(
[id] => 521734635_403433313001586_90696146
[from] => stdClass Object
(
[name] => Jp Peña
[id] => 100000599584313
)
[message] => they're just doing what the "christian" crusaders did to them.
[created_time] => 2012-03-29T10:33:12+0000
)
)
[count] => 3
)
[picture] => http://external.ak.fbcdn.net/safe_image.php?d=AQCr7a57qDUU2MHR&w=90&h=90&url=http%3A%2F%2Fwww.interaksyon.com%2Fassets%2Fimages%2Farticles%2Finterphoto_1332546902.jpg
[name] => Top Saudi cleric 'issues fatwa' on Christian churches; Bishops alarmed - InterAksyon.com
[description] => Christian bishops are sharply criticizing Saudi Arabia's top religious official after reports that he issued a fatwa saying all churches on the Arabian Peninsula should be destroyed. At least 3.5 million Christians live in the Gulf Arab region. They are mostly Catholic workers from the Philippines a...
[created_time] => 2012-03-29T10:00:41+0000
)
)
you got the whole list of objects in comments so give try following:
foreach( $yourObj->stream->comments as $comment){
echo sprintf('%s: %s', $comment->from->name, $comment->message);
}

Categories