为密钥组合具有相同值的数组 - PHP

时间:2013-01-10 10:11:28

标签: php arrays multidimensional-array

我有一个多维数组

Array(
[0] => Array
    (
        [category_id] => 1
        [question_id] => 1
        [option_id] => 2
        [title] => Do you wear glasses?
        [answer] => no
    )

[1] => Array
    (
        [category_id] => 1
        [question_id] => 2
        [option_id] => 3
        [title] => Your hobbies ?
        [answer] => movies
    )

[2] => Array
    (
        [category_id] => 1
        [question_id] => 4
        [option_id] => 8
        [title] => what is your status?
        [answer] => single
    )

[3] => Array
    (
        [category_id] => 1
        [question_id] => 2
        [option_id] => 1
        [title] => Your hobbies ?
        [answer] => travel
    )
) 

其中第2和第4个数组包含相同的问题(相同的类别ID和相同的问题ID) 我想组合具有相同类别ID和问题ID的数组。

我期待的是这个

Array(
[0] => Array
    (
        [category_id] => 1
        [question_id] => 1
        [option_id] => 2
        [title] => Do you wear glasses?
        [answer] => no
    )

[1] => Array
    (
        [category_id] => 1
        [question_id] => 2
        [option_id] => 3
        [title] => Your hobbies ?
        [answer] => movies,travel
    )

[2] => Array
    (
        [category_id] => 1
        [question_id] => 4
        [option_id] => 8
        [title] => what is your status?
        [answer] => single
    )
) 

手段仅结合其答案。请帮忙......

5 个答案:

答案 0 :(得分:0)

也许像这样的东西可以起作用,不确定是否有更好的方法来做到这一点:

new_array=array();
foreach($source_array as $e){
     $insert=1;
     foreach($new_array as $n)
         if(array_search($e['title'], $n)==false){
              $n['answer'].=", ".$e['answer'];
              $insert=0;   
         }
     }
     if($insert){
         $new_array[]=$e;
     }
}

答案 1 :(得分:0)

对于示例数组

Array
(
[0] => Array
    (
        [category_id] => 1
        [question_id] => 1
    )

[1] => Array
    (
        [category_id] => 2
        [question_id] => 2
    )

[2] => Array
    (
        [category_id] => 1
        [question_id] => 1
    )

)

你可以做这样的事情作为插图

// Define the array to work on
$array = array( array( 'category_id' => 1, 'question_id'=> 1), array( 'category_id' => 2, 'question_id' => 2),  array( 'category_id' => 1, 'question_id' => 1));

// Initialize the result array containing only unique question_id/category_id combinations
$unique_array = array();

foreach ($array as $key) {
    if (!count($unique_array)) {
        $unique_array[] = $key;
    } else {
        $unique = 1;
        foreach ($unique_array as $check) {
            if ( $check['category_id'] ==  $key['category_id'] && $check['question_id'] == $key['question_id'] ) {
                $unique = 0;
            }
        }
        if ( $unique ) {
            $unique_array[] = $key;
        }
    }
}

print_r($unique_array);

最后一个print_r的输出是

Array
(
[0] => Array
    (
        [category_id] => 1
        [question_id] => 1
    )

[1] => Array
    (
        [category_id] => 2
        [question_id] => 2
    )

)

答案 2 :(得分:0)

我询问订单是否重要的​​原因是,如果它不重要,您可以先对数组进行排序,然后只将每个数据与之前的兄弟进行比较:

$source=array(array("cat_id"=>1,"ques_id"=>1,"opt_id"=>2,"title"=>"cid:1, qid:1, oid:2","answer"=>"whatever"),array("cat_id"=>1,"ques_id"=>2,"opt_id"=>3,"title"=>"cid:1, qid:2, oid:3","answer"=>"whatever"),array("cat_id"=>1,"ques_id"=>4,"opt_id"=>8,"title"=>"cid:1, qid:4, oid:8","answer"=>"whatever"),array("cat_id"=>1,"ques_id"=>2,"opt_id"=>1,"title"=>"cid:1, qid:2, oid:1","answer"=>"whtvr"));
print_r($source); // just to debug

usort($source,function($a,$b){
    if($a["cat_id"]<$b["cat_id"])
        return -1;
    elseif($a["cat_id"]>$b["cat_id"])
        return 1;
    elseif($a["ques_id"]<$b["ques_id"])
        return -1;
    elseif($a["ques_id"]>$b["ques_id"])
        return 1;
    elseif($a["opt_id"]<$b["opt_id"])
        return -1;
    elseif($a["opt_id"]>$b["opt_id"])
        return 1;
    else
        return 0;
});

