1

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.

7
  • if you take a close look, SCANS contains array of objects and to use the array_search function you need to convert it to array first Commented Aug 20, 2019 at 12:06
  • $DATA is not possible to use array_column on. Use it on $DATA['SCANS'] adn it should work. And please post a working json. Commented Aug 20, 2019 at 12:08
  • @Andreas Notice: Undefined index: SCANS Warning: array_column() expects parameter 1 to be array Warning: array_search() expects parameter 2 to be array Commented Aug 20, 2019 at 12:10
  • yes that is because your json is invalid. post the real json Commented Aug 20, 2019 at 12:11
  • @Andreas also, array_search("003",array_column($DATA,"SCAN_CODE")); is working perfectly as it is string that is being searched. But it is not working with $CODES in place of "003" Commented Aug 20, 2019 at 12:12

3 Answers 3

1

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

3 Comments

Will it not work without foreach ? Also, there is high possibility that there will be multiple instances under SCANS So I need to find the first instance of my array_search() based on SCAN_TIME in ascending order with the values from $DATA['SCANS'].
@Alpha no, see my comment to your question. If the values in SCANS are in time order, this code will find the first one.
@Alpha see my edit. If the scans are not in time order, the new code will give you the value with the earliest times that matches a value in the $CODES array.
1

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

2 Comments

I think this will return the last value, I want the first instance of any of the values from $CODES from $DATA['SCANS'] based on SCAN_TIME in ascending order
@Alpha Updated the answer to return first key. Previously it was getting all keys.
0

<?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;

?>

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.