basic Array question
Results 1 to 5 of 5

Thread: basic Array question

  1. #1
    Junior Member
    Join Date
    Dec 2016
    Posts
    9

    basic Array question

    I'm so confused with multidimensional arrays. May I please ask a couple questions?

    1.) In the json array below, How do I use PHP to echo, for example, the price offered by Susan?

    I tried things like echo $result["data"][0]["Susan"] to work, but it won't work.

    2.) Near the top of the array it shows the lowest price is $25.56. How do I get PHP to echo that?

    Again, I tried echo $result[LowestPrice] but that doesn't work.

    3.) The array below is really big. What code do I use to create a smaller array of just [Name] and [OfferingPrice] ??

    If I can do that, then I can do the PHP thing where you go "foreach ($result as $var=>$value) { echo "$var...$value" } etc., and I can sort it etc.

    Here's the array:

    $result = json_decode($stuff,true);
    print_r($result);

    //this is what it is output
    Code:
    Array
    (
        [data] => Array
            (
                [Info] => Array
                    (
                        [LowestPrice] => 25.56
                        [PlaceOfManufacture] => USA
                    )
    
                [OfferingPrice] => Array
                    (
                        [0] => Array
                            (
                                [OfferingPrice] => 20.00
                                [SalesAssociate] => Array
                                    (
                                        [BadgeNumber] => 125
                                        [Name] => Fred
                                    )
    
                            )
    
                        [1] => Array
                            (
                                [OfferingPrice] => 18.36
                                [SalesAssociate] => Array
                                    (
                                        [BadgeNumber] => 932
                                        [Name] => Susan
                                    )
    
                            )
    
                        [2] => Array
                            (
                                [OfferingPrice] => 2.34
                                [SalesAssociate] => Array
                                    (
                                        [BadgeNumber] => 73
                                        [Name] => Grandpa
                                    )
    
                            )
    
                        [3] => Array
                            (
                                [OfferingPrice] => 44.28
                                [SalesAssociate] => Array
                                    (
                                        [BadgeNumber] => 202
                                        [Name] => Stewart
                                    )
    
                               )
    
                    )
    
            )
    
    )
    Last edited by NogDog; 09-08-2017 at 02:35 PM. Reason: added [code] tags

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,843
    "Susan" is a value, not an array key, so you'd instead have to traverse the 'offeringPrice' sub-array until you find an element where ['SalesAssociate']['Name'] == 'Susan', maybe something like:
    PHP Code:
    $offerBySusan null;
    foreach(
    $result['data']['offeringPrice'] as $offer) {
        if(
    $offer['SalesAssociate']['Name'] == 'Susan') {
            
    $offerBySusan $offer['OfferingPrice'];
            break;
        }

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Junior Member
    Join Date
    Dec 2016
    Posts
    9
    Thank you NogDog. Interestingly, your suggestion works without the "break." What could go wrong if I left out "break" ?

  4. #4
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,505
    It would take a little longer, because it would continue to search even after finding what it was looking for; and if Susan made more than one offer (which I'd guess doesn't happen) you'd get the last offer in the list she made instead of the first.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  5. #5
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,843
    Yep. For reference: http://php.net/break
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •