Home » Php » PHP foreach array and compare with GET

PHP foreach array and compare with GET

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to resolve my problem.

  1. I store array in json file
  2. I try to foreach array and compare it with my GET arguments

Problem was that only last of array filed works but other not.

auth.json

[{"License":"21X2-214X-SSDF-3215-SFFA","IP":"111111111","Note":"x1"},{"License":"S31S-SAF3-XF22-SLLT-341D","IP":"1111111","Note":"x2"}]

PHP Code

$license = json_decode(file_get_contents('auth.json'), true);
foreach ($license as $cec) {

        if ($_GET["license"] == $cec["License"] AND $_GET["IP"] == $cec["IP"]){

            $user_auth = array(
            "user_info" => array( 

                "auth" => 1
                ),
            );

        }else{
            $user_auth = array( 
            "user_info" => array( 

                "auth" => 0

                ),
            );
        } 

    }
How to&Answers:

Thats because it overrides the results. It works fine you just want a break point when it finds someone authorized.

foreach ($license as $cec) {

        if ($_GET["license"] == $cec["License"] AND $_GET["IP"] == $cec["IP"]){

            $user_auth = array(
                "user_info" => array(

                    "auth" => 1
                ),
            );

            break;

        }else{
            $user_auth = array(
                "user_info" => array(

                    "auth" => 0

                ),
            );
        }

}

Answer:

You can use in_array() function to check if a value exists in an array:

$json = '[{"License":"21X2-214X-SSDF-3215-SFFA","IP":"111111111","Note":"x1"},{"License":"S31S-SAF3-XF22-SLLT-341D","IP":"1111111","Note":"x2"}]';
$array = json_decode($json, true);
$test_data = array("21X2-214X-SSDF-3215-SFFA","111111111");//This is you test GET request
$test_data2 = array("S31S-SAF3-XF22-SLLT-341D", "1111111");//This is you test 2 GET request


foreach( $array as $cer){
    if(in_array($test_data[0], $cer) && in_array($test_data[1], $cer)){
        echo "Pair 1 exist";
    } else if (in_array($test_data2[0], $cer) && in_array($test_data2[1], $cer)){
        echo "Pair 2 exist";
    } else {
        echo "No match found";
    }
}

Output:

Pair 1 exist
Pair 2 exist

Answer:

Having the if/else within your loop, can lead to clobbering of $user_auth.
Given your example, loop through for your ‘needle’, and if found set a flag. Then later use that flag to construt your $user_auth array.

<?php
$json =<<<JSON
[{"License":"21X2-214X-SSDF-3215-SFFA","IP":"111111111","Note":"x1"},{"License":"S31S-SAF3-XF22-SLLT-341D","IP":"1111111","Note":"x2"}]
JSON;

$data    = json_decode($json, true);
$ip      = $_GET['IP']      ?? null;
$license = $_GET['license'] ?? null;
$found   = false;

foreach($data as $item)
    if($item['License'] == $license && $item['IP'] == $ip)
        $found = true;

$user_auth =
    array( 
        "user_info" =>
            array(
                "auth" => $found ? 1 : 0
            )
    );

var_dump($user_auth);

Output (empty $_GET):

array(1) {
  ["user_info"]=>
  array(1) {
    ["auth"]=>
    int(0)
  }
}