Ok, I'm at pset7 (C$50 Finance), more specifically trying to implement sell.php. I have created a template, sell_form.php, where users can select the symbol of the stock they want to sell and submitted. This symbol is posted to the controller sell.php, and it can be accessed at $_POST["symbol"]. Now what I want to do is to create a variable called $price where i can store the price of the given stock. I want to access the price that is present in the array "positions":

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

is there a way to access only the price of the symbol? If I do the following:

$price = positions[]["price"];

I'm pretty sure I'll get the prices of all the stocks owned by the user. In R accessing data it's pretty easy. All I would have to do is something like

price = positions$price[, symbol == "StockForSale"]

(Actually I'm a little bit rusty with R, so it's possible there is some syntax error, but you get the idea) Is there anything similar in php?

2 Answers 2


Just like _POST your positions is an associative array. Just as you get $_POST["symbol"] you get $positions["price"].

If I understood correctly you want to do something like the following


    $values = [
        "name" => "GOOG",
        "price" => 3.5

    printf("Name: %s\nPrice: %f\n", $values["name"], $values["price"]);



Name: GOOG
Price: 3.500000

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

  • Well, the thing is that what I really want to find is the number of the row in which I can find the positions["symbol"] that users select on sell_form.php. Once I get that number, I can easily retrieve the price by doing positions[number]["price"]. Suppose that the symbol that users select is GOOG, all I want to know is in which row GOOG is stored on positions[]
    – Cyborg101
    Aug 27, 2015 at 20:24

Ok guys, I don't think there is a straightforward way to accomplish what I wanted to do. Instead, I think the best method is to use a for each loop. With the following code I was able to access the price for the symbol that was submitted in $_POST["symbol"] and store it in the variable $price:

$price = 0;
foreach($positions as $position)
   if ($position["symbol"] == $_POST["symbol"])
       $price = $position["price"];

Hope that will help someone

You must log in to answer this question.

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