1

I am trying to get certain value from the stack below:

[
    {
        "o_id": "593ff39d86a43",
        "o_name": "Black",
        "o_picture": "/puma_black.jpeg",
        "o_sizes": {
            "1": {
                "o_price": "",
                "o_quantity": "7",
                "o_size": "S"
            },
            "2": {
                "o_price": "",
                "o_quantity": "4",
                "o_size": "M"
            },
            "3": {
                "o_price": "",
                "o_quantity": "5",
                "o_size": "L"
            }
        }
    },
    {
        "o_id": "593ff39d86a4d", <-- FROM THIS sub array
        "o_name": "White",
        "o_picture": "/puma_white.jpeg",
        "o_sizes": {
            "4": {
                "o_price": "",
                "o_quantity": "5",
                "o_size": "S"
            },
            "5": {
                "o_price": "",
                "o_quantity": "6", <- i need to find this
                "o_size": "M"
            }
        }
    }
]

So far by this function

$key = array_search([STACK], array_column("593ff39d86a4d", 'o_id'));

I managed to get this part:

{
"o_id": "593ff39d86a4d", <-- FROM THIS sub array
"o_name": "White",
"o_picture": "/puma_white.jpeg",
"o_sizes": {
    "4": {
        "o_price": "",
        "o_quantity": "5",
        "o_size": "S"
    },
    "5": {
        "o_price": "",
        "o_quantity": "6", <- i need to find this
        "o_size": "M"
    }
}

So i set the above to:

$sizes = $product->options[$key]['o_sizes'];

$sizes is now the array in which i need to find exactly size M and the quantity of this size. I tried to do another array_search, however i can't seem to get it. How do i achieve this from here?

Octo
  • 33
  • 9
  • array_search works only on an array, not on an object. If you use true for json_decode with its second parameter, it will turn all objects into associative arrays which might be more usefulful to you: http://php.net/json_decode. – hakre Jun 14 '17 at 10:49
  • This certainly doesn't seem like a duplicate to me, but since it's now closed, I can't submit an answer. I've pasted working code here instead: https://pastebin.com/uvMdjyuB. I hope that helps! – user94559 Jun 14 '17 at 11:04
  • @smarx that worked. thank you ever so much. – Octo Jun 14 '17 at 11:09

0 Answers0