2

I want to compare a value of data to a list of element which I had retrieved from php array(decoded json).

First,

This is the first array:

Array1
(
[0] => Array
    (
        [member_id] => 3
        [member_card_num] => 2013011192330791
        [member_barcode] => 2300067628912
    )

[1] => Array
    (
        [member_id] => 4
        [member_card_num] => 2328482492740000
        [member_barcode] => 3545637000
    )

[2] => Array
    (
        [member_id] => 2
        [member_card_num] => 40001974318
        [member_barcode] => 486126
    )

[3] => Array
    (
        [member_id] => 1
        [member_card_num] => 91001310000057698
        [member_barcode] => 000057698
    )

)

This is the second Array:

Array2
(

[0] => Array
    (
        [member_id] => 2
        [member_card_num] => 40001974318
        [member_barcode] => 486126
    )

)

Second,

I had retrieved the (member_barcode) which I required.

Here is the code:

For Array1:

 foreach ($decode1 as $d){

        $merchant_barcode = $d ['member_barcode']; 
        echo  $merchant_barcode;

     }  

For Array2:

 foreach ($decode2 as $d2){
      $user_barcode = $d2 ['member_barcode'];
      echo $user_barcode;
     }

Then, I get this output():

For Array1(merchant_barcode):

2300067628912
3545637000
486126
000057698

For Array2(user_barcode):

486126

The question is, I would to check and compare whether the user_barcode in Array2(486126) is exist/match to one of the merchant_barcode in Array1.

This is my code, but it only compare the user_barcode in Array2 to the last element(000057698) in Array1,

I want it to loop through each n check one by one. How can I do that?

public function actionCompareBarcode($user_barcode, $merchant_barcode){

      if(($user_barcode) == ($merchant_barcode)){
            echo "barcode exist. ";

        }

     else{

         echo "barcode not exist";
     }
  }

In this case, the output I get is "barcode not exist", but it should be "barcode exist". Anyone can help? Appreciate that. Im kinda new to php.

5 Answers 5

1

You could use a nested loop like:

foreach ($decode2 as $d2)
{
      $user_barcode = $d2 ['member_barcode'];
      foreach ($decode1 as $d)
      {
        $merchant_barcode = $d ['member_barcode']; 
        if ($merchant_barcode == $user_barcode)
        {
            echo "Match found!";
        }
        else
        {
            echo "No match found!";
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
<?

$a = array(
        array(
                'member_id' => 3,
                'member_card_num' => '2013011192330791',
                'member_barcode' => '2300067628912',
        ),
        array(
                'member_id' => 4,
                'member_card_num' => '2328482492740000',
                'member_barcode' => '3545637000',
        ),
        array(
                'member_id' => 2,
                'member_card_num' => '40001974318',
                'member_barcode' => '486126',
        ),
        array(
                'member_id' => 1,
                'member_card_num' => '91001310000057698',
                'member_barcode' => '000057698',
        )
);

$b = array(
        array(
                'member_id' => 2,
                'member_card_num' => '40001974318',
                'member_barcode' => '486126',
        )
);


array_walk($a, function($item) use($b) {
    echo ($b['0']['member_barcode'] == $item['member_barcode'] ? "found" : NULL);
});

?>

Comments

1

I'd use array_uintersect() to calculate if those multidimensional arrays have a common element:

<?php
$a = array(
  array(
    'member_id' => '3',
    'member_card_num' => '2013011192330791',
    'member_barcode' => '2300067628912',
  ),
  array(
    'member_id' => '2',
    'member_card_num' => '40001974318',
    'member_barcode' => '486126',
  )
);

$b = array(
  array(
    'member_id' => '2',
    'member_card_num' => '40001974318',
    'member_barcode' => '486126',
  )
);

$match = array_uintersect($a, $b, function($valueA, $valueB) {
    return strcasecmp($valueA['member_barcode'], $valueB['member_barcode']);
});

print_r($match);

Comments

0

Try calling that method as you are looping through Array1 and comparing the user_barcode to every value

Comments

0

You can compare two array this way too:

$per_arr = array();
$permissions = array()
foreach ($per_arr as $key => $perms) {
    if(isset($permissions[$key]['name'])){
        echo  $per_arr[$key]['name']; //matched data
    }else{
        echo $per_arr[$key]['name']; //not matched data
    }
}

Comments

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.