penitent_tangent
penitent_tangent

Reputation: 772

PHP foreach not behaving as expected

I have an array (POSTed from a Python application) called "observations". It looks like this:

Array
(
[0] => Array
    (
        ['remote_id'] => 1
        ['dimension_id'] => 1
        ['metric'] => 1
    )

[1] => Array
    (
        ['remote_id'] => 1
        ['dimension'] => 2
        ['metric'] => 2
    )

[2] => Array
    (
        ['remote_id'] => 1
        ['dimension_id'] => 3
        ['metric'] => 3
    )
 (etc)

I want to iterate through all those instances of remote_id, dimension_id and metric and write them to a database. But I can't access them - here's my PHP:

foreach ($_POST["observations"] as $observation) {
echo "Let's try and access the whole array... \n";
print_r ($observation);
echo "But what about the sub elements? \n";
print_r ($observation[0]);
print_r ($observation['dimension_id']);
}

This returns:

Let's try and access the whole array... 
Array
(
    ['remote_id'] => 1
    ['dimension_id'] => 1
    ['metric'] => 1
)
But what about the sub elements? 
Let's try and access the whole array... 
Array
(
    ['remote_id'] => 1
    ['dimension'] => 2
    ['metric'] => 2
)
But what about the sub elements? 
(etc)

So my print_r ($observation[0]) and print_r ($observation['dimension_id']) are both failing to access the appropriate sub-elements and returning empty. What am I missing here?

Edit: a few questions about my (potentially malformed) POST. Doing it in Python like so:

data = urllib.urlencode([
       ("observations[0]['remote_id']", 1),
       ("observations[0]['dimension_id']", 1),
       ("observations[0]['metric']",metric1),

       ("observations[1]['remote_id']", 1),
       ("observations[1]['dimension']", 2),
       ("observations[1]['metric']", metric2),

       ("observations[2]['remote_id']", 1),
       ("observations[2]['dimension_id']", 3),
       ("observations[2]['metric']",metric3),

       ("observations[3]['remote_id']", 1),
       ("observations[3]['dimension_id']", 4),
       ("observations[3]['metric']",metric4),

       ("observations[4]['remote_id']", 1),
       ("observations[4]['dimension_id']", 5),
       ("observations[4]['metric']",metric5),
    ])
    response = urllib2.urlopen(url=url, data=data)

Upvotes: 1

Views: 61

Answers (1)

S.Pols
S.Pols

Reputation: 3434

This works according to your given array:

$array = Array(
        0 => Array
        (
            'remote_id' => 1,
            'dimension_id' => 1,
            'metric' => 1
        ),
        1 => Array
            (
                'remote_id' => 1,
                'dimension_id' => 2,
                'metric' => 2
            ),
        2 => Array
            (
                'remote_id' => 1,
                'dimension_id' => 3,
                'metric' => 3
            )
);  
foreach ($array as $observation) {

    echo "Remote id: ". $observation['remote_id']."<br />";
    echo "Dimension id: ". $observation['remote_id']."<br />";
    echo "Metric: ". $observation['metric']."<br />";
}

That will print:

Remote id: 1
Dimension id: 1
Metric: 1
Remote id: 1
Dimension id: 1
Metric: 2
Remote id: 1
Dimension id: 1
Metric: 3

But it looks like your $_POST["observations"] is not an array of $observation's but just one $observation.

There is probably something wrong in your form. Did you use arrays in your input like <input type="text" name="observations[0]['metric']" />?

Upvotes: 1

Related Questions