KingCoder11 KingCoder11 - 3 months ago 26
PHP Question

php array index giving errors

I have created a php array, and var dumped it.
In the var dump, I can see a item, but when I try to access that Item, it gives a php error. Here is the var_dump.

array(16) {
[0]=>
string(1) "3"
["id"]=>
string(1) "3"
[1]=>
string(2) "11"
["price"]=>
string(2) "11"
[2]=>
string(13) "Title"
["name"]=>
string(13) "Title"
[3]=>
string(15) "String"
["seller"]=>
string(15) "String"
[4]=>
string(1) "0"
["category"]=>
string(1) "0"
[5]=>
string(347) "String"
["description"]=>
string(347) "String"
[6]=>
string(69) "String"
["img"]=>
string(69) "String"
[7]=>
string(1) "0"
["stock"]=>
string(1) "0"
}


This piece of code is giving the error. Any help would be nice.

$array["img"]


Error Log:

[02-Jul-2017 13:40:30 America/New_York] PHP Notice: Undefined index: img in myWebPage on line 98
[02-Jul-2017 13:40:30 America/New_York] PHP Notice: Undefined index: img in myWebPage on line 100


Code:

var_dump($product);
echo $product["img"];
echo '<div class="card">
<img class="card-img-top" src="'.$product["img"].'" alt="'.$product["name"].'" style="height: 200px;">
<div class="card-block">
<h4 class="card-title">'.$product["name"].'</h4>
<a href="#" class="btn btn-primary">Buy Now</a>
</div>
</div>';
// The array is stored in product.

Answer Source

Try $array[0]["img"] , undefined index means you are not specifying the index of the element hence [0]

For future reference use php.net , here is the page for arrays http://php.net/manual/en/function.array.php