0

i'm working with an external API that provides me some information,

That information has some items that gives me normal items with one backpack, and others without.

This is an example of the json response:

With backpack

    "featured": [
        {
            "id": "CID_641_Athena_Commando_M_SweaterWeather",
            "name": "Rodolfo",
            "price": 1200,
            "image": "$$$URL$$$",
            "otherItemsDetails": [
                {
                    "name": "Guirlanda Explosiva",
                    "images": {
                        "icon": "$$$URL$$$",}
                }
            ]
        },

Without backpack

        {
            "id": "Pickaxe_ID_137_NutCracker",
            "name": "Globo de Neve",
            "price": 800,
            "image": "$$$URL$$$",
            "otherItemsDetails": []
        },

So i'm currently using foreach

$backpack = $item['otherItemsDetails'][0]['images']['icon'];

and

        <?php 
        if(isset($backpack)){
            echo "<img height='50' width='50' src='$backpack;' class='sec'>";
        }
        else{   
        }
        ?>

But it keeps giving me the "Undefined offset: 0".

Thanks!

  • 1
    How is `$item` defined? – Jeto Jan 04 '20 at 23:31
  • If I understand this correctly when otherItemsDetails is empty you should get this error. So you'll need to check the length of the array before attempting to access the the 0th element as that element is undefined if the array is empty. – hraynaud Jan 05 '20 at 00:16
  • Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Sherif Jan 05 '20 at 01:07

1 Answers1

0

Your array seems ok. Maybe the problem it's in your PHP code.

The error Undefined offset: 0 meens that your array ($item['otherItemsDetails']) haven't a item with the index 0, like your code without backpack. You can solve that problem creating a condition that verify if the index exists on your array:

if(isset($item['otherItemsDetails'][0])) {
    // code
}

You can also use hacks, like:

echo @$item['otherItemsDetails'][0];
// will return a empty string if doesn't exists.
  • 1
    Don't use the error supression operator `@`. It literally prevents you from knowing that there is an error and thus potentially buggy code. Using `isset()` to check if a variable is set is fine, but using `@` is **not**. – Sherif Jan 04 '20 at 23:48
  • I'm just showing that are many options to solve the problem. The operator `@` can be used fine in some cases, but doesn't necessarily mean that will bug the code or something like that if you use in a array that can be empty or not. Btw, I also recommend to use the condition `isset()`, but it's always good to know other options. ;) – William Carneiro Jan 04 '20 at 23:52
  • In this case, no it's not. It's actually potentially harmful to suggest the use of the error suppression operator, especially to someone who is new to PHP and does not have a strong handle on the innards of the language, **because** the error suppression operator literally turns off the error handler while handling the operation this means you will **never** know about any errors that occur as a result therein. And I will continue to down vote your answer specifically for this reason. – Sherif Jan 05 '20 at 00:23
  • Knowing there is an error and deciding to ignore it because you understand the ramifications is one thing, but making it impossible to know that an error exists is a totally different ball game. This is never advisable in any scenario. – Sherif Jan 05 '20 at 00:25
  • I'll repeat: I'm not recommending to use the `@` operator. This operator exists on PHP language and MOST be learned. Tell a person that it's new on PHP to ignore the errors NEVER was my intention on my answer, that's why I recommended first to use the condition verifying if the index exists on the array. – William Carneiro Jan 05 '20 at 00:55
  • On my answer I said exactly what will happen if the operator `@` be used on that case: return a empty string if the index doesn't exist. Your down vote will be useful just for your opinion, not for the correct answer. – William Carneiro Jan 05 '20 at 00:59
  • No, you don't. Nowhere in your answer do you explain what `@` will do or that it will disable the error handler or that it will prevent the user from finding useful error information, or that it is dangerous and should not be used. All that could actually more readily be said by just **not** mentioning it at all :) – Sherif Jan 05 '20 at 01:04
  • The use of the operator on the question case isn't harmful, please chill out! I never said that you must use the operator to prevent errors or something like that. I said exactly what can be helpful to solve the problem, and this it's important too. Thank you for complementing my answer telling what the operator can do wrong, but this don't mean that the operator NEVER MUST BE USED, otherwise the operator would never exist on the language. Learn isn't never a problem. You can do a same thing with a lot of ways, that's why I gave more than one method to solve the question problem. – William Carneiro Jan 05 '20 at 01:33
  • It is indeed harmful since it *hides* error information, which is useful and necessary for debugging. The fact that you can't understand that is what makes it all more the harming. – Sherif Jan 05 '20 at 01:46
  • I understand perfectly what the operator does. If I wanted you prevent the code errors I would have said to use `error_reporting()`, not the operator that MUST ONLY BE USED if you already KNOW what your code are doing. In that case, the array can give an index or NOT. If you need a different response to the array return, **use a condition**. But if the array can be empty (or not defined), you can use `(array)@$array` that will return the array that contains the content or a empty array if isn't defined. UNDERSTAND: there's cases that the operator it's useful. We can code in different ways. – William Carneiro Jan 05 '20 at 02:26