1

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

                ),
            );
        } 

    }

3 Answers 3

1

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

                ),
            );
        }

}
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

I don't understand why you put $test_data as variable? i need it from $json also i will have more data in json later...
You receive you data thru the $_GET[], thats why i use test_data
its same like, if(in_array($_GET["license"], $cer) && in_array($_GET["IP"], $cer)) ?
0

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)
  }
}

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.