how to get the value of array? - php

I want to get the value of productId and addressTypeID in this array:
Array
(
[0] => Array
(
[entityId] => 7100122
[orderId] => 110631
[createDate] => 2010-10-19T05:40:00
[lastUpdateDate] => 2010-10-19T05:40:00
[deleted] =>
[products] => Array
(
[Product] => Array
(
[productId] => 674976
[productCode] =>
[productDescription] => testtest
[units] => 3
)
)
[addresses] => Array
(
[Address] => Array
(
[addressTypeID] => 8
[addressLine1] => Cebu, Ceity
[city] => City
[zipcode] => 6000
[state] => cebu
[countryCode] => PH
)
)
)
)
How can I get the value of it?

like:
$myarray[0]['products']['Product']['productId']
$myarray[0]['addresses']['Address']['addressTypeID']
look more in
http://php.net/manual/en/language.types.array.php

$product_id = $your_array['products']['Product']['productId'];
$address_id = $your_array['addresses']['Address']['addressTypeID'];

Related

How to insert a new values pair in an associative array in PHP?

I've an associative array. I am attempting to create each sub array within a loop and insert it into a multidimensional array. Google suggested using array_merge, but after using 'print_r' on the multidimensional array with the code below, only the last sub-array is being displayed.
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
[id] => 184
[firstName] => skg
[lastName] => s
[address] => Array
(
[address1] => sakthi
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
)
[jobOrder] => Array
(
[id] => 88
[title] => Tech Analyst
)
)
[1] => Array
(
[candidate] => Array
(
[id] => 852
[firstName] => mso cool
[lastName] =>
[address] => Array
(
[address1] =>
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
)
[jobOrder] => Array
(
[id] => 57
[title] => Tester
)
)
And Another Array like this :
[rating] => Array
(
[0] => 7
[1] => 5
[2] =>
)
How to get merge this array value into previous array values like
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
[id] => 184
[firstName] => skg
[lastName] => s
[address] => Array
(
[address1] => sakthi
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
[rating] => 7
)
[jobOrder] => Array
(
[id] => 88
[title] => Tech Analyst
)
)
[1] => Array
(
[candidate] => Array
(
[id] => 852
[firstName] => mso cool
[lastName] =>
[address] => Array
(
[address1] =>
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
[rating] => 5
)
[jobOrder] => Array
(
[id] => 57
[title] => Tester
)
)
I tried lot of tricks but couldn't get the desired array format. Can any one help me in this to get the desired array? Thanks in advance.
$arr = array(); //filled with data
$rating = array(); //filled with rates
foreach ( $rating as $key =>$rate ) {
$arr['data'][ $key ]['rating'] = $rate;
}
<?php
$arr=Array(
'data'=>Array(
0=>Array('candidate'=>Array()),
1=>Array('candidate'=>Array())
)
);
$rating=Array(7,5);
foreach($rating as $key=>$val){
$arr['data'][$key]['rating']=$val;
}
print_r($arr);
Returns:
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
)
[rating] => 7
)
[1] => Array
(
[candidate] => Array
(
)
[rating] => 5
)
)
)

How to retrieve object from xml

