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 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败