Home > Enterprise >  How to check JSON String based on another JSON String?
How to check JSON String based on another JSON String?

Time:10-25

I'd like to do a loop with these JSON strings.

$json_m = '[
             {"name":"1","value":"1"},
             {"name":"2","value":"2"},
             {"name":"3","value":"3"},
             {"name":"4","value":"4"},
             {"name":"5","value":"5"},
           ]';

$json_a = '[
             {"name":"1-m","value":"1"},                 
             {"name":"2-m","value":"3"},                 
             {"name":"3-m","value":"5"},
           ]';

I do a loop and on $json_m. If the value exists in both JSON, I set the parameter to TRUE

This is my current code:

$jm         = json_decode($json_m, true);
$ja         = json_decode($json_a, true);

$i          = 0;
$is_exist   = FALSE;
    
    foreach($jm as $rows ){
        
        if($rows["value"] == $ja[$i]["value"]){
            $is_exist   = TRUE;
        }
            
        $dataadd    = array (   'ID'        => $i,
                                'NAME'      => $rows["value"],
                                'STATUS'    => $is_exist
                            );
        $i  ;
        
    }

This script returns as all FALSE, based on my example the STATUS should return like this:

TRUE
FALSE
TRUE
FALSE
TRUE

Do I miss something or what? Any help is greatly appreciated.

CodePudding user response:

You could make your life easier by extracting all the value values from $ja using array_column; you can then search that using in_array. You can then iterate $jm using array_map to produce an array of $dataadd values:

$ja_values = array_column($ja, 'value');

$dataadd = array_map(fn ($arr, $idx) => array('ID' => $idx, 'NAME' => $arr['value'], 'STATUS' => in_array($arr['value'], $ja_values)), $jm, array_keys($jm));

var_export($dataadd);

Output (for your sample data):

array (
  0 => 
  array (
    'ID' => 0,
    'NAME' => '1',
    'STATUS' => true,
  ),
  1 => 
  array (
    'ID' => 1,
    'NAME' => '2',
    'STATUS' => false,
  ),
  2 => 
  array (
    'ID' => 2,
    'NAME' => '3',
    'STATUS' => true,
  ),
  3 => 
  array (
    'ID' => 3,
    'NAME' => '4',
    'STATUS' => false,
  ),
  4 => 
  array (
    'ID' => 4,
    'NAME' => '5',
    'STATUS' => true,
  ),
)

CodePudding user response:

You can do using array_search like below

$json_m = '[
             {"name":"1","value":"1"},
             {"name":"2","value":"2"},
             {"name":"3","value":"3"},
             {"name":"4","value":"4"},
             {"name":"5","value":"5"}
           ]';

$json_a = '[
             {"name":"1-m","value":"1"},                 
             {"name":"2-m","value":"3"},                 
             {"name":"2-m","value":"3"},  
             {"name":"3-m","value":"5"}
           ]';

$jm         = json_decode($json_m, true);
$ja         = json_decode($json_a, true);

$javal = array_unique(array_column($ja, 'value'));


$is_exist   = FALSE;
$dataadd =[];
foreach($jm as $i=> $rows ){
        $is_exist   = FALSE;
        if(is_numeric(array_search($rows["value"], $javal))) {
            $is_exist   = TRUE; 
        }
        $dataadd[]    = array (   'ID'        => $i,
                                'NAME'      => $rows["value"],
                                'STATUS'    => $is_exist
                            );
    }
    print_r($dataadd);                                  
  • Related