1

About implementing index.php, spec give an example of:

$positions = [];
foreach ($rows as $row)
{
    $stock = lookup($row["symbol"]);
    if ($stock !== false)
    {
        $positions[] = [
            "name" => $stock["name"],
            "price" => $stock["price"],
            "shares" => $row["shares"],
            "symbol" => $row["symbol"]
        ];
    }
}

But rows is a two dimensional array, while position is one. And rows is the return result of querying portfolios.

e.g. I(id=1) have two stocks, google for 10 shares and facebook for 20 shares.

The rows would be [["id" => 1, "symbol" => "GOOG", "shares" => 10], ["id" => 1, "symbol" => "FB", "shares" => 20]

Using another block of code spec provides:

<table>
    <?php

        foreach ($positions as $position)
        {
            print("<tr>");
            print("<td>{$position["symbol"]}</td>");
            print("<td>{$position["shares"]}</td>");
            print("<td>{$position["price"]}</td>");
            print("</tr>");
        }

    ?>
</table>

I get the result of:

FB 20 78.40

GOOG 10 511.10

How does position declared as position[] (one dimension) gives this result? Also, since position is only one dimension, isn't the inner most part of first block of code keep rewriting the contents of position? Sorry for this silly question.. I got really confused

2

When you call an iterative construct like:

foreach ($rows as $row)

you're creating a loop that will cicle throughout the whole iterable, in this case $rows.

Every time through the loop, you'll get one element at a time from $rows, and store it temporarily in a variable called $row.

If $rows is a 2D array, i.e., an array within an array in php, every element of it will be the inner array in each position.

Something like:

array2D
{
    array1D_1
    {
        "element1" => foo;
        "element2" => foo;
        "element3" => foo;
    }

    array1D_2
    {
        "element1" => foo;
        "element2" => foo;
        "element3" => foo;
    }

    array1D_3
    {
        "element1" => foo;
        "element2" => foo;
        "element3" => foo;
    }
}

You'd get each inner array as:

foreach ($array2D as $array1D)
{
    echo "Inner array";
    print_array($array1D);
}

Assuming you had previously defined print_array(), you'd get:

    Inner array
    {
        "element1" => foo;
        "element2" => foo;
        "element3" => foo;
    }
    Inner array
    {
        "element1" => foo;
        "element2" => foo;
        "element3" => foo;
    }
    Inner array
    {
        "element1" => foo;
        "element2" => foo;
        "element3" => foo;
    }

HTH!

| improve this answer | |
  • But I declare positions as $positions[]; When I $positions[] = [...], how can I store a entire array in it? – RexYuan Dec 19 '14 at 14:06
  • Unlike 'C', in 'php' it's not you (as a programmer) who decides the type of the variable. It's php, at runtime, who decides on it depending on what gets stored in it. Here's a nice explanation about initializing arrays with '[]' syntax. Check the "Creating/modifying with square bracket syntax" section – abelinux Dec 19 '14 at 15:17
  • I read about it. So to be sure, if I initialize an array like $a = [], then I execute $a = 0, $a = [1, 2], and $a = [1, 2, 3], the content of array $a is now [0, [1, 2], [1, 2, 3]] – RexYuan Dec 20 '14 at 5:46
  • Exactly! Watch it execute here. CodingGround is a nice resource, BTW :) Also notice this $a = [] syntax will only work on php 5.4 and greater. For older versions (or for compatibility issues, for that matter;) you must use $a = array(). – abelinux Dec 20 '14 at 14:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .