搜索数组值并返回php中的行

时间:2014-09-04 21:07:25

标签: php arrays function search

找不到合适的答案,希望有人可以提供帮助。基本上想要搜索[VIN]值,如果匹配则返回[MEDIAID]或行。

Array
(
    [0] => Array
        (
            [CLIENTID] => GALLEY
            [VIN] => WBAFDEG2317MCB73388
            [MEDIAID] => 10011020061817-galley_082114-SDvcl-140880481613056500
            [DEALERNAME] => Demo
            [PUBLISHON] => 2014-08-28
        )
    [1] => Array
        (
            [CLIENTID] => GALLEY
            [VIN] => WAULC68E74A053WE251
            [MEDIAID] => 10011020061817-galley_082114-SDvcl-140880482109709900
            [DEALERNAME] => Demo
            [PUBLISHON] => 2014-08-26
        )
    [2] => Array
        (
            [CLIENTID] => GALLEY
            [VIN] => WAULC68E74A053WE251
            [MEDIAID] => 10011020061817-galley_082114-SDvcl-140880482109709900
            [PUBLISHON] => 2014-08-26
        )
)       

我需要在php中执行此操作。

1 个答案:

答案 0 :(得分:0)

从此already answered question复制的功能结构,我只做了一些更改,使其适用于您的应用程序:

function searchForId($id, $array) {
   foreach ($array as $key => $val) {
       if ($val['VIN'] === $id) {
           return $val['MEDIAID'];
       }
   }
   return null;
}

$array = array(
  0=> array(
    'CLIENTID' => 'GALLEY',
    'VIN' => 'WBAFDEG2317MCB73388',
    'MEDIAID' => '10011020061817-galley_082114-SDvcl-140880481613056500',
    'DEALERNAME' => 'Demo',
    'PUBLISHON' => '2014-08-28'
    ),
  1=> array(
    'CLIENTID' => 'GALLEY',
    'VIN' => 'WAULC68E74A053WE251',
    'MEDIAID' => '10011020061817-galley_082114-SDvcl-140880482109709900',
    'DEALERNAME' => 'Demo',
    'PUBLISHON' => '2014-08-26'
    ),
  2=> array(
    'CLIENTID' => 'GALLEY',
    'VIN' => 'WAULC68E74A053WE251',
    'MEDIAID' => '10011020061817-galley_082114-SDvcl-140880482109709900',
    'DEALERNAME' => 'Demo',
    'PUBLISHON' => '2014-08-26'
    ),    
  );

$search = searchForId('WAULC68E74A053WE251', $array);

var_dump($search);

结果:

string(53) "10011020061817-galley_082114-SDvcl-140880482109709900"

请参阅demo