-3
[
 {
 "pid": "1",
 "pname": "Burger",
 "price": "20",
 "qnty": "1",
 "pimg": "1.jpg"
 },
 {
 "pid": "2",
 "pname": "Cheese burger",
 "price": "30",
 "qnty": "1",
 "pimg": "2.jpg"
 }
]

I have an array like above. how to check if array has particular "pid".

for an example if array has "pid" 1 then display view button otherwise display add to cart button

Maulik
  • 87
  • 7
  • Does this answer your question? [PHP - find entry by object property from an array of objects](https://stackoverflow.com/questions/4742903/php-find-entry-by-object-property-from-an-array-of-objects) – Jenne Jun 12 '20 at 08:11

4 Answers4

1
foreach ($array as $item){
   if($item->pid == 1) {
       //do some work...
   }
}
lmboom
  • 189
  • 4
0

You can take benefit of isset() & !empty()

foreach($lists as $list) {
    if(isset($list['pid']) && (!empty($list['pid']))) { // this will check if a key exists. If yes, then check for it's value. If value is there, then true
        // pid exists and has a value
    } else {
    }
}
Digvijay
  • 7,836
  • 3
  • 32
  • 53
0
$array = [
 {
 "pid": "1",
 "pname": "Burger",
 "price": "20",
 "qnty": "1",
 "pimg": "1.jpg"
 },
 {
 "pid": "2",
 "pname": "Cheese burger",
 "price": "30",
 "qnty": "1",
 "pimg": "2.jpg"
 }
]

You can use isset fucntion.

$key = 1;
$count =0 ;

foreach($array as $a) {
    if(isset(array_search($key,$a))){
       //your logic to execute if the $key is there in the array
       $count ++;
   }
 }
  if($count==0){
      //add to cart logic
  }
Hirumina
  • 738
  • 1
  • 9
  • 23
  • Your second part of code is not a valid one its totally wrong. – Kunal Raut Jun 12 '20 at 05:28
  • 1
    You cannot validate the key by `empty()` .See the https://www.php.net/manual/en/function.empty.php – Kunal Raut Jun 12 '20 at 05:32
  • 1
    Because we do not have assignment operator in the array. – Kunal Raut Jun 12 '20 at 05:32
  • i want to check if array has particular "pid" and if yes then display "view button" because pid is already in array and if not exists in array then display " add to cart button" but here if pid is not exists then "add to cart button" displays as much as keys available in array. if pid is exists then it display view button which is ok – Maulik Jun 12 '20 at 06:03
  • @Maulik what i have done here is assigning the value that you want to find in the array which is assigned to `pid` and loop through the array to find if the value is assigned to pid in the loop using `array_search`. – Hirumina Jun 12 '20 at 06:33
  • @Maulik do you want to add those buttons according to the data that you have in each sub array or just one buton using the main array – Hirumina Jun 12 '20 at 06:35
  • suppose i have 3 product with 3 individual page. if i go to page no.1 then there should be add to cart button if that product is not in array. if that product is already in array then hide add to cart and show view cart button. as per your code if product is not in cart then i am seeing more than 1 buttons( total buttons is equal to products in array ) of add to cart because of foreach loop. i just want vice versa of add to cart and view cart – Maulik Jun 12 '20 at 06:43
  • can you add your code to the question ( button handling part) – Hirumina Jun 12 '20 at 06:47
  • @Maulik i think i just realized what you really want.And i have updated the answer. Check if this resolves your issue. – Hirumina Jun 12 '20 at 06:54
0

You need to iterate array using loop and then check the value against any index of array

foreach ($items as $item){
   if($item->pid == 1) {
       // do whatever you want to do
   }
}
Harris Khan
  • 247
  • 10
  • 26