1

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.

2
  • you have for every element on m-array you need to do a loop through a-array, also you need to do $is_exist = FALSE; in the first line in the forloop Commented Oct 25, 2022 at 4:16
  • so, I need to loop a-array inside m-array? I try do that. Thank You Commented Oct 25, 2022 at 4:20

2 Answers 2

1

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);                                  
Sign up to request clarification or add additional context in comments.

1 Comment

1

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

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.