比较两个数组并订购它们

时间:2011-10-25 12:13:56

标签: php arrays

我想比较两个数组,然后根据Key字段对它们中的一个进行排序。

$arr_x[0] = array('name' => 'max', 'score' => '1');
$arr_x[1] = array('name' => 'june', 'score' => '2');
$arr_x[2] = array('name' => 'jacob', 'score' => '1');
$arr_x[3] = array('name' => 'john', 'score' => '4');



$arr_y[0] = array('name' => 'jacob', 'score' => '7');
$arr_y[1] = array('name' => 'john', 'score' => '6');
$arr_y[2] = array('name' => 'june', 'score' => '9');

我需要的订单后的结果是......

$arr_x[0] = array('name' => 'max', 'score' => '1');
$arr_x[1] = array('name' => 'june', 'score' => '2');
$arr_x[2] = array('name' => 'jacob', 'score' => '1');
$arr_x[3] = array('name' => 'john', 'score' => '4');

$arr_y[0] = array('name' => 'max', 'score' => '');
$arr_y[1] = array('name' => 'june', 'score' => '9');
$arr_y[2] = array('name' => 'jacob', 'score' => '7');
$arr_y[3] = array('name' => 'john', 'score' => '6');

我真的很感激一些帮助,因为我无法弄清楚如何做到这一点。这是一个例子,在实际情况中,每个可能有20-50个。

非常感谢。

4 个答案:

答案 0 :(得分:1)

可能有更好的方法(减少循环),但这是我的两分钱:

$temp = $arr_z = array();

foreach ($arr_x as $k => $item) {
  $temp[$item['name']]['position_x'] = $k;
}

foreach ($arr_y as $k => $item) {
  if (isset($temp[$item['name']])) {
    $temp[$item['name']]['position_y'] = $k;
  }
}

foreach ($temp as $name => $data) {
  if (isset($data['position_y'])) {
    $arr_z[$data['position_x']] = $arr_y[$data['position_y']];
  } else {
    $arr_z[$data['position_x']] = array('name'=>$name,'score'=>'');
  }
}

$arr_y = $arr_z;
unset($arr_z);

此代码不会考虑同一名称可能出现多次,或者如果$arr_y中没有$arr_x,则可能会显示一个值。

Demo here

答案 1 :(得分:0)

$a1 = array();
$a2 = array();
// fill arrays with values / create two dimensional array
$a3 = array(); //this one will hold sorted array

foreach($a1 as $key => $val){
    if(array_key_exists($key,$a2)){
         // compare values for same key here
        $a3[$key] = $a2[$key]; //put the value and same key to final array
    }
    else {
        $a3[$key] = ''; //init key in final array to empty value
    }
}

答案 2 :(得分:0)

试试这个:

$arr_y = array();

$arr_y[0] = array('name' => 'jacob', 'score' => '7');
$arr_y[1] = array('name' => 'john', 'score' => '6');
$arr_y[2] = array('name' => 'june', 'score' => '9');


$arr_z = array();

foreach($arr_y as $key => $value)
{
    $arr_z[$value['name']] = $value['score'];
}

$arr_x = array();

$arr_x[0] = array('name' => 'max', 'score' => '1');
$arr_x[1] = array('name' => 'june', 'score' => '2');
$arr_x[2] = array('name' => 'jacob', 'score' => '1');
$arr_x[3] = array('name' => 'john', 'score' => '4');

$arr_y_sorted = array();

foreach($arr_x as $key => $value)
{
    $arr_y_sorted[] = array('name' => $value['name'],'score' => isset($arr_z[$value['name']])?$arr_z[$value['name']]:'');    
}


print_r($arr_y_sorted);

答案 3 :(得分:0)

可能有更好的方法(减少循环),但这是我的两分钱:

$temp = $arr_z = array();

foreach ($arr_x as $k => $item) {
  $temp[$item['name']]['position_x'] = $k;
}

foreach ($arr_y as $k => $item) {
  if (isset($temp[$item['name']])) {
    $temp[$item['name']]['position_y'] = $k;
  }
}

foreach ($temp as $name => $data) {
  if (isset($data['position_y'])) {
    $arr_z[$data['position_x']] = $arr_y[$data['position_y']];
  } else {
    $arr_z[$data['position_x']] = array('name'=>$name,'score'=>'');
  }
}

此代码不会考虑同一名称可能出现多次,或者如果$arr_y中没有$arr_x,则可能会显示一个值。

Demo here