I am trying to get the value of bank_name in php. Can't get it to display.
stdClass Object ( [account_id] => 43726384 [name] => Account Name [state] => action_required [description] => [owner_user_id] =>[type] => personal [create_time] => 1477684534 [disablement_time] => [country] => US [currencies] => Array ( [0] => USD ) [action_reasons] => Array ( [0] => kyc [1] => bank_account ) [disabled_reasons] => Array ( ) [image_uri] => [supported_card_types] => Array ( [0] => visa [1] => mastercard [2] => american_express [3] => discover [4] => jcb [5] => diners_club ) [gaq_domains] => Array ( [0] => ) [balances] => Array ( [0] => stdClass Object ( [balance] => 0 [currency] => USD [disputed_amount] => 0 [incoming_pending_amount] => 0 [outgoing_pending_amount] => 0 [reserved_amount] => 0 [bank_name] => Bank Of America [withdrawal_next_time] => [withdrawal_period] => [withdrawal_type] => ) ) [statuses] => Array ( [0] => stdClass Object ( [currency] => USD [incoming_payments_status] => ok [outgoing_payments_status] => paused [account_review_status] => not_requested ) ) )
It's not very clear if you display data that way, but I think this would be the way to access to bank_name:
$bankName = $object->balances[0]->bank_name;
(If it doesn't work, please show the XML before parsing it to an object)

Multidimensional Array PHP Output Values as a row [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is my array
Array
(
[TrackResponse] => Array
(
[Response] => Array
(
[TransactionReference] => Array
(
[CustomerContext] => RocketShipIt
)
[ResponseStatusCode] => 1
[ResponseStatusDescription] => Success
)
[Shipment] => Array
(
[Shipper] => Array
(
[ShipperNumber] => 746354
[Address] => Array
(
[AddressLine1] => 11 PINE ST
[City] => NEW BEDFORD
[StateProvinceCode] => MA
[PostalCode] => 01032 9785
[CountryCode] => US
)
)
[ShipTo] => Array
(
[Address] => Array
(
[City] => FOUNTAIN HILLS
[StateProvinceCode] => AZ
[PostalCode] => 85268
[CountryCode] => US
)
)
[ShipmentWeight] => Array
(
[UnitOfMeasurement] => Array
(
[Code] => LBS
)
[Weight] => 1.00
)
[Service] => Array
(
[Code] => 003
[Description] => GROUND
)
[ShipmentIdentificationNumber] => 746463772264354327
[PickupDate] => 20140709
[DeliveryDateUnavailable] => Array
(
[Type] => Scheduled Delivery
[Description] => Scheduled Delivery Date is not currently available, please try back later
)
[Package] => Array
(
[TrackingNumber] => 746463772264354327
[Activity] => Array
(
[0] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => FOUNTAIN HILLS
[StateProvinceCode] => AZ
[PostalCode] => 85268
[CountryCode] => US
)
[Code] => ML
[Description] => FRONT DOOR
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => D
[Description] => DELIVERED
)
[StatusCode] => Array
(
[Code] => FS
)
)
[Date] => 20140716
[Time] => 142400
)
[1] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => TEMPE
[StateProvinceCode] => AZ
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => OUT FOR DELIVERY
)
[StatusCode] => Array
(
[Code] => DS
)
)
[Date] => 20140716
[Time] => 041900
)
[2] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => TEMPE
[StateProvinceCode] => AZ
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => ARRIVAL SCAN
)
[StatusCode] => Array
(
[Code] => AR
)
)
[Date] => 20140715
[Time] => 114500
)
[3] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => HODGKINS
[StateProvinceCode] => IL
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => DEPARTURE SCAN
)
[StatusCode] => Array
(
[Code] => DP
)
)
[Date] => 20140711
[Time] => 144600
)
[4] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => HODGKINS
[StateProvinceCode] => IL
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => LOCATION SCAN
)
[StatusCode] => Array
(
[Code] => LC
)
)
[Date] => 20140711
[Time] => 090500
)
[5] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => HODGKINS
[StateProvinceCode] => IL
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => UNLOAD SCAN
)
[StatusCode] => Array
(
[Code] => UL
)
)
[Date] => 20140711
[Time] => 085300
)
[6] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => HODGKINS
[StateProvinceCode] => IL
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => ARRIVAL SCAN
)
[StatusCode] => Array
(
[Code] => AR
)
)
[Date] => 20140711
[Time] => 061900
)
[7] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => W SPRINGFIELD
[StateProvinceCode] => MA
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => DEPARTURE SCAN
)
[StatusCode] => Array
(
[Code] => DP
)
)
[Date] => 20140709
[Time] => 213400
)
[8] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => W SPRINGFIELD
[StateProvinceCode] => MA
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => ORIGIN SCAN
)
[StatusCode] => Array
(
[Code] => OR
)
)
[Date] => 20140709
[Time] => 183400
)
[9] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => W SPRINGFIELD
[StateProvinceCode] => MA
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => P
[Description] => PICKUP SCAN
)
[StatusCode] => Array
(
[Code] => PU
)
)
[Date] => 20140709
[Time] => 163200
)
[10] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => M
[Description] => BILLING INFORMATION RECEIVED
)
[StatusCode] => Array
(
[Code] => MP
)
)
[Date] => 20140709
[Time] => 112842
)
)
[PackageWeight] => Array
(
[UnitOfMeasurement] => Array
(
[Code] => LBS
)
[Weight] => 1.00
)
)
)
)
)
I'm looking to access values in the Activity numeric indexed array like such.
foreach($response as $row)
{
foreach($row['Shipment']['Package'] as $k)
{
echo $k['ActivityLocation']['Address']['City'];
}
}
I wanna be able to access the values inside of the numeric keyed array, and print them out in a row 1-10 etc.
The error i'm getting is Fatal error: Cannot use string offset as an array in test.php on line 287
If anybody could help me solve this you would be the bestus!
Please and Thanks
foreach($row['Shipment']['Package'] as $k)
{
echo $k['ActivityLocation']['Address']['City'];
}

