Alpha
Alpha

Reputation: 329

How to use array_search for searching values from multiple values in an array

I have array $DATA as below

{
    "LSP": "EXB",
    "AWB": "8421604",
    "SCANS": [
        {
            "SCAN_TIME": "2019-07-17 20:05:00",
            "SCAN_STATUS": "Uploaded",
            "SCAN_CODE": "001",
            "SCAN_LOCATION": "DLH"
        },
        {
            "SCAN_TIME": "2019-07-18 15:52:00",
            "SCAN_STATUS": "Picked Up",
            "SCAN_CODE": "0011",
            "SCAN_LOCATION": "DLH"
        },
        {
            "SCAN_TIME": "2019-07-19 00:22:00",
            "SCAN_STATUS": "Scanned",
            "SCAN_CODE": "003",
            "SCAN_LOCATION": "GAX"
        }

    ]
}

$CODES = array("0011","003");

Now I want to search the SCAN_CODE values in $CODES from $DATA

So using array_search the same returns an error. My current code to get KEY is

$SEARCH_KEY = array_search($CODES,array_column($DATA,"SCAN_CODE"));

$SEARCH_KEY returns

false

My requirement is to get the first INDEX VALUE even if there are multiple INDEXES with same VALUE for SCAN_CODE and I guess array_search returns only the first instance.

Upvotes: 1

Views: 1304

Answers (3)

Nick
Nick

Reputation: 147166

Assuming that your $DATA is actually the JSON source, and that you've edited some of it out and left a dangling comma, then this code will find you the lowest index at which one of the values in $CODES appears. Note that you have to search for values individually, as array_search will otherwise look for the array as a value to match.

$DATA = json_decode($json, true);
$CODES = array("0011","003");
$SEARCH_KEY = count($DATA['SCANS']);
foreach ($CODES as $CODE) {
    $SEARCH_KEY = min($SEARCH_KEY, array_search($CODE,array_column($DATA['SCANS'],"SCAN_CODE")));
}
echo $SEARCH_KEY;

Output (for your data):

1

Demo on 3v4l.org

If you want to find the scan with the earliest SCAN_TIME which matches one of the codes, you can sort the SCANS and then get an index into the sorted array e.g.

$DATA = json_decode($json, true);
$CODES = array("0011","003");
$SEARCH_KEY = count($DATA['SCANS']);
$SCANS = $DATA['SCANS'];
array_multisort(array_column($SCANS, 'SCAN_TIME'), $SCANS);
print_r($SCANS);
foreach ($CODES as $CODE) {
    $SEARCH_KEY = min($SEARCH_KEY, array_search($CODE,array_column($SCANS,"SCAN_CODE")));
}
print_r($SCANS[$SEARCH_KEY]);

Output (for my changed data in this demo):

Array
(
    [SCAN_TIME] => 2019-07-19 00:22:00
    [SCAN_STATUS] => Scanned
    [SCAN_CODE] => 003
    [SCAN_LOCATION] => GAX
)

Upvotes: 1

Babbal Ahsan
Babbal Ahsan

Reputation: 40

<?php 
$someJSON = '{
	        "LSP": "EXB",
            "AWB": "8421604",
			"SCANS": [{"SCAN_TIME": "2019-07-17 20:05:00",
			"SCAN_STATUS": "Uploaded",
			"SCAN_CODE": "001",
			"SCAN_LOCATION": "DLH"
			},{
            "SCAN_TIME": "2019-07-18 15:52:00",
            "SCAN_STATUS": "Picked Up",
            "SCAN_CODE": "0011",
            "SCAN_LOCATION": "DLH"
        },
        {
            "SCAN_TIME": "2019-07-19 00:22:00",
            "SCAN_STATUS": "Scanned",
            "SCAN_CODE": "003",
            "SCAN_LOCATION": "GAX"
        }

    ]
}';

 $someArray = json_decode($someJSON, true);
 $CODES = array("0011","003");
 $matched=array();
foreach($someArray['SCANS'] as $item)
{
   
	if (in_array($item['SCAN_CODE'], $CODES))
    {
	   
	   array_push($matched,$item['SCAN_CODE']);
	  
    }
}
echo"<pre>";
print_r($matched);
exit;

?>

Upvotes: 0

ascsoftw
ascsoftw

Reputation: 3476

array_search expects the parameter to be searched as string. You are passing an array. You need to loop through $CODES and search for each code.

foreach( $CODES as $code ) {
    $SEARCH_KEY = array_search( $code , array_column($DATA['SCANS'],"SCAN_CODE"));
    if( $SEARCH_KEY !== false ) { //If we have find the Key, break from the loop.
        break;
    }
}

Here is the working demo

Upvotes: 1

Related Questions