0

I am new to php and i have a small question.

This is my array

$cars=array( "1234"=> array("Toyota","100","2","white"),
            "2468"=> array("Mazda","1000","0","red"),
            "4587"=> array("Mercedes","200","0","green")
                        ); 
$_SESSION['cars']=$cars;

the elements 1234, 2468 and 4587 are basically registration numbers of the cars and my task is to insert these registration number in a table.

if( isset($_SESSION['cars']))
        {
        foreach($_SESSION['cars'] as $key)
        {?>
                <tr><td><?php echo $key?></td></tr>

this is wht i did but it gives me an error saying Notice: Array to string conversion.

can anyone tell me how to do this ? I'll be grateful

0

2 Answers 2

2

This should work for you:

if( isset($_SESSION['cars'])) {

    foreach($_SESSION['cars'] as $key => $v)
        echo "<tr><td>" . $key . "</td></tr><br />";
}

EDIT:

It should work see this example:

<?php

    session_start();

    $cars = array(
                "1234"=> array("Toyota","100","2","white"),
                "2468"=> array("Mazda","1000","0","red"),
                "4587"=> array("Mercedes","200","0","green")
            ); 

    $_SESSION['cars'] = $cars;

    if( isset($_SESSION['cars'])) {

        foreach($_SESSION['cars'] as $key => $v)
            echo "<tr><td>" . $key . "</td></tr><br />";
    }

?>

Output:

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

11 Comments

I tried that but it inserted 0 1 and 2 in the registration number column :S i am supposed to insert the whole value "1234" ..any idea wht shld b done ?
It should work. In case you omitted some code between setting and using $_SESSION['cars'], maybe it does not have the content you expect it to have. You can just easily check it out with var_dump($_SESSION['cars']);
@Rizier123 okay i am gonna check it out
@Sasha alryt i'll check that
@Sasha u r right :O..the values it self are 0 1 2 ...so why is it not storing 1234 as the name of elements of the outer array ?
|
0
<?php
        $cars=array( "1234"=> array("Toyota","100","2","white"),
            "2468"=> array("Mazda","1000","0","red"),
            "4587"=> array("Mercedes","200","0","green")
        );
        $_SESSION['cars']=$cars;
        //In separated file
        if(!isset($_SESSION)) session_start();
        if( isset($_SESSION['cars']))
        {
            foreach($_SESSION['cars'] as $key => $value)
            {
                echo "<tr><td> $key </td></tr>";
            }
        }
?>

6 Comments

Registration 0 1 2
these r the values i got after writing the above code :S
@Ashley Try my example, or can you add print_r($_SESSION['cars']);die(); in your code and show me the result?
Array ( [0] => Array ( [0] => Toyota [1] => 100 [2] => 2 [3] => white ) [1] => Array ( [0] => Mazda [1] => 1000 [2] => 0 [3] => red ) [2] => Array ( [0] => Mercedes [1] => 200 [2] => 0 [3] => green ) )
@Ashley, great! now , can you show me the code used to initialize $_SESSION['cars']?
|

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.