1

I'm trying to check value on my cart in my codeigniter project, i have this array:

$cart = Array
(
    [547650d3bfb9bce7ea6d61faf312fa41] => Array
        (
            [id] => 1502369193000
            [productid] => Product 1
            [qty] => 1
            [type] => sales
            [user_id] => admin
            [rowid] => 547650d3bfb9bce7ea6d61faf312fa41
        )

    [7473a2e4d2e4150c7de11d201538e179] => Array
        (
            [id] => 1502369241000
            [productid] => SG KENNA
            [qty] => 1
            [type] => purchase
            [user_id] => admin
            [rowid] => 7473a2e4d2e4150c7de11d201538e179
        )    
)

What i need just, check inside array user_id = 'admin' and type = 'sales' are exist.

This is what i try but with no success

$type = array_search('sales', array_column($cart, 'type'));
$user_id = array_search('admin', array_column($cart, 'user_id'));
if($type AND $user_id){
  echo "exist";
} else {
  echo "no exist";
}

Update: I use a loop like this and still no success

foreach ($cart as $item) {
      if ($item['type'] == "sales" AND $item['user_id'] == "admin") {
        echo "exist";
      } else {
        echo "no exist";
      }
    }
Andhika R.K.
  • 426
  • 1
  • 11
  • 30

7 Answers7

0

Try using in_array inside a loop:

$cars = array
  (
  array("Volvo",22,18),
  array("BMW",15,13),
  array("Saab",5,2),
  array("Land Rover",17,15)
  );

foreach ($cars as $key => $value) {

 if (in_array("Volvo", $value, TRUE))
  {
  echo "Match found<br>";
  break;

  }
else
  {
  echo "Match not found<br>";
  } 
}
Rajan
  • 2,427
  • 10
  • 51
  • 111
0

Since you need to do two subsequent checks, it's better if you filter the array and check the result's count. Here's an example of doing this by using an anonymous (lambda) function:

Given that your array is $arr:

$results = array_filter( $arr, function ($entry) {
    return $entry['type'] === 'sales' && $entry['user_id'] === 'admin';
});

then you can check your $results array echo count($results) ? 'exists' : 'does not exist';

Edit:

You can also check for isset($entry['type']) && isset($entry['user_id']) for extra safety in the closure

Fotis
  • 1,322
  • 16
  • 30
0

You can use the below code, which will return an array of results that match the given conditions. Of course, you can return an entire array or some value or whatever you want. You can change and tweak it to your needs:

<?php
$array = array(
    '547650d3bfb9bce7ea6d61faf312fa41' => array
    (
        'id' => 1502369193000,
        'productid' => 'Product 1',
        'qty' => 1,
        'type' => 'sales',
        'user_id' => 'admin',
        'rowid' => '547650d3bfb9bce7ea6d61faf312fa41'
    ),
    '7473a2e4d2e4150c7de11d201538e179' => array
    (
        'id' => 1502369241000,
        'productid' => 'SG KENNA',
        'qty' => 1,
        'type' => 'purchase',
        'user_id' => 'admin',
        'rowid' => '7473a2e4d2e4150c7de11d201538e179'
    )
);

function searchInArray($array) {
   $returnArray = [];
   foreach ($array as $key => $val) {
       if ($val['user_id'] === 'admin' && $val['type'] === 'sales') {
           $returnArray[$key] = $array[$key];
       }
   }
   return $returnArray;
}

print_r(searchInArray($array));
if(isset($array) && !empty($array)) {
     echo 'Found';
} else {
     echo 'None found';
}
?>
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
0

you can check this using for loop , it will check your array data that user_id and type is match or not if match then $found true and break and if you want to check one of two is ok then use || condition

<?php
$cart  = array(
    '547650d3bfb9bce7ea6d61faf312fa41' => array
    (
        'id' => 1502369193000,
        'productid' => 'Product 1',
        'qty' => 1,
        'type' => 'sales',
        'user_id' => 'admin',
        'rowid' => '547650d3bfb9bce7ea6d61faf312fa41'
    ),
    '7473a2e4d2e4150c7de11d201538e179' => array
    (
        'id' => 1502369241000,
        'productid' => 'SG KENNA',
        'qty' => 1,
        'type' => 'purchase',
        'user_id' => 'admin',
        'rowid' => '7473a2e4d2e4150c7de11d201538e179'
    )
);

$found = false;
foreach ($cart as $key => $data) {
    if ($data['user_id'] == 'admin' && $data['type'] == 'sales') {

        $found = true;

    }
}

if ($found === false) {
  echo 'not found';
} else {
    echo 'found';
}

Output is : Found

hope it will help you

Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43
0

Try Out This.

    $Your_user_id = searchArrayKeyVal("user_id", 'admin', $YourMultidimensionalArray);
    $Your_type = searchArrayKeyVal("type", 'sales', $YourMultidimensionalArray);
    if ($Your_user_id!==false) {
        echo "Found user_id";
    } else {
        echo "Not Found user_id";
    }

    if ($Your_type!==false) {
        echo "Found type";
    } else {
        echo "Not Found type";
    }
Roshan Padole
  • 390
  • 4
  • 11
-2

Use this code to get the values

echo $arrayname['547650d3bfb9bce7ea6d61faf312fa41']['user_id'];

echo arrayname['547650d3bfb9bce7ea6d61faf312fa41']['type'];
-2

You should try something like this:

foreach($cart as $row) {
    if(!empty($row) && !empty($row['type']) && !empty($row['user_id'])) {
        if ($row['type'] == 'sales' && $row['user_id'] == 'admin'){
            echo "exist";
            $found = true;
            break;
        }
    }
} 
if(!$found){
    echo "no exist";
}
perodriguezl
  • 430
  • 3
  • 13