1

Why does the following code:

if (isset($_GET['trainType']) && isset($_GET['onTime']) && isset($_GET['gotSeat'])) {
    $train[0]['trainType'] = $_GET['trainType'];
    $train[0]['trainType']['onTime'] = $_GET['onTime'];
    $train[0]['trainType']['gotSeat'] = $_GET['gotSeat'];   
    echo '<pre>';
    print_r($train);
    echo '</pre>';
}

Return the following array:

Array
(
    [0] => Array
        (
            [trainType] => tLine
        )

)

I had initially assumed it would return something more resembling to this:

Array
(
    [0] => Array
        (
            [trainType] => 'passenger'
            Array =>
                (
                    [onTime] => true
                    [gotSeat] => true
                )

        )

)

Any guidance on what I should do to achieve what I am trying to do? I am hoping that my code makes what I am trying to do obvious.

3
  • can you var_dump() your $_GET array? Commented Oct 23, 2012 at 5:32
  • trainType = string 'VLine' (length=5) onTime = string 'true' (length=4) gotSeat = string 'true' (length=4) Commented Oct 23, 2012 at 5:35
  • so you are trying to append to a string as though it were an array Commented Oct 23, 2012 at 5:36

2 Answers 2

1

This line will set trainType to a string value:

$train[0]['trainType'] = 'hello';

Then these lines will actually be used for character substitution, with a slight twist:

$train[0]['trainType']['onTime'] = 'foo';
$train[0]['trainType']['gotSeat'] = 'bar';

Both onTime and gotSeat will result in 0 (because you're working with a string) and will replace the first character with f then b.

Therefore print_r($train) returns:

(
    [0] => Array
        (
            [trainType] => bello
        )

)

Here is how I would format this data:

// define our list of trains
$train = array();

// create a new train
$new = new stdClass;
$new->type = 'a';
$new->onTime = 'b';
$new->gotSeat = 'c';

// add the new train to our list
$train[] = $new;

The result of print_r($trains):

Array
(
    [0] => stdClass Object
        (
            [type] => a
            [onTime] => b
            [gotSeat] => c
        )

)

Accessing this data:

echo $trains[0]->type; // returns 'a'
echo $trains[0]->onTime; // returns 'b'
echo $trains[0]->gotSeat; // returns 'c'
Sign up to request clarification or add additional context in comments.

2 Comments

That is pretty cool. I've never really worked with objects before, but this is probably what I should have been doing. Even though I could have originally done this w/ my original array, this answer has given me an idea which will improve my code in the long run so I appreciate that. One question I had however is why the contents of the array was mixed instead of ignored, like you said? (ie: VLine became TLine (replaced the first letter of VLine with the T from True (from the other $_GET vars)
I've updated my answer to solve this problem. It wasn't immediately clear until I ran a test locally.
0

You are implicitly setting (or needing) a key = 0 for

array (
  "onTime" => true,
  "gotSeat" => true
)

So you must instead just do this:

if (isset($_GET['trainType']) && isset($_GET['onTime']) && isset($_GET['gotSeat'])) {
    $train[0]['trainType'] = $_GET['trainType'];
    $train[0][0]['onTime'] = $_GET['onTime'];
    $train[0][0]['gotSeat'] = $_GET['gotSeat'];
    echo '<pre>';
    print_r($train);
    echo '</pre>';
}

Note that all I did was to change the incorrect $train[0]['trainType']['onTime'] to $train[0][0]['trainType'] in your code, and similarly for gotSeat.

Or you can define a new key, maybe like this: $train[0]['booking']['onTime'] = ...

Comments

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.