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) "[email protected]" ["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) "[email protected]" ["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!!!

6
  • please post the array declaration also. Commented Jul 31, 2013 at 9:30
  • when you var_dump $booked['30_booking'], what does it display, without foreach loop Commented Jul 31, 2013 at 9:35
  • The array declaration is done in a codeignitor model, and then in the controller, so it's a little tricky to post it above - I have put the output of the array above and the output of the sub array, just so that you can see what is being var dumped out if that's any help? Commented Jul 31, 2013 at 9:42
  • What is the output of $booked['30_booking']->title? Commented Jul 31, 2013 at 9:49
  • The var_dump output of $booked['30_booking']->title is: string(2) "Ms" Commented Jul 31, 2013 at 9:52

3 Answers 3

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) "[email protected]" 
        ["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/ .

Sign up to request clarification or add additional context in comments.

2 Comments

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!
$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.
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']);
    }
}

2 Comments

Hi, thank you for you answer, would you be able to tell me how you retrieve the value of "test" from the sub array title variable?
Check the edited answer. If total is not a object give it as $val['total']->title
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

Comments

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.