Fatal error: Cannot use [] for reading in

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jone kent
Forum Newbie
Posts: 6
Joined: Sun Nov 20, 2011 2:44 am

Fatal error: Cannot use [] for reading in

Post by jone kent »

Hello i am new here also new in php

i have problem with my code

Code: Select all

    $seller_id = $listings[]['Account_ID'];
    $seller_info = $rlAccount -> getProfileInfo( $seller_id );
    $rlSmarty -> assign_by_ref( 'seller_info', $seller_info ); 
i get this error Fatal error: Cannot use [] for reading in

the array is

Code: Select all

    Array (
    [0] => Array
    (
    [name] => Automobiles
    [ID] => 1
    [Account_ID] => 0
    )
    [1] => Array
    (
    [name] => Automobiles
    [ID] => 2
    [Account_ID] => 1
    )
    )
    .
    .
    . 
Please help how i can fix it
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Fatal error: Cannot use [] for reading in

Post by twinedev »

You must provide an index for every level of an array, the only time you can use [] is when you are adding an item to an array, then it will auto use the next number index available.

you will need to provice the index for the item you are wanting

Code: Select all

$seller_id = $listings[0]['Account_ID'];
-Greg
jone kent
Forum Newbie
Posts: 6
Joined: Sun Nov 20, 2011 2:44 am

Re: Fatal error: Cannot use [] for reading in

Post by jone kent »

i tried this one but it's not work for me, its give me listing number 0
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Fatal error: Cannot use [] for reading in

Post by McInfo »

For an aide to my example, here is an array of names grouped by street and house numbers.

Code: Select all

$occupants_by_address = array (
    '30th Street' => array (
        'Number 13' => 'Bill Jones',
        'Number 25' => 'Portia Caine',
    ),
    '42nd Street' => array (
        'Number 13' => 'Amanda Howard',
        'Number 7' => 'Bryce Hardin',
    ),
);
You can ask the question "Who lives on 30th Street at Number 13?" In code, the question would be represented as

Code: Select all

echo $occupants_by_address['30th Street']['Number 13']; // Bill Jones
The answer would be Bill Jones.

However, the question "Who lives at Number 13?" is unanswerable because no street was specified.

Code: Select all

echo $occupants_by_address[]['Number 13']; // Wrong - no street specified
Both Bill Jones and Amanda Howard live at Number 13, but it's not the same Number 13 because the streets are different.

If you want a better answer, you will have to show us more code or further explain what your code is supposed to do.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Fatal error: Cannot use [] for reading in

Post by twinedev »

jone kent wrote:i tried this one but it's not work for me, its give me listing number 0
Exactly, the sample I gave specified the index 0. You didn't say which one you wanted, so I just gave an example. Change the 0 to the one you want.
jone kent
Forum Newbie
Posts: 6
Joined: Sun Nov 20, 2011 2:44 am

Re: Fatal error: Cannot use [] for reading in

Post by jone kent »

Ok i attached 2 files for this code, what i want is to get listing with seller name and seller image for each listing
Attachments
untitled.zip
(3.34 KiB) Downloaded 101 times
jone kent
Forum Newbie
Posts: 6
Joined: Sun Nov 20, 2011 2:44 am

Re: Fatal error: Cannot use [] for reading in

Post by jone kent »

can anyone help me???
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Fatal error: Cannot use [] for reading in

Post by pickle »

Your question has been answered. You cannot use [] for reading - its a write operator. If you want to read, you have to provide an index. While [] and, say, [0] may look similar, they do completely different things.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Fatal error: Cannot use [] for reading in

Post by twinedev »

Code: Select all

$strInfo = '';
foreach($listings as $aryItem) {
    $seller_info = $rlAccount->getProfileInfo($aryItem['Account_ID']);
    // You will need to code this for how you want to string them togeher...
    $strInfo .= $seller_info . "<br>\n";
}
$rlSmarty -> assign_by_ref( 'seller_info', $strInfo ); 
jone kent
Forum Newbie
Posts: 6
Joined: Sun Nov 20, 2011 2:44 am

Re: Fatal error: Cannot use [] for reading in

Post by jone kent »

twinedev wrote:

Code: Select all

$strInfo = '';
foreach($listings as $aryItem) {
    $seller_info = $rlAccount->getProfileInfo($aryItem['Account_ID']);
    // You will need to code this for how you want to string them togeher...
    $strInfo .= $seller_info . "<br>\n";
}
$rlSmarty -> assign_by_ref( 'seller_info', $strInfo ); 
Sorry it not working :(
Post Reply