比较数组中的值和获取键/索引

时间:2013-10-11 11:18:20

标签: php

我有阵列:

        $array = array(
            3 => array(
                'first' => 'aaa',
                'second' => '111',
                'third' => '!!!'
            ),
            7 => array(
                'first' => 'bbb',
                'second' => '222',
                'third' => '###'
            ),
            9 => array(
                'first' => 'ccc',
                'second' => '333',
                'third' => '^^^'
            ),

        );

和三个变量:

$first = 'bbb';
$second = '222';
$third = '###';

或者可以是数组:

$data = array(
     'first' => 'bbb',
     'second' => '222',
     'third' => '###'
);

我想从$ array获取这些值的键。在这个例子中应该返回7.

如何将此数组与$ first,$ second和$ third或array $ data进行比较,并使用这些值从$ array获取键?

1 个答案:

答案 0 :(得分:-1)

试试这个:

      $array = array(
        3 => array(
            'first' => 'aaa',
            'second' => '111',
            'third' => '!!!'
        ),
        7 => array(
            'first' => 'bbb',
            'second' => '222',
            'third' => '###'
        ),
        9 => array(
            'first' => 'ccc',
            'second' => '333',
            'third' => '^^^'
        ),
    );

    $data = array(
        'first' => 'bbb',
        'second' => '222',
        'third' => '###'
    );

    foreach ($array as $key => $val1) {
        $arr_diff = array_diff($val1, $data);

        if(empty($arr_diff))
        {
            echo $key;
        }
    }

foreach循环你会纠正答案。