0

I have this multidimensional array. I need to search it and return only the matched and partial matched values. The resulted array should be same as original array dimension or structure . I need a function like:

function search_array($array,'kumar') { ....... }

My Array is

$array  = array(
    0 => array(
        2 => 'ram kumar',
        1 => 'alagu jubi',
        0 => 'kumar',
    ),
    1 => array(
        3 => 'senthil pandian',
        2 => 'jubi alagu',
        1 => 'manikandan',
        0 => array(
            2 => 'jancy',
            1 => 'guru',
            0 => 'rajesh kumar'
        )
    ),
    2 => array(
        2 => 'pandi',
        1 => 'selva',
        0 => 'ajith'
    )
);

Expected Result:

array
    (
    [0] => array
        (
            [2] => ram kumar
            [0] => kumar
        )

    [1] => array
        (
            [0] => array
                (
                    [0] => rajesh kumar
                )

        )

)

I try with this

function search_array($array, $val){
  $ArrIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array)); 
  foreach($ArrIterator as $id => $sub){
    $childArray = $ArrIterator->getSubIterator();
    if(strstr(strtolower($sub), strtolower($val))){
      $childArray = iterator_to_array($childArray);
      $result[] = $childArray;
    }
  }     
  return $result;
}

$resultsMul = search_array($array, 'kumar');

Its returns like this .

Array
(
    [0] => Array
        (
            [2] => ram kumar
            [1] => alagu jubi
            [0] => kumar
        )

    [1] => Array
        (
            [2] => jancy
            [1] => guru
            [0] => rajesh kumar
        )

)

But I need only the matched and partial matched values with the original array dimension.

What is efficient/best way to handle this? My array has nearly 200 000 records (may increase) and this array is global for my website (like a database).

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
alagu
  • 11
  • 3

2 Answers2

0

I'm not really sure how to solve this with an Iterator, but you could create a simple recursive function to achieve this:

function search_array(&$array, $val)
{
    foreach ($array as $key => &$value) {
        if (is_array($value)) {
            search_array($value, $val);
        } elseif (stripos($value, $val) === FALSE) {
            unset($array[$key]);
        }
    }
}

Usage:

search_array($array, 'kumar');
$result = array_filter($array);

Demo

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

You could recursively traverse the array linearized, then filter leafs based on regex (or the filter of your choice) and for the matches fetch the keys to build the result array:

$rai = new RecursiveArrayIterator($array);
$rii = new RecursiveIteratorIterator($rai);
$sii = new SubIteratorIterator($rii, SubIteratorIterator::USE_KEY);
$ri  = new RegexIterator($rii, '~kumar~i');

$result = NULL;
foreach ($ri as $value) {
    $p = & $result;
    foreach ($sii as $key) {
        $p = & $p[$key];
    }
    $p = $value;
    unset($p);
}

The RecursiveArrayIterator, RecursiveIteratorIterator and RegexIterator are standard PHP SPL types, the SubIteratorIterator is part of Iterator-Garden, it's an encapsulated variation of the common as in Get array's key recursively and create underscore seperated string.

The access (here to set) by those keys to the result array (compare for example: use strings to access (potentially large) multidimensional arrays but there are better examples) is merely straight forward with some referencing.

With your given input, the result then is:

Array
(
    [0] => Array
        (
            [2] => ram kumar
            [0] => kumar
        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => rajesh kumar
                )

        )

)
Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Hi Hakre, Thanks for your reply. Is any other way to do this stuff ? or this is the best way ? please suggest me ... because my array having nearly 2 lakhs records (may increase) and also this array is global for my website(like a database). – alagu May 06 '14 at 06:52