2

I am confused about why I can't print the content of $positions using the code below.

$rows = query("SELECT * FROM protfolio WHERE id = ?", $_SESSION["id"]);

$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"] 
      ];
    }
}   

$_SESSION["positions"] = $positions;
print_r($positions]);
render("portfolio.php", ["positions" => $positions, "title" => "Portfolio"]);

What does $positions = [] mean? Does it declare a 2d array?

2
  • Have you tried var_dump() instead of print_r? Commented Aug 21, 2014 at 8:07
  • I try it after you told me. It said it is empty. But the positions is declared out of function, so it should be existed. @Luke Van In
    – far2go
    Commented Aug 22, 2014 at 2:02

1 Answer 1

1

why I can't print then content of $position?

Because you don't have a variable named $position in context. Rather, you have a variable named $positions.

what does the $positions = [] mean, declare a 2d array?

Actually, it just mean "declare an associative array named $positions". Arrays in PHP are a little bit different from arrays in C since PHP is loosely-typed. You don't need to explicitly say "I need this to be a 2D-array". You just declare it as an array, then you can set its keys to have arrays associated with them if you want to.

16
  • Sorry about that I mean $positions, I made a typo. If I want a array I just need $arrayname, but what does $arrayname = [ ] mean? And could you see the comments, please tell me why.
    – far2go
    Commented Aug 21, 2014 at 9:13
  • If I use $arrayname; I declare a common array, we can use index to access it like $arrayname[0], but declare an empty associate array is use $arrayname = [] and use $arrayname["index"] to access the member. Are that correct?
    – far2go
    Commented Aug 21, 2014 at 9:20
  • 1
    @far2go actually, if you just used $arrayname = someValue; this will be a regular variable. If you used $arrayname = []; this will declare an associative array. You can access the elements of this array either by using numeric indexes (e.g., $arrayname[0]) or by using the name of the keys in that array. For example, $arrayname[] = ["name" => "Kareem"];, I could use $arrayname["name"] to access the value "Kareem".
    – kzidane
    Commented Aug 21, 2014 at 9:33
  • 1. So the difference between common array and associate array is we can use another token instead of numerical index to get the value, right?. 2. And I think because PHP is loosely typed, the $varriablename can contain every type. We can use $a, why $a = []. I consider $a can become associate array if you give it the parameters.
    – far2go
    Commented Aug 21, 2014 at 9:43
  • 1
    @far2go not exactly. An array in PHP, as this manual suggests, is an ordered map that associates keys to values. And yes, you can initialize $a with any type of data since PHP is loosely-typed. And there are many ways of declaring and initializing an array in PHP and this is just one of them.
    – kzidane
    Commented Aug 21, 2014 at 9:55

You must log in to answer this question.

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