如果匹配,如何获得其他数组值?

时间:2017-04-02 16:20:42

标签: php arrays foreach

数组1输出

 Array ( 
       [0] => Array ( [ID] => 335 [userid] => 4 [username] => demo [media_id] => 17 ) 
       [1] => Array ( [ID] => 436 [userid] => 4 [username] => demo [media_id] => 18 )
       [2] => Array ( [ID] => 637 [userid] => 4 [username] => demo [media_id] => 19 ) 
       [3] => Array ( [ID] => 838 [userid] => 4 [username] => demo [media_id] => 20 ) 
 );

数组2输出

 Array ( 
        [1] => Array ( [ID] => 35 [userid] => 4  [media_id] => 17 ) 
        [2] => Array ( [ID] => 36 [userid] => 4  [media_id] => 18 )
     );

如果匹配,如何获取其他数组值?如果media_id中的useridarray 2array 1中的ID匹配,那么我需要如何从username获取相关array 1array 2 $array1 = array ( 0 => array ( "ID" => "335", "userid" => "4", "username" => "demo", "media_id" => "17" ), 1 => array ( "ID" => "436", "userid" => "4", "username" => "demo", "media_id" => "18" ), 2 => array ( "ID" => "637", "userid" => "4", "username" => "demo", "media_id" => "19" ), 3 => array ( "ID" => "838", "userid" => "4", "username" => "demo", "media_id" => "20" ) ); $array2 = array ( 1 => array ( "ID" => "35", "userid" => "4", "media_id" => "17" ), 2 => array ( "ID" => "36", "userid" => "4", "media_id" => "18" ) ); foreach($array2 as $array) { foreach($array1 as $get_data) { if($array1['media_id'] == $get_data['media_id'] && $array1['userid'] == $get_data['userid']){ $get_result[] = //get_data true; } else { $get_result[] = //get_data false; } } //get ID and username or show false value } 的foreach循环?

更新

foreach($array2 as $array) {
     $return = "false";
     foreach($array1 as $get_data) {
         if($array1['media_id'] == $get_data['media_id'] && $array1['userid'] == $get_data['userid']) {
         $get_result[] = ['ID' => $get_data['ID'], 'username' => $get_data['username']];
         $return = "true";
    }
  }

   if($return = "false"){
        echo false;
   }

}

最终答案

{{1}}

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题(我不确定),这应该有效:

foreach($array2 as $array) {
  foreach($array1 as $get_data) {
    if($array1['media_id'] == $get_data['media_id'] && $array1['userid'] == $get_data['userid']) {
      $get_result[] = ['ID' => $get_data['ID'], 'username' => $get_data['username']];
    }
  }
}
if (count($get_result) == 0) {
  $get_result[] = false;
}

答案 1 :(得分:0)

不确定你的真实需求,但@gmc给你(部分?全部?)你需要什么恕我直言

<?php

$array1 = array ( 
0 => array ( "ID" => "335", "userid" => "4", "username" => "demo", "media_id" => "117" ), 
1 => array ( "ID" => "436", "userid" => "4", "username" => "demo", "media_id" => "118" ),
2 => array ( "ID" => "637", "userid" => "4", "username" => "demo", "media_id" => "19" ), 
3 => array ( "ID" => "838", "userid" => "4", "username" => "demo", "media_id" => "20" ) 
                  );

$array2 = array ( 
1 => array ( "ID" => "35", "userid" => "4", "media_id" => "17" ),
2 => array ( "ID" => "36", "userid" => "4", "media_id" => "18" )
                      );

$get_result = array();

  foreach($array2 as $array) {

      foreach($array1 as $get_data) {
             if( ($array['media_id'] == $get_data['media_id']) && ($array['userid'] == $get_data['userid'])) {
                                $get_result[] = ['ID' => $get_data['ID'], 'username' => $get_data['username']];
                               }
                            }
  }

if (count($get_result) == 0) {
echo"false";
}
else {
echo"true"; print_r($get_result); }
?>

/* I modified values to 117 & 118 -> returns "false" */
/* If you set values to 17 & 18 -> returns "true" */