如何在2D数组中搜索退出值

时间:2012-02-18 05:53:51

标签: php arrays multidimensional-array

我想检查数组中的值,如果退出则显示“退出”,否则显示“不在数组中”..

1)。     我创建了一个array =>

    $browse['pro_id'] = $id;
    $browse['pro_name'] = $mobile_details[0]['pro_name'];
    $browse['pro_brand'] = $mobile_details[0]['pro_brand'];
    $browse['pro_price_own'] = $mobile_details[0]['pro_price_own'];
    $mob_arr = $browse;
    print_r($browse);

//像这样的打印数组..

Array
(
    [pro_id] => mob810013034
    [pro_name] => Galaxy Y S5360
    [pro_brand] => Samsung
    [pro_price_own] => 6291
)

2)。过了2次。我正在上面给出array =>

中的数组
array_push($mob_arr,$browse);
print_r($mob_arr);

//This print array like this...


Array
(
    [pro_id] => mob810013034
    [pro_name] => Galaxy Y S5360
    [pro_brand] => Samsung
    [pro_price_own] => 6291 

    [0] => Array
        (
            [pro_id] => mobka10013042
            [pro_name] => A 1
            [pro_brand] => Karbonn
            [pro_price_own] => 6000
        )
)

我想检查整个阵列中的[pro_id] = mobka10013042然后继续;在$ mob_arr

中再次使用puch数组
array_push($mob_arr,$browse);

我使用的是in_array,但它不能用于此...

请给我建议.....

3 个答案:

答案 0 :(得分:0)

不确定方法是什么。但这可能有助于开始。

$mob_arr = array();
$browse['pro_id'] = 'mob810013034';
$browse['pro_name'] = 'Galaxy Y S5360';
$browse['pro_brand'] = 'Samsung';
$browse['pro_price_own'] = '6291';
array_push($mob_arr,$browse);

$browse['pro_id'] = 'mobka10013042';
$browse['pro_name'] = 'A 1';
$browse['pro_brand'] = 'Karbonn';
$browse['pro_price_own'] = '6000';

array_push($mob_arr,$browse);


if(checkId($mob_arr, 'pro_id', 'mobka10013042')) {
  print 'found the value...';
} else {
  print 'no can not find the value...';
}

function checkId($arr, $k, $v) {
  foreach($arr as $browse) {
    if($browse[$k] == $v) {
      return true;
    }
  }
  return false;
}

答案 1 :(得分:0)

尝试创建这样的结构:

$myarr = Array
(
    [0] => Array
        (
            [pro_id] => mob810013034
            [pro_name] => Galaxy Y S5360
            [pro_brand] => Samsung
            [pro_price_own] => 6291
        )

    [1] => Array
        (
            [pro_id] => mobka10013042
            [pro_name] => A 1
            [pro_brand] => Karbonn
            [pro_price_own] => 6000
        )
)

然后使用pop检索其值:

$mob_arr = array();
while(!empty($myarr))
{
   $temp = array_pop($myarr);
   if($temp['pro_id']!='mobka10013042'){
       array_push($mob_arr,$temp);
   }
}

if(empty($mob_arr)){
 //actions when mob_arr variable is empty
} else {
  //if not
}

答案 2 :(得分:0)

$i=0;
foreach($mob_arr as $row )
{
   if(is_array($row))
   {
     if(in_array('mobka10013042',$row))
    {
      $i=1;
    }
  }
}

if($i==1)
{
  continue 
}
else
{
  // push another array
}