1

我有一个多维数组

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
    )
) 

其中第二个和第四个数组包含相同的问题(相同的类别 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
    )
) 

意味着只结合它的答案。请帮忙 ...

4

5 回答 5

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如果需要,您也可以在内部的匿名函数中替换它。

于 2013-01-10T10:43:19.683 回答
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 将具有上述格式的所需数据
答案和选项都将用逗号分隔

于 2013-01-10T11:06:08.907 回答
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;
     }
}
于 2013-01-10T10:34:06.583 回答
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
    )

)
于 2013-01-10T10:40:26.397 回答
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;
}

你的输出将是

Array ( [0] => Array ( [category_id] => 1 [question_id] => 1 [option_id] => 2 [title] => 你戴眼镜吗?[answer] => 否 )

[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
    )

)

于 2013-01-10T12:21:47.257 回答