Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP search multidimensional array that contains a value

I want to get arrays that contains a value from column

[{"city":"Bergerac","city_code":"EGC","country":"France","iata_code":"EGC","name":"Bergerac Dordogne P\u00e9rigord"},{"city":"Cork","city_code":"ORK","country":"Ireland","iata_code":"ORK","name":"Cork"},{"city":"Arctic Bay","city_code":"YAB","country":"Canada","iata_code":"YAB","name":"Arctic Bay"},{"city":"Corn Island","city_code":"RNI","country":"Nicaragua","iata_code":"RNI","name":"Corn Island"}, {"city":"Arctic Bay","city_code":"YAB","country":"Canada","iata_code":"YAB","name":"Arctic Bay2"}, {"city":"Arctic Bay","city_code":"YAB","country":"Canada","iata_code":"YAB","name":"Arctic Bay3"}]

I want to search "can" on column "country"

and return

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[{"city":"Arctic Bay","city_code":"YAB","country":"Canada","iata_code":"YAB","name":"Arctic Bay"}, {"city":"Arctic Bay","city_code":"YAB","country":"Canada","iata_code":"YAB","name":"Arctic Bay2"}, {"city":"Arctic Bay","city_code":"YAB","country":"Canada","iata_code":"YAB","name":"Arctic Bay3"}]

Here is what is did

$index = array_search('Can', array_column($array, 'country'));

but I didn’t get the result, it only returns the result when I search "Canada"

>Solution :

To search for a substring in the column "country" and return the matching arrays, you can use a loop to iterate through the array and check if the "country" value contains the substring you are looking for. Here’s an example code:

$array = '[{"city":"Bergerac","city_code":"EGC","country":"France","iata_code":"EGC","name":"Bergerac Dordogne P\u00e9rigord"},{"city":"Cork","city_code":"ORK","country":"Ireland","iata_code":"ORK","name":"Cork"},{"city":"Arctic Bay","city_code":"YAB","country":"Canada","iata_code":"YAB","name":"Arctic Bay"},{"city":"Corn Island","city_code":"RNI","country":"Nicaragua","iata_code":"RNI","name":"Corn Island"}, {"city":"Arctic Bay","city_code":"YAB","country":"Canada","iata_code":"YAB","name":"Arctic Bay2"}, {"city":"Arctic Bay","city_code":"YAB","country":"Canada","iata_code":"YAB","name":"Arctic Bay3"}]';

$search_string = 'can';

$result = array();
$array = json_decode($array, true);

foreach ($array as $value) {
    if (stripos($value['country'], $search_string) !== false) {
        $result[] = $value;
    }
}

$result = json_encode($result);
echo $result;

In this code, we are iterating through each value in the array, and checking if the "country" value contains the substring we are looking for using the stripos() function. If the stripos() function returns a value other than false, it means that the substring was found in the "country" value, so we add the entire value to the $result array. Finally, we encode the $result array back to JSON format and output it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading