PHP multidimensional array search by key value array

Tìm kiếm 1 mảng đa chiều key và value

<?php
function search_revisions($dataArray, $search_value, $key_to_search, $other_matching_value = null, $other_matching_key = null) {
    // This function will search the revisions for a certain value
    // related to the associative key you are looking for.
    $keys = array();
    foreach ($dataArray as $key => $cur_value) {
        if ($cur_value[$key_to_search] == $search_value) {
            if (isset($other_matching_key) && isset($other_matching_value)) {
                if ($cur_value[$other_matching_key] == $other_matching_value) {
                    $keys[] = $key;
                }
            } else {
                // I must keep in mind that some searches may have multiple
                // matches and others would not, so leave it open with no continues.
                $keys[] = $key;
            }
        }
    }
    return $keys;
}
$data = array(
    array(
       
        'controller_id' => '',
        'action_id' => '',
		'layout' => 'column-1'
    ),
    array(
       
        'controller_id' => 'dashboard',
        'action_id' => 'list',
		'layout' => 'column-2'
    ),
    array(
  
        'controller_id' => 'dashboard',
        'action_id' => '',
		'layout' => 'column-2'
    )
);

$findKey = search_revisions($data,'dashboard', 'controller_id', 'list', 'action_id');
print_r($findKey);
if($findKey){
echo '<pre>';
print_r($data[$findKey[0]]['layout']);
echo '</pre>';
}

 

Tin tức khác