1

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

Array (
    [0] => stdClass Object (
        [0] => 1
        [1] => o4
        [2] => Efexor
        [3] => Eklil
        [4] => 1
        [5] => 30
        [6] => 15
        [7] => Phezer
        [8] => Venlafaxine
        [9] =>
        [10] => 0
        [11] => 2020-03-25
        [12] => 2021-08-25
        [13] => 1
        [14] => 0
        [15] => 0
        [16] => caps
        [17] => ..\upload\Efexor.jpg
        [id] => 1
        [qty] => 15
        [Sale] => 1
        [name] => Efexor
        [bonus] => 0
        [price] => 30
        [company] => Phezer
        [draftid] => 1
        [discount] => 0
        [imageurl] => ..\upload\Efexor.jpg
        [owned-com] => Eklil
        [pack-size] => 0
        [codenumber] => o4
        [chemicalcom] => Venlafaxine
        [dosage-form] => caps
        [concentration] =>
        [expiration-date] => 2021-08-25
        [production-date] => 2020-03-25
 )
    [1] => stdClass Object (
        [0] => 2
        [1] =>
        [2] => Efexor
        [3] => Eklil
        [4] => 1
        [5] => 15
        [6] => 10
        [7] => Phezer
        [8] => Venlafaxine
        [9] =>
        [10] => 0
        [11] => 2020-03-25
        [12] => 2021-08-25
        [13] => 0
        [14] => 0
        [15] => 0
        [16] => caps
        [17] => ..\upload\Efexor.jpg
        [id] => 2
        [qty] => 10
        [Sale] => 0
        [name] => Efexor
        [bonus] => 0
        [price] => 15
        [company] => Phezer
        [draftid] => 1
        [discount] => 0
        [imageurl] => ..\upload\Efexor.jpg
        [owned-com] => Eklil
        [pack-size] => 0
        [codenumber] =>
        [chemicalcom] => Venlafaxine
        [dosage-form] => caps
        [concentration] =>
        [expiration-date] => 2021-08-25
        [production-date] => 2020-03-25
    )
    [2] => stdClass Object (
        [0] => 3
        [1] =>
        [2] => panadol
        [3] => com2
        [4] => 1
        [5] => 5
        [6] => 12
        [7] => GSK
        [8] => Paracetamol
        [9] =>
        [10] => 0
        [11] => 2020-04-09
        [12] => 2021-02-09
        [13] => 0
        [14] => 0
        [15] => 2
        [16] => taps
        [17] => ..\upload\panadol.png
        [id] => 3
        [qty] => 12
        [Sale] => 0
        [name] => panadol
        [bonus] => 2
        [price] => 5
        [company] => GSK
        [draftid] => 1
        [discount] => 0
        [imageurl] => ..\upload\panadol.png
        [owned-com] => com2
        [pack-size] => 0
        [codenumber] =>
        [chemicalcom] => Paracetamol
        [dosage-form] => taps
        [concentration] =>
        [expiration-date] => 2021-02-09
        [production-date] => 2020-04-09
    )
    [3] => stdClass Object (
        [0] => 4
        [1] => codenumber
        [2] => name
        [3] => owned
        [4] => 1
        [5] => 9
        [6] => 1
        [7] => gsk
        [8] => gscom
        [9] => 12mg
        [10] => 3
        [11] => 2020-03-25
        [12] => 2021-02-09
        [13] => 1
        [14] => 0
        [15] => 0
        [16] => caps
        [17] => ..\upload\panadol.png
        [id] => 4
        [qty] => 1
        [Sale] => 1
        [name] => name
        [bonus] => 0
        [price] => 9
        [company] => gsk
        [draftid] => 1
        [discount] => 0
        [imageurl] => ..\upload\panadol.png
        [owned-com] => owned
        [pack-size] => 3
        [codenumber] => codenumber
        [chemicalcom] => gscom
        [dosage-form] => caps
        [concentration] => 12mg
        [expiration-date] => 2021-02-09
        [production-date] => 2020-03-25
    )
    [4] => stdClass Object (
        [0] => 5
        [1] => d322
        [2] => fsdfds
        [3] => ewfwef
        [4] => 1
        [5] => 3232
        [6] => 222
        [7] => none
        [8] => nonoe
        [9] => nonoe
        [10] => 12
        [11] => 2020-03-25
        [12] => 2021-02-09
        [13] => 1
        [14] => 0
        [15] => 2
        [16] => caps
        [17] => ..\upload\panadol.png
        [id] => 5
        [qty] => 222
        [Sale] => 1
        [name] => fsdfds
        [bonus] => 2
        [price] => 3232
        [company] => none
        [draftid] => 1
        [discount] => 0
        [imageurl] => ..\upload\panadol.png
        [owned-com] => ewfwef
        [pack-size] => 12
        [codenumber] => d322
        [chemicalcom] => nonoe
        [dosage-form] => caps
        [concentration] => nonoe
        [expiration-date] => 2021-02-09
        [production-date] => 2020-03-25
    )
    [5] => stdClass Object (
        [0] => 6
        [1] =>
        [2] =>
        [3] =>
        [4] => 1
        [5] => 0
        [6] => 0
        [7] =>
        [8] =>
        [9] =>
        [10] => 0
        [11] => 0000-00-00
        [12] => 0000-00-00
        [13] => 0
        [14] => 0
        [15] => 0
        [16] => eyedrop
        [17] => ..\upload\panadol.png
        [id] => 6
        [qty] => 0
        [Sale] => 0
        [name] =>
        [bonus] => 0
        [price] => 0
        [company] =>
        [draftid] => 1
        [discount] => 0
        [imageurl] => ..\upload\panadol.png
        [owned-com] =>
        [pack-size] => 0
        [codenumber] =>
        [chemicalcom] =>
        [dosage-form] => eyedrop
        [concentration] =>
        [expiration-date] => 0000-00-00
        [production-date] => 0000-00-00
    )
    [6] => stdClass Object (
        [0] => 7
        [1] =>
        [2] =>
        [3] =>
        [4] => 1
        [5] => 0
        [6] => 0
        [7] =>
        [8] =>
        [9] =>
        [10] => 0
        [11] => 0000-00-00
        [12] => 0000-00-00
        [13] => 0
        [14] => 0
        [15] => 0
        [16] =>
        [17] => ..\upload\panadol.png
        [id] => 7
        [qty] => 0
        [Sale] => 0
        [name] =>
        [bonus] => 0
        [price] => 0
        [company] =>
        [draftid] => 1
        [discount] => 0
        [imageurl] => ..\upload\panadol.png
        [owned-com] =>
        [pack-size] => 0
        [codenumber] =>
        [chemicalcom] =>
        [dosage-form] =>
        [concentration] =>
        [expiration-date] => 0000-00-00
        [production-date] => 0000-00-00
    )
    [7] => stdClass Object (
        [0] => 8
        [1] =>
        [2] =>
        [3] =>
        [4] => 1
        [5] => 0
        [6] => 0
        [7] =>
        [8] =>
        [9] =>
        [10] => 0
        [11] => 0000-00-00
        [12] => 0000-00-00
        [13] => 0
        [14] => 0
        [15] => 0
        [16] => eardrop
        [17] => ..\upload\panadol.png
        [id] => 8
        [qty] => 0
        [Sale] => 0
        [name] =>
        [bonus] => 0
        [price] => 0
        [company] =>
        [draftid] => 1
        [discount] => 0
        [imageurl] => ..\upload\panadol.png
        [owned-com] =>
        [pack-size] => 0
        [codenumber] =>
        [chemicalcom] =>
        [dosage-form] => eardrop
        [concentration] =>
        [expiration-date] => 0000-00-00
        [production-date] => 0000-00-00
    )
)

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

<?PHP
 public function getPrice($id,$codenumber){
        if($this->connected === true){
            try{
                $items = $this->connection->prepare("SELECT `items` FROM `bills` WHERE id=:id");
                $items->bindParam(":id",$id);
                $items->execute();
                $fetchedItems = $items->fetchColumn();
                $items = json_decode($fetchedItems);
                foreach ($items as $item){
                    $price = $item->price;
                    echo $price; // What i want is to echo the Price-Key for the value that has the $codenumber [key]
                }
            }
            catch(PDOException $e){
                if($this->errors === true){
                    return $this->error($e->getMessage());
                }else{
                    return false;
                }
            }
        }
    }
?>

2 Answers 2

2

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

$codenumber = 'o4';
$array = array_column($array, null, 'codenumber');
echo $array[$codenumber]->price;  // outputs 30

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

echo array_column($array, null, 'codenumber')[$codenumber]->price;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, my problem solved and the code is very easy to understand.
1

I believe what you are looking for is something like this, where $id is the value you are searching for and $json is your array. If count doesn't exist, it will automatically be created with a value of 0.

$id = 100;
$items = json_decode($fetchedItems, true);
foreach($items as $k => $v){
   if(isset($json[$k]["count"]) === false){
      $json[$k]["count"] = 0;
   }

   if($v["codenumber"] == $id){
      $json[$k]["count"] += $v["price"];
   }
}

2 Comments

it doesn't work I tried to manipulate around the code but I get Fatal error: Cannot use object of type stdClass as array FYI the JSON array is decoded before looping foreach and the second code block, to be honest, I didn't understand it
@MrObscure I made a change, I added true to json_decode so it converts it into an array.

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.