如何在php多维数组中引用具有特定条件的单元格

如何在php多维数组中引用具有特定条件的单元格,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我得到了这样一个数组: $array[0][name] = "Axel"; $array[0][car] = "Pratzner"; $array[0][color] = "black"; $array[1][name] = "John"; $array[1][car] = "BMW"; $array[1][color] = "black"; $array[2][name] = "Peggy"; $array[2][car] = "VW";

我得到了这样一个数组:

$array[0][name]   = "Axel";
$array[0][car]  =     "Pratzner";
$array[0][color]   = "black";

$array[1][name]   = "John";
$array[1][car]  =     "BMW";
$array[1][color]   = "black";

$array[2][name]   = "Peggy";
$array[2][car]  =     "VW";
$array[2][color]   = "white";
我想做一些事情,比如得到所有的名字,其中车=宝马和颜色=白色

有人能就PHP的拼写给出建议吗

function getWhiteBMWs($array) {
    $result = array();
    foreach ($array as $entry) {
        if ($entry['car'] == 'bmw' && $entry['color'] == 'white')
            $result[] = $entry;
    }

    return $result;
}
编辑:这是一个更通用的解决方案:

// Filter an array using the given filter array
function multiFilter($array, $filters) {
    $result = $array;

    // Removes entries that don't pass the filter
    $fn = function($entry, $index, $filter) {
        $key = $filter['key'];
        $value = $filter['value'];
        $result = &$filter['array'];

        if ($entry[$key] != $value)
            unset($result[$index]);
    };

    foreach ($filters as $key => $value) {
        // Pack the filter data to be passed into array_walk
        $filter = array('key' => $key, 'value' => $value, 'array' => &$result);

        // For every entry, run the function $fn and pass in the filter data
        array_walk($result, $fn, $filter);
    }

    return array_values($result);
}

// Build a filter array - an entry passes this filter if every
// key in this array corresponds to the same value in the entry.
$filter = array('car' => 'BMW', 'color' => 'white');

// multiFilter searches $array, returning a result array that contains
// only the entries that pass the filter. In this case, only entries
// where $entry['car'] = 'BMW' AND $entry['color'] = 'white' will be
// returned.
$whiteBMWs = multiFilter($array, $filter);
编辑:这是一个更通用的解决方案:

// Filter an array using the given filter array
function multiFilter($array, $filters) {
    $result = $array;

    // Removes entries that don't pass the filter
    $fn = function($entry, $index, $filter) {
        $key = $filter['key'];
        $value = $filter['value'];
        $result = &$filter['array'];

        if ($entry[$key] != $value)
            unset($result[$index]);
    };

    foreach ($filters as $key => $value) {
        // Pack the filter data to be passed into array_walk
        $filter = array('key' => $key, 'value' => $value, 'array' => &$result);

        // For every entry, run the function $fn and pass in the filter data
        array_walk($result, $fn, $filter);
    }

    return array_values($result);
}

// Build a filter array - an entry passes this filter if every
// key in this array corresponds to the same value in the entry.
$filter = array('car' => 'BMW', 'color' => 'white');

// multiFilter searches $array, returning a result array that contains
// only the entries that pass the filter. In this case, only entries
// where $entry['car'] = 'BMW' AND $entry['color'] = 'white' will be
// returned.
$whiteBMWs = multiFilter($array, $filter);

在代码中这样做或多或少模拟了RDBMS的完美功能。类似这样的方法会奏效:

function getNamesByCarAndColor($array,$color,$car) { 
    $matches = array();
    foreach ($array as $entry) { 
        if($entry["color"]== $color && $entry["car"]==$car) 
           matches[] = $entry["name"]; 
    }
    return $matches;
}

这段代码适用于较小的阵列,但随着阵列越来越大,这显然不是一个很好的解决方案,索引解决方案会更干净。

在代码中这样做或多或少模拟了RDBMS的完美功能。类似这样的方法会奏效:

function getNamesByCarAndColor($array,$color,$car) { 
    $matches = array();
    foreach ($array as $entry) { 
        if($entry["color"]== $color && $entry["car"]==$car) 
           matches[] = $entry["name"]; 
    }
    return $matches;
}

这段代码适用于较小的数组,但随着数组越来越大,这显然不是一个很好的解决方案,索引解决方案会更干净。

每次处理数组时,循环都是一个解决方案,您的问题标题与bodyUse数据库和sql查询相矛盾:-创建一个函数并让它将Doy it函数名命名为$color,$car{foreach$array作为$value{如果$value[color]==$color&&$value[car]==$car return$value[name];}每次必须处理数组时,循环都是一个解决方案,您的问题标题与bodyUse数据库和sql查询相矛盾:-创建一个函数并让它将doing it函数名命名为$color,$car{foreach$数组作为$value{if$value[color]==$color&&$value[car]=$car return$value[name];}