PHP - get values from multi level array

Hi,
what is the best way to extract and display the following

Regular Parcel $16.65
Express Post Parcel $39.30

from my array below  

$auspost_array = json_decode($auspost_json,true);

print_r($auspost_array);

Array ( [services] => Array ( [service] => Array ( [0] => Array ( [code] => AUS_PARCEL_REGULAR [name] => Regular Parcel [price] => 16.65 [max_extra_cover] => 5000 [options] => Array ( [option] => Array ( [0] => Array ( [code] => AUS_SERVICE_OPTION_STANDARD [name] => Standard Service ) [1] => Array ( [code] => AUS_SERVICE_OPTION_REGISTERED_POST [name] => Registered Post [suboptions] => Array ( [option] => Array ( [0] => Array ( [code] => AUS_SERVICE_OPTION_DELIVERY_CONFIRMATION [name] => Delivery Confirmation ) [1] => Array ( [code] => AUS_SERVICE_OPTION_PERSON_TO_PERSON [name] => Person to Person ) [2] => Array ( [code] => AUS_SERVICE_OPTION_EXTRA_COVER [name] => Extra Cover ) ) ) ) [2] => Array ( [code] => AUS_SERVICE_OPTION_COD_POSTAGE_FEES [name] => C.O.D - Postage & Fees ) [3] => Array ( [code] => AUS_SERVICE_OPTION_COD_MONEY_COLLECTION [name] => C.O.D - Money Collection, Postage & Fees ) [4] => Array ( [code] => AUS_SERVICE_OPTION_TRACK_OVER_500G [name] => Tracking (when lodged in a Retail outlet for parcels over 500gms) ) [5] => Array ( [code] => AUS_SERVICE_OPTION_TRACK [name] => Tracking (available through Click and Send) ) [6] => Array ( [code] => AUS_SERVICE_OPTION_TRACK_SIGN [name] => Tracking with Signature (available through Click and Send) [suboptions] => Array ( [option] => Array ( [code] => AUS_SERVICE_OPTION_EXTRA_COVER [name] => Extra Cover ) ) ) ) ) ) [1] => Array ( [code] => AUS_PARCEL_EXPRESS [name] => Express Post Parcel [price] => 39.30 ) [2] => Array ( [code] => AUS_PARCEL_PLATINUM [name] => Express Post Platinum Parcel [price] => 44.70 [max_extra_cover] => 5000 [options] => Array ( [option] => Array ( [code] => AUS_SERVICE_OPTION_PLATINUM_EXTRA_COVER_SERVICE [name] => Standard cover [suboptions] => Array ( [option] => Array ( [code] => AUS_SERVICE_OPTION_EXTRA_COVER [name] => Extra Cover ) ) ) ) ) ) ) )

Open in new window


Thanks in advance for your feedback..
sabecsAsked:
Who is Participating?
 
Ray PaseurCommented:
Please see http://www.laprbass.com/RAY_temp_sabecs.php

<?php // RAY_temp_sabecs.php
error_reporting(E_ALL);
echo '<pre>';

// SOME TEST DATA
$jso = <<<JSO
{"services":{"service":[{"code":"AUS_PARCEL_REGULAR","name":"Regular Parcel","price":14.75,"max_extra_cover":5000,"options":{"option":[{"code":"AUS_SERVICE_OPTION_STANDARD","name":"Standard Service"},{"code":"AUS_SERVICE_OPTION_REGISTERED_POST","name":"Registered Post","suboptions":{"option":[{"code":"AUS_SERVICE_OPTION_DELIVERY_CONFIRMATION","name":"Delivery Confirmation"},{"code":"AUS_SERVICE_OPTION_PERSON_TO_PERSON","name":"Person to Person"},{"code":"AUS_SERVICE_OPTION_EXTRA_COVER","name":"Extra Cover"}]}},{"code":"AUS_SERVICE_OPTION_COD_POSTAGE_FEES","name":"C.O.D - Postage & Fees"},{"code":"AUS_SERVICE_OPTION_COD_MONEY_COLLECTION","name":"C.O.D - Money Collection, Postage & Fees"},{"code":"AUS_SERVICE_OPTION_TRACK_OVER_500G","name":"Tracking (when lodged in a Retail outlet for parcels over 500gms)"}]}},{"code":"AUS_PARCEL_REGULAR_SATCHEL_5KG","name":"Prepaid Parcel Post Plus 5kg Satchel","price":"14.50","max_extra_cover":5000,"options":{"option":[{"code":"AUS_SERVICE_OPTION_STANDARD","name":"Standard Service"},{"code":"AUS_SERVICE_OPTION_REGISTERED_POST","name":"Registered Post","suboptions":{"option":[{"code":"AUS_SERVICE_OPTION_DELIVERY_CONFIRMATION","name":"Delivery Confirmation"},{"code":"AUS_SERVICE_OPTION_PERSON_TO_PERSON","name":"Person to Person"},{"code":"AUS_SERVICE_OPTION_EXTRA_COVER","name":"Extra Cover"}]}},{"code":"AUS_SERVICE_OPTION_COD_POSTAGE_FEES","name":"C.O.D - Postage & Fees"},{"code":"AUS_SERVICE_OPTION_COD_MONEY_COLLECTION","name":"C.O.D - Money Collection, Postage & Fees"}]}},{"code":"AUS_PARCEL_EXPRESS_SATCHEL_5KG","name":"Express Post 5kg Satchel","price":21.65},{"code":"AUS_PARCEL_EXPRESS","name":"Express Post Parcel","price":"30.50"},{"code":"AUS_PARCEL_PLATINUM","name":"Express Post Platinum Parcel","price":"35.90","max_extra_cover":5000,"options":{"option":{"code":"AUS_SERVICE_OPTION_PLATINUM_EXTRA_COVER_SERVICE","name":"Standard cover","suboptions":{"option":{"code":"AUS_SERVICE_OPTION_EXTRA_COVER","name":"Extra Cover"}}}}}]}}
JSO;

// DECODE IT INTO A USEFUL OBJECT DATA STRUCTURE
$obj = json_decode($jso);

// ACTIVATE THIS TO SEE THE ENTIRE OBJECT
// var_dump($obj);

// WHAT WE WANT TO FIND
$names = array
( 'Regular Parcel'
, 'Express Post Parcel'
)
;

// USE AN ITERATOR TO ACCESS THE ARRAY OF INTERNAL OBJECTS
foreach ($obj->services->service as $serviceObject)
{
    $n = (string)$serviceObject->name;
    $p = (string)$serviceObject->price;
    if (in_array($n, $names)) echo PHP_EOL . "$n $p";
}

Open in new window

HTH, ~Ray
0
 
lwadwellCommented:
Best way ... not sure about that.
A way ... how about two:
// Simple Loop
foreach($auspost_array['services']['service'] as $key => $value){
    if ( $value['code'] == 'AUS_PARCEL_REGULAR' ) echo "AUS_PARCEL_REGULAR price = ", $value['price'], PHP_EOL;
    if ( $value['code'] == 'AUS_PARCEL_EXPRESS' ) echo "AUS_PARCEL_EXPRESS price = ", $value['price'], PHP_EOL;
}

// Using a custom function
function getPrice ($arr, $needle) {
    foreach($arr as $key => $value){
        if ( $value['code'] == $needle ) return $value['price'];
    }
    return null;
}
echo "AUS_PARCEL_REGULAR price = ", getPrice($auspost_array['services']['service'], 'AUS_PARCEL_REGULAR'), PHP_EOL;
echo "AUS_PARCEL_EXPRESS price = ", getPrice($auspost_array['services']['service'], 'AUS_PARCEL_EXPRESS'), PHP_EOL;

Open in new window

0
 
