Problem with FOREACH - getting errors

I am debugging a form processing function and and I am getting errors that I do not understand.

The form returns a 2 dimensional array. The first key is the index number of a photograph. The nested keys are the alt_text and the caption.

I added this code at the beginning of the function:


function updatephotos($dbh, $x) {

 $rows = $x['photos'];
 print_r($rows);

  foreach( $rows as $r ) {
      print_r($r);
      echo $r['alt_text'];
      echo $r['caption'];
  }  

The two echos are causing errors to be raised.


[Wed Dec 21 06:51:56 2011] [error] [client 192.168.1.77] PHP Notice:  Undefined index: alt_text in /big0/www/roissy.ca/include/cpprocessforms.php on line 172, referer: http://www.dawsonsphotographytest.com/cp2.php
[Wed Dec 21 06:51:56 2011] [error] [client 192.168.1.77] PHP Notice:  Undefined index: caption in /big0/www/roissy.ca/include/cpprocessforms.php on line 173, referer: http://www.dawsonsphotographytest.com/cp2.php
[Wed Dec 21 06:51:56 2011] [error] [client 192.168.1.77] PHP Notice:  Undefined index: alt_text in /big0/www/roissy.ca/include/cpprocessforms.php on line 172, referer: http://www.dawsonsphotographytest.com/cp2.php
[Wed Dec 21 06:51:56 2011] [error] [client 192.168.1.77] PHP Notice:  Undefined index: caption in /big0/www/roissy.ca/include/cpprocessforms.php on line 173, referer: http://www.dawsonsphotographytest.com/cp2.php

but the print_r calls return:


 Array
(
    [1] => Array
        (
            ['alt_text'] => Lucy
            ['caption'] => aaa
        )

    [2] => Array
        (
            ['alt_text'] => Lucy
            ['caption'] => bbb
        )

)
Array
(
    ['alt_text'] => Lucy
    ['caption'] => aaa
)
Array
(
    ['alt_text'] => Lucy
    ['caption'] => bbb

Can someone see what I am doing wrong?

Many thanks

Try this:

foreach ($rows AS $key => $value) echo "$key = $value<br>";

it will show you what is in each key value pairs.

Thank you. But I still do not understand the error I get.

I have this code:

$rows = $x[‘photos’];
/* print_r($rows); */

foreach ($rows AS $key => $r) {

echo “$key = $r<br>”;
echo $r[‘alt_text’] . “<br>”; <<<< raises an error. index ‘alt_text’ unknown
echo $r[‘caption’] . “<br>”; <<<< raises an error. index ‘caption’ unknown

foreach( $r AS $k => $v) {

  echo "$k = $v&lt;br&gt;";
  echo $r[$k] . "&lt;br&gt;";
  }

}

But the output of the echos suggests that the indexes should be ok

1 = Array

‘alt_text’ = Lucy
Lucy
‘caption’ = aaa
aaa
2 = Array

‘alt_text’ = Lucy
Lucy
‘caption’ = bbb
bbb

The blank rows are the echo errors.

Why do the explicit keys not work?

Thanks

I had to test this out to ensure I was seeing it right, and I was…

The reason you’re getting the error is because the key is not alt_text or caption, it’s ‘alt_text’ or ‘caption’- it’s including the single quotes. The print_r is what tipped me off to it.

To test it, try this and look at the values in the brackets:
$a = array(“alt_text” => “test alt text”, “caption” => = “test caption”);
print_r($a);
$b = array(“‘alt_text’” => “test alt text”, “‘caption’” => = “test caption”);
print_r($b);

I do not understand what you mean by “broken up”.

Can you expand on this?

My goal is to be able to use ‘alt_text’ and ‘captions’ an array indexes for clarity in the code.

I can change the initial form if it will help.

Sorry - you caught me mid-edit. I didn’t like the response, and did some further investigation, and changed the post. Please read post #4 again and see if that helps you any…

Thank you so much!

I removed the quotes in the form and now everything works beautifully!

Glad you got it working.