parsing xml array response in PHP objects

i am working on a UPS tracking API, i have called the API to track the package, i am getting the right response, but in array format, i am new to json decoding, please tell me how to parse this in PHP, do i have to create multiple objects in PHP? i am attaching the output as well as the PHP code.
PHP Code
if (isset($_POST['af0'])) {
if (preg_match('/^[a-z\d_]{4,80}$/i', $_POST['trackingNumber'])) {
$cleanTrackingNumber = $_POST['trackingNumber'];
$someArray = upsTrack("$cleanTrackingNumber");
echo '<pre>'; print_r($someArray); echo '</pre>';
} else {
echo 'Invalid tracking number... sigh...';
}
}
Output
Array
(
[TRACKRESPONSE] => Array
(
[RESPONSE] => Array
(
[TRANSACTIONREFERENCE] => Array
(
[XPCIVERSION] => 1.0
)
[RESPONSESTATUSCODE] => 1
[RESPONSESTATUSDESCRIPTION] => Success
)
[SHIPMENT] => Array
(
[SHIPPER] => Array
(
[SHIPPERNUMBER] => A6161A
[ADDRESS] => Array
(
[ADDRESSLINE1] => 132 E 43RD ST
[CITY] => NEW YORK
[STATEPROVINCECODE] => NY
[POSTALCODE] => 10017 4019
[COUNTRYCODE] => US
)
)
[SHIPTO] => Array
(
[ADDRESS] => Array
(
[CITY] => TORONTO
[STATEPROVINCECODE] => ON
[POSTALCODE] => M5V3X1
[COUNTRYCODE] => CA
)
)
[SHIPMENTWEIGHT] => Array
(
[UNITOFMEASUREMENT] => Array
(
[CODE] => LBS
)
[WEIGHT] => 3.20
)
[SERVICE] => Array
(
[CODE] => 011
[DESCRIPTION] => STANDARD
)
[REFERENCENUMBER] => Array
(
[CODE] => 13
[VALUE] => A6161AD9HPK
)
[SHIPMENTIDENTIFICATIONNUMBER] => 1ZA6161A6832763249
[PICKUPDATE] => 20140519
[SCHEDULEDDELIVERYDATE] => 20140521
[PACKAGE] => Array
(
[TRACKINGNUMBER] => 1ZA6161A6832763249
[ACTIVITY] => Array
(
[ACTIVITYLOCATION] => Array
(
[ADDRESS] => Array
(
[CITY] => SECAUCUS
[STATEPROVINCECODE] => NJ
[COUNTRYCODE] => US
)
)
[STATUS] => Array
(
[STATUSTYPE] => Array
(
[CODE] => I
[DESCRIPTION] => DEPARTURE SCAN
)
[STATUSCODE] => Array
(
[CODE] => DP
)
)
[DATE] => 20140520
[TIME] => 053000
)
[MESSAGE] => Array
(
[CODE] => 01
[DESCRIPTION] => On Time
)
[PACKAGEWEIGHT] => Array
(
[UNITOFMEASUREMENT] => Array
(
[CODE] => LBS
)
[WEIGHT] => 3.20
)
[REFERENCENUMBER] => Array
(
[CODE] => 19
[VALUE] => MMTD71EUY061A
)
)
)
)
)
If you already have the output in Array format then you don't have anything more to do with json decoding.
You can use $someArray to get any data from the array.
It's up to you if you want to just display it or save it, for example in a database.
What is it you trying to accomplish?