$source=array_reduce($source,function(&$a,$b){
    if(empty($a))
    {
        $a=array($b);
    }
    else
    {
        $cache=array_pop($a);
        if($cache["cat_id"]==$b["cat_id"] && $cache["ques_id"]==$b["ques_id"])
        {
            $cache["answer"].=", ".$b["answer"];
            $a[]=$cache;
        }
        else
        {
            $a[]=$cache;
            $a[]=$b;
        }
    }
    return $a;
},array());
print_r($source);

输出:

// first print_r
Array
(
    [0] => Array
        (
            [cat_id] => 1
            [ques_id] => 1
            [opt_id] => 2
            [title] => cid:1, qid:1, oid:2
            [answer] => whatever
        )

    [1] => Array
        (
            [cat_id] => 1
            [ques_id] => 2
            [opt_id] => 3
            [title] => cid:1, qid:2, oid:3
            [answer] => whatever
        )

    [2] => Array
        (
            [cat_id] => 1
            [ques_id] => 4
            [opt_id] => 8
            [title] => cid:1, qid:4, oid:8
            [answer] => whatever
        )

    [3] => Array
        (
            [cat_id] => 1
            [ques_id] => 2
            [opt_id] => 1
            [title] => cid:1, qid:2, oid:1
            [answer] => whtvr
        )

)
// second print_r
Array
(
    [0] => Array
        (
            [cat_id] => 1
            [ques_id] => 1
            [opt_id] => 2
            [title] => cid:1, qid:1, oid:2
            [answer] => whatever
        )

    [1] => Array
        (
            [cat_id] => 1
            [ques_id] => 2
            [opt_id] => 1
            [title] => cid:1, qid:2, oid:1
            [answer] => whtvr, whatever
        )

    [2] => Array
        (
            [cat_id] => 1
            [ques_id] => 4
            [opt_id] => 8
            [title] => cid:1, qid:4, oid:8
            [answer] => whatever
        )

)

请注意,您在评论中表示option_id无关紧要,因此我没有替换它;如果需要,你也可以在array_reduce内的匿名函数中替换它。

答案 3 :(得分:0)

$data = array();

//$new is the array in which you have data originally
foreach($new as $arrayK => $arrayV){
    if(!isset($data[$arrayV['category_id']][$arrayV['question_id']]))
        $data[$arrayV['category_id']][$arrayV['question_id']] = $arrayV;
    else{
        $option = $data[$arrayV['category_id']][$arrayV['question_id']]['option_id'] . ',' . $arrayV['option_id'];
        $data[$arrayV['category_id']][$arrayV['question_id']]['option_id'] = $option;

        $answer = $data[$arrayV['category_id']][$arrayV['question_id']]['answer'] . ',' . $arrayV['answer'];
        $data[$arrayV['category_id']][$arrayV['question_id']]['answer'] = $answer;
    }
}

$ data将以所述格式提供所需数据 答案和选项都将以逗号分隔

答案 4 :(得分:0)

foreach($arr as $k=>$a)
{
    $common[$k] = search($arr,'category_id',$a['category_id'],'question_id',$a['question_id']);
    $answers = array_map(function($item) { return $item['answer']; }, $common[$k]);
    $options = array_map(function($item) { return $item['option_id']; }, $common[$k]);
    foreach($common[$k] as $temp)
    {
        $finalAns = $temp;
        $finalAns['answer'] = implode(",",$answers);
        $finalAns['option_id'] = implode(",",$options);
    }
    $final[] = $finalAns;
}

$final = array_map("unserialize", array_unique(array_map("serialize", $final)));
echo "<pre>";
print_r($final);

将下面的功能放在您的公共功能文件或同一个文件中。

function search($array, $key, $value, $key1, $value1)
{
    $results = array();

    if (is_array($array))
    {
        if (isset($array[$key]) && $array[$key] == $value && isset($array[$key1]) && $array[$key1] == $value1)
        {
            $results[] = $array;
        }

        foreach ($array as $subarray)
            $results = array_merge($results, search($subarray, $key, $value,$key1,$value1));
    }

    return $results;
}

您的输出将是

阵 (     [0] =&gt;排列         (             [category_id] =&gt; 1             [question_id] =&gt; 1             [option_id] =&gt; 2             [title] =&gt;你戴眼镜吗?             [answer] =&gt;没有         )

[1] => Array
    (
        [category_id] => 1
        [question_id] => 2
        [option_id] => 3,1
        [title] => Your hobbies ?
        [answer] => movies,travel
    )

[2] => Array
    (
        [category_id] => 1
        [question_id] => 4
        [option_id] => 8
        [title] => what is your status?
        [answer] => single
    )