2012-12-04 55 views
3

HI我對PHP相當陌生。Php Array key =>值搜索

我有一個數組

$ ARR =陣列(0 =>數組( 'GID'=> 1, '組名'=> 「CAT1」, '成員'=>數組(0 =>數組( '''')), 1 => array('GID'=> 11,'mname'=>'wwww'),1 => array('mid'=> 12,'mname'=>'wswww')) => 2, '組名'=> 「CAT2」, '成員'=>數組(0 =>數組( '中'=> 13, 'MNAME'=> 'gggwwww'),1 =>數組( '中' => 14,'mname'=>'wvvwww'))), 2 => array('GID'=> 3,'groupname'=>「cat1」,'members'=> array(0 => array '中'=> 15, 'MNAME'=> 'wwddsww')),1 =>數組( '中'=> 16, 'MNAME'=> 'wwwdddw')));

 ie...,I have GID,groupname,mid(member id),mname(member name).I want to insert a new mid and mname into a group if it is already in the array ,if it is not exists then create a new subarray with these elements..I also need to check a member id(mid) is also present.........................I used the code but its not working fine............. if (!empty($evntGroup)) { 


      foreach ($evntGroup as $k => $group) { 

       if ($group['GID'] == $group_id) { 
        foreach($group as $j=> $mem){ 

        if($mem['mid'] == $mem_id){ 

         unset($evntGroup[$k]['members'][$j]['mid']); 
         unset($evntGroup[$k]['members'][$j]['mname']); 
        } 
        else{ 


        $evntGroup[$k]['members'][] = array(
         'mid' => $mem_id, 
         'mname' => $mem_name); 

        }} 
       } else { 


        $evntGroup[] = array(
         'GID' => $group_id, 
         'groupname' => $Group['event_group_name'], 
         'members' => array(
          0 => array(
           'mid' => $mem_id, 
           'mname' => $mem_name 
          ) 
         ) 
        ); 

       } 

      } 
     } else { 

      $evntGroup[$i]['GID'] = $group_id; 
      $evntGroup[$i]['groupname'] = $Group['event_group_name']; 
      $evntGroup[$i]['members'][] = array(
       'mid' => $mem_id, 
       'mname' => $mem_name); 
      $i++; 

     } 
+0

它的'mid'是在'members'有其自己的ID ...什麼你的預期結果就像'GID'=> 1'被修改爲 – Baba

回答

2

在函數的形式,最簡單的解決方案將是這個樣子:

function isGidInArray($arr, $val) { 
    foreach($arr as $cur) { 
     if($cur['GID'] == $val) 
      return true; 
    } 
    return false; 
} 

你更新你的問題來指定要如果指定的GID被發現怎麼辦,但是這只是一個簡單的除循環:

function doSomethingIfGidInArray($arr, $val) { 
    foreach($arr as $cur) { 
     if($cur['GID'] == $val) { 
      doSomething(); 
      break; //Assuming you only expect one instance of the passed value - stop searching after it's found 
     } 
    } 
} 

有不幸的是,這將父陣列內檢索每個數組的索引相同沒有本地PHP陣列功能。我經常想要這樣的事情。如果GID等於

+0

ie ...,我有GID,groupname,mid(成員id),mname(成員名稱)。我想插入一個新的mid和mname如果它已經在數組中,如果它不存在,那麼創建一個包含這些元素的新子數組..我還需要檢查一個成員id(mid)也存在...... ...................我用的代碼,但它不能正常工作............. –

1

像這樣將匹配3:

foreach($arr as $item) { 
    if($item['GID'] == 3) { 
    // matches 
    } 
} 
1

有代碼

function updateByGid(&$array,$gid,$groupname,$mid,$mname) { 
    //For each element of the array 
    foreach ($array as $ii => $elem) { 
     //If GID has the same value 
     if ($elem['GID'] == $gid) { 
      //Insert new member 
      $array[$ii]['members'][]=array(
         'mid'=>$mid, 
         'mname'=>$mname); 
      //Found! 
      return 0; 
     } 
    }  
    //If not found, create new 
    $array[]=array(
      'GID'=>$gid, 
      'groupname'=>$groupname, 
      'members'=>array(
       0=>array(
        'mid'=>$mid, 
        'mname'=>$mname 
       ) 
      ) 
    ); 

    return 0; 
} 
+0

我有GID,groupname,mid(成員id),mname(成員名稱)。我想插入一個新的mid和mname到一個組中,如果它已經在數組中,如果它不存在,那麼用這些元素創建一個新的子數組 –

+0

和...什麼是問題?少了什麼東西? – RainHeart257

+0

然後,你想在GID中尋找一個mid和mname,如果你沒有找到它,插入一個新的成員?你有你想要使用的GID,還是你必須創建一個新的(如果它不存在)? 我認爲解決方案很簡單,但我不太瞭解你 – RainHeart257