How to get the Multidimensional array value in foreach loop using php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
This my code ..... I need output of hotel name, id, selling price etc
array value will be loop because I have get result in bulk manner .
Array
(
[TestMode] => true
[HotelAvailability] => Array
(
[0] => Array
(
[Hotel] => Array
(
[Id] => 3027405
[Name] => Royal Plaza
[Region] => Array
(
[Id] => 16849
[Name] => Chennai (Madras)
)
[Type] => Hotel
[Stars] => 3
[Rank] => 1
)
[Result] => Array
(
[QuoteId] => 12249136-10
[Room] => Array
(
[RoomType] => Array
(
[Code] => 1002196
[Text] => Double Standard
)
[MealType] => Array
(
[Code] => 1000018
[Text] => Bed and breakfast
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1824.26
[Estimated] => false
[Converted] => false
)
[Guests] => Array
(
[Adult] => Array
(
[0] => Array
(
[Id] => -252645204
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 912.13
[Estimated] => false
[Converted] => false
)
)
[1] => Array
(
[Id] => -252645205
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 912.13
[Estimated] => false
[Converted] => false
)
)
)
)
[Confirmation] => allocation
)
)
)
[1] => Array
(
[Hotel] => Array
(
[Id] => 2118726
[Name] => Days Inn Deccan Plaza
[Region] => Array
(
[Id] => 16849
[Name] => Chennai (Madras)
)
[Type] => Hotel
[Stars] => 3
[Rank] => 1
)
[Result] => Array
(
[QuoteId] => 12249136-36
[Room] => Array
(
[RoomType] => Array
(
[Code] => 1002196
[Text] => Double Standard
)
[MealType] => Array
(
[Code] => 1000018
[Text] => Bed and breakfast
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 3192.90
[Estimated] => false
[Converted] => false
)
[Guests] => Array
(
[Adult] => Array
(
[0] => Array
(
[Id] => -252645292
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1596.45
[Estimated] => false
[Converted] => false
)
)
[1] => Array
(
[Id] => -252645293
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1596.45
[Estimated] => false
[Converted] => false
)
)
)
)
[Confirmation] => allocation
)
)
)
[2] => Array
(
[Hotel] => Array
(
[Id] => 723729
[Name] => Green Park
[Region] => Array
(
[Id] => 16849
[Name] => Chennai (Madras)
)
[Type] => Hotel
[Stars] => 4
[Rank] => 1
)
[Result] => Array
(
[0] => Array
(
[QuoteId] => 12249136-33
[Room] => Array
(
[RoomType] => Array
(
[Code] => 1004527
[Text] => Double Or Twin Deluxe
)
[MealType] => Array
(
[Code] => 1000018
[Text] => Bed and breakfast
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 3273.82
[Estimated] => false
[Converted] => false
)
[Guests] => Array
(
[Adult] => Array
(
[0] => Array
(
[Id] => -252645286
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1636.91
[Estimated] => false
[Converted] => false
)
)
[1] => Array
(
[Id] => -252645287
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1636.91
[Estimated] => false
[Converted] => false
)
)
)
)
[Confirmation] => allocation
)
)
[1] => Array
(
[QuoteId] => 12249136-34
[Room] => Array
(
[RoomType] => Array
(
[Code] => 2062742
[Text] => Double or Twin BUSINESS-CLUB
)
[MealType] => Array
(
[Code] => 1000018
[Text] => Bed and breakfast
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 3863.18
[Estimated] => false
[Converted] => false
)
[Guests] => Array
(
[Adult] => Array
(
[0] => Array
(
[Id] => -252645288
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1931.59
[Estimated] => false
[Converted] => false
)
)
[1] => Array
(
[Id] => -252645289
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1931.59
[Estimated] => false
[Converted] => false
)
)
)
)
[Confirmation] => allocation
)
)
)
)
If I understood your question correctly, you would like to extract the Hotel Id, Name, and SellingPrice keys from this array, in a loop.
If we assume the above array is stored in a variable called $HotelData, and that the array structure is consistent with the above output in your question, then the following code should allow you to do what you want...
foreach($HotelData['HotelAvailability'] as $hotel) {
$id = $hotel['Hotel']['Id'];
$name = $hotel['Hotel']['Name'];
$price = $hotel['Result']['Room']['SellingPrice']['Currency'] . ' ' . $hotel['Result']['Room']['SellingPrice']['Amount'] ;
echo "$id - $name: $price<br>\n";
}
/* This should output something like
*
* 3027405 - Royal Plaza: INR 1824.26
* 2118726 - Days Inn Deccan Plaza - INR 3192.90
* ...
*/
foreach($data['HotelAvailability'] AS $record) {
echo 'Id: '; echo $record['Hotel']['Id'];
echo '<br />';
echo 'Name: '; echo $record['Hotel']['Name'];
echo '-------------'; echo '<br />';
}

Categories