douhong9210 2016-10-12 11:14
浏览 29
已采纳

在多维数组上搜索值然后显示结果

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

  • 写回答

6条回答 默认 最新

  • dou7851 2016-10-12 11:23
    关注

    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
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端