1

I'm trying to retrieve the values on multidimensional arrays using a search like function.

$rows = array(
    array(
        'Name'=>'City of God',
        'Year'=>'2002',
        'Rating'=>'10'
    ),
    array(
        'Name'=>'The Great Escape',
        'Year'=>'1963',
        'Rating'=>'9'
    ),
    array(
        'Name'=>'Dune',
        'Year'=>'1984',
        'Rating'=>'6'
    ),
    array(
        'Name'=>'Superbabies: Baby Geniuses 2',
        'Year'=>'2004',
        'Rating'=>'1'
    )
);

So for example, if you want to search the array with a value of Name with 'City of God' and Year with '1963' the expected output should be like this

$rows = array(
    array(
        'Name'=>'City of God',
        'Year'=>'2002',
        'Rating'=>'10'
    ),
    array(
        'Name'=>'The Great Escape',
        'Year'=>'1963',
        'Rating'=>'9'
    ),
);

Currently, the function I am using is this

function multiSearch(array $array, array $pairs)
{
    $found = array();
    foreach ($array as $aKey => $aVal) {
        $coincidences = 0;
        foreach ($pairs as $pKey => $pVal) {
            if (array_key_exists($pKey, $aVal) && $aVal[$pKey] == $pVal) {
                $coincidences++;
            }
        }

        if ($coincidences == count($pairs)) {
            $found[$aKey] = $aVal;
        }
    }

    return $found;
} 

However, using this function only capable of retrieving data of the same array key. So for example if I search the value of Name of 'City of God'

$x = multiSearch($rows, array('Name' => 'City of God') 

This will display the correct output like this

$rows = array(
    array(
        'Name'=>'City of God',
        'Year'=>'2002',
        'Rating'=>'10'
    ),
);

Unfortunately, if you try to use, 'Name' => 'City of God' and 'Year' => '1963' It will not display any output. I'm stuck on figuring out on displaying the correct output, any ideas would be appreciated

Atashi Dubz
  • 241
  • 1
  • 3
  • 14
  • try in_array() function –  Oct 12 '16 at 11:17
  • Possible duplicate of [How to search by key=>value in a multidimensional array in PHP](http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php) – Mayank Vadiya Oct 12 '16 at 11:19
  • after in_array, use unset [http://stackoverflow.com/questions/369602/delete-an-element-from-an-array] by key (arrays that you dont need) – Denis Solakovic Oct 12 '16 at 11:21

5 Answers5

0

Try this :

$datas = array(
    array(
        'Name'=>'City of God',
        'Year'=>'2002',
        'Rating'=>'10'
    ),
    array(
        'Name'=>'The Great Escape',
        'Year'=>'1963',
        'Rating'=>'9'
    ),
    array(
        'Name'=>'Dune',
        'Year'=>'1984',
        'Rating'=>'6'
    ),
    array(
        'Name'=>'Superbabies: Baby Geniuses 2',
        'Year'=>'2004',
        'Rating'=>'1'
    )
);

$search = array(
    'Name' => 'Dune',
    'Year' => '2004'
);

$output = array();
foreach ($datas as $key1 => $row) {
    foreach ($row as $key2 => $value) {
        if($search[$key2] == $value) {
        // if(stristr($value, $search[$key2]) !== FALSE) {  // if you want to search
            $output[] = $datas[$key1];
            break;
        }
    }
}

echo "<pre>"; print_r($output); exit;

Output:

Array
(
    [0] => Array
        (
            [Name] => Dune
            [Year] => 1984
            [Rating] => 6
        )

    [1] => Array
        (
            [Name] => Superbabies: Baby Geniuses 2
            [Year] => 2004
            [Rating] => 1
        )

)
Rijin
  • 625
  • 1
  • 4
  • 13
  • you are fixing what's not broken - his solution works with only a minimal change. it's actually pretty much equivalent to your solution. – hummingBird Oct 13 '16 at 08:02
0

If you need OR, as you mentioned in your question, you then need to see if at least one coincidence happened:

if ($coincidences > 0) {
   $found[$aKey] = $aVal;
}

This way, both entries:

$rows = array(
   array(
      'Name'=>'City of God',
      'Year'=>'2002',
      'Rating'=>'10'
   ),
   array(
     'Name'=>'The Great Escape',
     'Year'=>'1963',
     'Rating'=>'9'
   ),
);

will be matched.

hummingBird
  • 2,495
  • 3
  • 23
  • 43
0

Modify the search function to this - works for both the cases mentioned:

function multiSearch($rows, $value) {
  $newArr = array();
  foreach ($rows as $row) {
    foreach ($value as $key => $val) {
      if (isset($row[$key]) && $row[$key] == $val) {
        $newArr[] = $row;
      }
    }
  }
  return $newArr;
}

Calling with one or two values produces the required output -

$x = multiSearch($rows, array('Name' => 'City of God')); 

$x = multiSearch($rows, array('Name' => 'City of God', 'Year' => '1963'));

//returns $rows containing this pair
jitendrapurohit
  • 9,435
  • 2
  • 28
  • 39
0

Take a look on my suggestion

function search ($storage, $criteria)
{
    return array_filter(
       $storage,
       function ($row) use ($criteria) {
           foreach ($criteria as $key => $value) {
               if ($row[$key] === $value) {
                   return true;
               }
           } 
       }
    );
}

$result = search($datas, $search);

/**
 * $result = [
 *    2 => [
 *       'Name'   => 'Dune'
 *       'Year'   => '1984'
 *       'Rating' => '6'
 *    ],
 *    3 => [
 *       'Name'   => 'Superbabies: Baby Geniuses 2'
 *       'Year'   => '2004'
 *       'Rating' => '1'
 *    ]
 *  ];
 */
Vitalii
  • 1,137
  • 14
  • 25
-1

what about a recursive call?

function recursiveInArray ($needle, $array) {
    $status = false;
    foreach($array as $value) {
        if(is_array($value)) {
            recursiveInArray($needle, $value);
        } else {
            $status = ($needle == $value);
        }
    }
    return $status;
}

i know its abstract, but should help you working with multi dimensional searching

mtizziani
  • 956
  • 10
  • 23