简体   繁体   中英

how to access 2d array values?

do you have any idea why the "echo" in the following loop doesn't work?

while( $nl = mysql_fetch_array($Lresult) )
{
    $clkword[$i] = $nl['Word'];
    $relatedlinks[$i] = array(
        $i => array(
            "CWord" => $nl['Word'],
            "RLinks" => $nl['Link_Add']
        )
    );
    echo $relatedlinks[$i]['CWord'];
    $i++;
}

Because the way you've set it up, $relatedlinks[$i] is an array containing (at the key $i ) an array containing keys "CWord" and "RLinks" . In other words, you have an array inside an array inside an array, whereas what you wanted was an array inside an array. Change the line

$relatedlinks[$i]=array($i => array("CWord" => $nl['Word'],
                                    "RLinks" => $nl['Link_Add']));

to read

$relatedlinks[$i] = array( "CWord"  => $nl['Word'],
                           "RLinks" => $nl['Link_Add']
                           );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM