0

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.
11
  • What is name of your array(name of array varieble)? and what is the error? Commented Jul 2, 2017 at 17:39
  • What is the error? Commented Jul 2, 2017 at 17:39
  • Can you add more of the code you are using? Commented Jul 2, 2017 at 17:43
  • Try $array[0]["img"] , undefined index means you are not specifying the index of the element hence [0] Commented Jul 2, 2017 at 17:45
  • How did you create that aarray. It looks wrong where it says ["img"]=> string(69) "String" Commented Jul 2, 2017 at 17:45

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.