0

I am trying to get values from array inside of another array. I have seen lots of answers here about this but i cannot manage to do so. I have an array like this:

Array
(
    [0] => Array
        (
            [listingid] => 1234
            [availability] => Array
                (
                    [0] => Array
                        (
                            [von] => 2015-11-07
                            [bis] => 2016-03-19
                        )

                    [1] => Array
                        (
                            [von] => 2016-03-19
                            [bis] => 2016-03-28
                        )

                    [2] => Array
                        (
                            [von] => 2016-03-28
                            [bis] => 2016-07-30
                        )

                )

        )

    [1] => Array
        (
            [listingid] => 5678
            [availability] => Array
                (
                    [0] => Array
                        (
                            [von] => 2015-11-07
                            [bis] => 2016-03-19
                        )

                    [1] => Array
                        (
                            [von] => 2016-03-19
                            [bis] => 2016-03-28
                        )

                    [2] => Array
                        (
                            [von] => 2016-03-28
                            [bis] => 2016-07-30
                        )

                )

        )
)

and i do this to get values:

foreach($first as $key => $value){
    echo "Value: " . $value[$key]['availability']['von'] . "<br>";
}

which normally should work based on the answers i saw but it gives me empty value.

I have also tried this:

foreach($first as $key => $value){
    $listid = $value['listingid'];
    echo $listid;
}

and this gives me the value of the listingid. How can i get values for von and bis?

Any suggestions?

2
  • Look how you access listingid: $value['listingid']; and how you access availability: $value[$key]['availability']['von']. And also look in which dimension both of those keys are.. Commented Mar 31, 2016 at 13:12
  • 1
    Compare $value['listingid'] and $value[$key]['availability']['von'], so you know the first one works. Now also compare where those two keys are located: listingid and availability Commented Mar 31, 2016 at 13:15

1 Answer 1

1

You just need to run a nested loop.

foreach($first as $value){
   echo "Listing ID:".$value['listingid'];
   echo "<br/>";
   foreach($value['availability'] as $availability) {
         echo "Value: " . $availability['von'] . "<br>";
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot...i was trying this: foreach($first as $key => $value){ //$listid = $value['listingid']; foreach($first['availability'] as $availability){ echo "Value: " . $availability['von'] . "<br>"; } } but it was giving me errors.....
That's because you used $first['availability']. Should have used $value['availability'] instead. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.