Marco GasiFreelancerCommented:
You could use the SPL RecursiveArrayIterator (http://it.php.net/recursivearrayiterator):

$matches = array();
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($auspost_array));
foreach($iterator as $name=>$value) {
  if(preg_match("/Regular Parcel/", $name) || preg_match("/Express Post Parcel/", $name)){
    $maches[] = $name . ": " . $value;
  }
}

print_r($matches);

Cheers
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Mark BradyPrincipal Data EngineerCommented:
You might want to consider breaking that array down into smaller arrays. That would not be the easiest to work with like it is.
0
 
Ray PaseurCommented:
Maybe the best way is not to use your array, but to use the original data.  Can you please post that in the code snippet?  Thanks, ~Ray
0
 
Mark BradyPrincipal Data EngineerCommented:
Good call Ray :)
0
 
sabecsAuthor Commented:
Sorry for the delay in getting back to you, I though I had already accepted a solution. lwadwell solutions works for me but if there is a better way I have posted the original data below.

Thanks..

{"services":{"service":[{"code":"AUS_PARCEL_REGULAR","name":"Regular Parcel","price":14.75,"max_extra_cover":5000,"options":{"option":[{"code":"AUS_SERVICE_OPTION_STANDARD","name":"Standard Service"},{"code":"AUS_SERVICE_OPTION_REGISTERED_POST","name":"Registered Post","suboptions":{"option":[{"code":"AUS_SERVICE_OPTION_DELIVERY_CONFIRMATION","name":"Delivery Confirmation"},{"code":"AUS_SERVICE_OPTION_PERSON_TO_PERSON","name":"Person to Person"},{"code":"AUS_SERVICE_OPTION_EXTRA_COVER","name":"Extra Cover"}]}},{"code":"AUS_SERVICE_OPTION_COD_POSTAGE_FEES","name":"C.O.D - Postage & Fees"},{"code":"AUS_SERVICE_OPTION_COD_MONEY_COLLECTION","name":"C.O.D - Money Collection, Postage & Fees"},{"code":"AUS_SERVICE_OPTION_TRACK_OVER_500G","name":"Tracking (when lodged in a Retail outlet for parcels over 500gms)"}]}},{"code":"AUS_PARCEL_REGULAR_SATCHEL_5KG","name":"Prepaid Parcel Post Plus 5kg Satchel","price":"14.50","max_extra_cover":5000,"options":{"option":[{"code":"AUS_SERVICE_OPTION_STANDARD","name":"Standard Service"},{"code":"AUS_SERVICE_OPTION_REGISTERED_POST","name":"Registered Post","suboptions":{"option":[{"code":"AUS_SERVICE_OPTION_DELIVERY_CONFIRMATION","name":"Delivery Confirmation"},{"code":"AUS_SERVICE_OPTION_PERSON_TO_PERSON","name":"Person to Person"},{"code":"AUS_SERVICE_OPTION_EXTRA_COVER","name":"Extra Cover"}]}},{"code":"AUS_SERVICE_OPTION_COD_POSTAGE_FEES","name":"C.O.D - Postage & Fees"},{"code":"AUS_SERVICE_OPTION_COD_MONEY_COLLECTION","name":"C.O.D - Money Collection, Postage & Fees"}]}},{"code":"AUS_PARCEL_EXPRESS_SATCHEL_5KG","name":"Express Post 5kg Satchel","price":21.65},{"code":"AUS_PARCEL_EXPRESS","name":"Express Post Parcel","price":"30.50"},{"code":"AUS_PARCEL_PLATINUM","name":"Express Post Platinum Parcel","price":"35.90","max_extra_cover":5000,"options":{"option":{"code":"AUS_SERVICE_OPTION_PLATINUM_EXTRA_COVER_SERVICE","name":"Standard cover","suboptions":{"option":{"code":"AUS_SERVICE_OPTION_EXTRA_COVER","name":"Extra Cover"}}}}}]}}

Open in new window

0
 
sabecsAuthor Commented:
Thanks for your help, much appreciated.
0
 
Ray PaseurCommented:
Thanks for the points! ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.