Did I screw up or am I going crazy

The following script runs a method (fetch_item_info) in a class called Item.php.

The method populates an array which I assign to $item_info. The array is populated by fetching data from a mysql table and extracting it with a while… statement via mysql_fetch_array.

The array exists and the assignment works as is demonstrated by printing out the array with print_r.

However, when I try to assign the value of an element of the array to a variable, it fails.

Here is the script:


<?php // filename.php

include_once ('_classes/Item.php');

$item = new Item;

$item_id = 84;

$item_info = array();

$item_info = $item->fetch_item_info($item_id);

echo '<pre>';
print_r ($item_info);
echo '</pre>';

echo '<br/><br/>File name is: ';
echo $item_info['item_image'];

$image = $item_info['item_image'];

echo "<br/><br/>\\$image = $image";

?>

Here is the output:


Array
(
    [0] => Array
        (
            [item_id] => 84
            [title] => asdf
            [item_image] => figure_2_8x8_72dpi.jpg
            [creator_id] => 2
            [status] => Active
            [media] => dfgh
            [height] => 8
            [width] => 8
            [price] => 7000
            [description] => qwerghjkl
            [comments] => fghjklghj
        )

)


File name is:

$image =

I know my syntax isn’t wrong as the following demonstrates:

The alternative Script:


<?php // filename.php

$item_info['item_image'] = '546728.jpg';
$item_info['image_name'] = 'Some Name';

echo '<pre>';
print_r ($item_info);
echo '</pre>';

echo '<br/><br/>File name is: ';
echo $item_info['item_image'];

$image = $item_info['item_image'];

echo "<br/><br/>\\$image = $image";

?>

The output:


Array
(
    [item_image] => 546728.jpg
    [image_name] => Some Name
)


File name is: 546728.jpg

$image = 546728.jpg

I have actually had similar issues before and solved them eventually by simply re-typing the code (maybe there was some invisible character somewhere) or discovering I have used a reserved word. I have tried re-typing this a number of times and I don’t think I am using any reserved words.

Can anyone help me figure out what is going on???

–Kenoli

echo '<br/><br/>File name is: ';
echo $item_info[0]['item_image'];

You forgot the [0], as indicated by your print_r

OMG. Thanks. The problem came in my method which I assigned like this:

$items = $row

It created a nested array. I took out the brackets and all works!

It is amazing sometimes to realize how I can’t see what is in front of my eyes.

–Kenoli