0

I have finished my portfolio.php and index.php. However, when I try and load the page, I get the error:

Illegal string offset 'symbol' in /home/jharvard/vhosts/pset7/templates/portfolio.php on line 11

When trying to debug, I used dump($positions) in index.php and the array appears to be correct.

In portfolio.php, this is my code which is giving the error (specifically the printing of $position["symbol"]:

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

Any guidance would be appreciated. I've spent hours trying to figure this out. Thank you.

3
  • can you paste here what dump give you back? It looks like you are accesing the value 'symbol' in a non-array value
    – Dazag
    Commented May 7, 2015 at 22:39
  • Array ( [name] => Google Inc. [price] => 530.70 [shares] => 100 [symbol] => goog )
    – ytma
    Commented May 8, 2015 at 0:27
  • 3
    Possible duplicate of PSET7: Extracting strings from queries with PHP Commented Feb 21, 2017 at 12:29

1 Answer 1

0

So yes, it's what I told you in the comments. I think you are missunderstanding the way foreach() is working. E.g.

$foo = ['car' => 'audi', 'color' => 'red', 'wheels' => 5];
foreach ($foo as $value) {
    echo ' ' . $value;
}
//it prints: audi red 5

foreach ($foo as $index => $value) {
    echo $index . ' => ' . $value . ', ';
}
//it will print: car => audi, color => red, wheels => 5

Here you can find the documentation, examples and explanations. http://es1.php.net/manual/en/control-structures.foreach.php

You must log in to answer this question.

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