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

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

手段はその答えだけを組み合わせます。助けてください ...

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;
}

あなたの出力は

配列 ( [0] => 配列 ( [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,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 に答える