php - How to search in multi dimensional array

401

i have multi dimension arrays in which i want to look for some particular words this is how my array looks like

Array
(
    [0] => Array
        (
            [sch_name] => Montfort School Roorkee
            [sch_degree] => High School
            [sch_field] => Commerce
            [sch_grade] => A+
            [sch_from_year] => 2007
            [sch_to_year] => 2015
            [sch_desc] => Montfort School was very nice
        )

    [1] => Array
        (
            [sch_name] => City Public
            [sch_degree] => 12
            [sch_field] => Commerce
            [sch_grade] => 12
            [sch_from_year] => 2007
            [sch_to_year] => 2015
            [sch_desc] => Great School
        )

)

Now suppose i want to find 'montfort' word in this multi dimensional array

So i tried using this function

function searchForWord($id, $array) {
   foreach ($array as $key => $val) {
       if ($val['sch_name'] === $id) {
           return $key;
       }
   }
   return null;
}
echo $id = searchForWord('montfort', $p);

it resulted in nothing but when i wrote 'Montfort School Roorkee' result was found, why these word are so sensitive is there any other way to get values

can't i use something like we do in mysql query Like %keyword

398

Answer

Solution:

Try this:

$cols = array_column($your_array,'sch_name');
$keys = [];
foreach ($cols as $k=>$v) {
    if (strpos(strtolower($cols[$k]),strtolower('Mont'))!==false) {
        $keys[]=$k;               
    }
}
print_r($keys);

replace $your_array with your array.

it's case insensitive and will match anything partial. You could make it a function like so:

function SearchArrayForKeys($array,$column_name,$search) {
    if (!$array||!is_array($array)) {
        return [];
    }
    $cols = array_column($array,$column_name);
    if (!$cols){
        return [];
    }
    $keys = [];
    foreach ($cols as $k=>$v) {
        if (strpos(strtolower($cols[$k]),strtolower($search))!==false) {
            $keys[]=$k;               
        }
    }        
    return $keys;
}

usage:

$keys = SearchArrayForKeys($your_array,'sch_name','Mont');
602

Answer

Solution:

In your code you are actually trying a complete string matchif ($val['sch_name'] === $id) which wouldn't return true unless the input search pattern is the whole string.

To find partial strings from a string usestrpos(<string>,<pattern>).

For example:

<?php
   $needle='Montfort';
   $haystack='Montfort School was very nice';

   if(strpos($haystack,$needle)!==false){
     print("Found");
   }
   else{
     print("Not Found");
   }
?>

Also if you want a case insensitive comparison usestrtolower on both strings which you use in thestrpos match.

Eg :if(strpos(strtolower($haystack),strtolower($needle))!==false)

People are also looking for solutions to the problem: fpdf - How to iterate through $_POST arrays in PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.