Skip to content
Advertisement

handling an Array of objects

I have a JSON array of objects each object contains a MySQL database row that also contains information about an item. my question is how can I get a certain key-value from an object depending on another key? let me get it straight, items is a JSON array contains a couple of objects each object is generated with both numeric and string keys, So what I need is to get the value of the price key that in the same Object with a specific codenumber basically getting the price value from the codenumber key the Second thing is how to add a new key within a new value to the object? like if I have a variable called $Count how can I add his value to the object within a new key like Count? is the function array_push can add a new key to the Object?

the JSON array

JavaScript

my attempt to make the first function core.php:-

JavaScript

Advertisement

Answer

Assuming that codenumber is unique in the array, just re-index your array with codenumber and then it’s simple (PHP >= 7):

JavaScript

That’s reusable multiple times. If you just need to do it once:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement