Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

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..
0
sabecs
Asked:
sabecs
  • 3
  • 2
  • 2
  • +2
2 Solutions
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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
 
sabecsAuthor Commented:
Thanks for your help, much appreciated.
0
 
Ray PaseurCommented:
Thanks for the points! ~Ray
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now