1

I'm trying to get the values of a sub array out in a foreach loop.

I looked at this example as it seemed really relevant to my problem, but unfortunately I've not been able to get it to work, and I'm hoping someone here can pick up where I've gone wrong.

PHP foreach not working with sub-array

I have an array called $booked and inside that array I have a sub array $booked['30_booked']. Within that second array there are multiple values, such as title, name, address etc.

My code currently looks like this:

foreach($booked['30_booking'] as $new_var){
        var_dump('</br>', $booked['30_booking']);
        var_dump('</br>', $new_var);
        var_dump($new_var['title'], $new_var->title, $booked['30_booking']->title); exit(); 
    }

I've output the data as you can see above in var_dump statements to try and get one of these methods to work.

Unfortunately nothing within $new_var is pulling out the title, but $booked['30_booking']->title

Have I not put it into the foreach statement correctly?

All help appreciated - thanks!

EDIT:

Main array output snippet:

array(6) { ["30_booked"]=> object(stdClass)#21 (34) { ["id"]=> string(2) "30" ["title"]=> string(2) "Ms" ["firstname"]=> string(5) "FIRST NAME" ["surname"]=> string(9) "LAST NAME" ["address"]=> string(6) "- -- -" ["postcode"]=> string(7) "FAK E99" ["country"]=> string(14) "United Kingdom" ["phone"]=> string(11) "01221111111" ["alt_phone"]=> string(0) "" ["email"]=> string(25) "fake@fake.co.uk" ["notes"]=> string(8) "FAKE DEAL"  } } 

EDIT 2:

Sub Array $booked['30_booking'] snippet:

object(stdClass)#21 (34) {  ["id"]=> string(2) "30" ["title"]=> string(2) "Ms" ["firstname"]=> string(5) "FIRST NAME" ["surname"]=> string(9) "LAST NAME" ["address"]=> string(6) "- -- -" ["postcode"]=> string(7) "FAK E99" ["country"]=> string(14) "United Kingdom" ["phone"]=> string(11) "01221111111" ["alt_phone"]=> string(0) "" ["email"]=> string(25) "fake@fake.co.uk" ["notes"]=> string(8) "FAKE DEAL" }

EDIT 3:

My var_dump of the $new_var by itself is bringing back the value of the id - but when I try and get the second value out the sub array "title" it doesn't return anything.

FINAL FIX:

Thanks to Kita I realised I was returning a std class object and not a second array, something that I stupidly missed the first time round. Because of that I can't actually foreach on the object.

Which led me to this post which will help me fix the issue:

PHP foreach array with stdClass Object

Thank you very much for all your help!!!

Community
  • 1
  • 1
hlh3406
  • 1,382
  • 5
  • 29
  • 46

3 Answers3

1

You expected an array inside $booked['30_booking'] but in fact there was a stdClass object.

array(6) { 
    ["30_booked"]=> object(stdClass)#21 (34) {  
        ["id"]=> string(2) "30" 
        ["title"]=> string(2) "Ms" 
        ["firstname"]=> string(5) "FIRST NAME" 
        ["surname"]=> string(9) "LAST NAME" 
        ["address"]=> string(6) "- -- -" 
        ["postcode"]=> string(7) "FAK E99" 
        ["country"]=> string(14) "United Kingdom" 
        ["phone"]=> string(11) "01221111111" 
        ["alt_phone"]=> string(0) "" 
        ["email"]=> string(25) "fake@fake.co.uk" 
        ["notes"]=> string(8) "FAKE DEAL"
    } 
    //I assume you have left out the other array elements from the Main array snippet.
}

Getting stdClass instead of array usually happens when you parse a JSON string with json_decode() without the second parameter.

// without second parameter
var_dump( json_decode( '{"id":"30"}' ) );
object(stdClass)#1 (1) {
  ["id"]=>
  string(2) "30"
}

// 'true' as second parameter
var_dump( json_decode( '{"id":"30"}', true ) );
array(1) {
  ["id"]=>
  string(2) "30"
}

Above examples are hosted at: http://ideone.com/GNMRlD

Since stdClass object itself does not provide iteration functionality, using it with foreach will yield errors.

You can convert stdClass into array with functions such as http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/ .

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Kita
  • 2,604
  • 19
  • 25
  • Hi, thanks for your answer - as I'm pulling but a std class object instead of an array, should I not be able to display it by calling $new_var->title, or is it that I can't put a std class object into a foreach loop? Thanks for your help! – hlh3406 Jul 31 '13 at 10:13
  • `$booked["30_booked"]` itself is an `stdClass` object. You don't have to put it inside `foreach` loop, otherwise the code will generate errors. You should be able to fetch `title` with `$booked["30_booked"]->title`. – Kita Jul 31 '13 at 10:21
0

For eg. if your array looks like below. foreach which I used will be working

$new_array = array(array('total'=>array('title'=>'test','text'=>'text')));

foreach ($new_array as $val)
{
    print_r($val['total']['title']); // Use this for array
    print_r($val->total->title); // Use this for object
}

if your array looks like below. Use the below foreach.

$new_array = array(array('total'=>array(array('title'=>'test','text'=>'text'),array('title'=>'test1','text'=>'text1'))));

foreach ($new_array as $val)
{
    foreach($val['total'] as $new_val)
    {
        print_r($new_val['title']);
    }
}
Saranya Sadhasivam
  • 1,296
  • 6
  • 9
0

You can try this, I hope that helps you

foreach($booked as $new_var){
   var_dump('</br>', $new_var);
   var_dump('</br>',$new_var['30_booking']->title);   
}

I think in your foreach there is mistake of word 30_booking since in your main array out put display it shows 30_booked check that also

Poonam
  • 4,591
  • 1
  • 15
  • 20