将2个数组与特定元素合并

时间:2018-09-28 05:18:04

标签: php arrays

让我说我有2个这样的数组

数组问题:

array (size=4)
  0 => 
    array (size=2)
      'id' => string '4'
      'question' => string 'what food do you like?'
  1 => 
    array (size=2)
      'id' => string '5'
      'question' => string 'where do you want to go?'
  2 => 
    array (size=2)
      'id' => string '6'
      'question' => string 'are you busy?'
  3 => 
    array (size=2)
      'id' => string '7'
      'question' => string 'are you enjoy the party?'

第二个数组是Answer数组:

array (size=3)
  0 => 
    array (size=2)
      'id' => string '4'
      'answer' => string 'burger'
  1 => 
    array (size=2)
      'id' => string '5'
      'answer' => string 'go to mall'
  2 => 
    array (size=2)
      'id' => string '6'
      'answer' => string 'no im not'

我如何合并两个数组变成这样

id : 4
question : 'what food do you like?'
answer : 'burger'

id : 5
question : 'where do you want to go?'
answer : 'go to mall'

id : 6
question : 'are you busy?'
answer : 'no im not'

id : 7
question : 'are you enjoy the party?'
answer : ''

如果某个问题没有答案,我需要添加答案”(空)。我怎样才能合并该数组

3 个答案:

答案 0 :(得分:1)

此代码将为您提供所需的结果。对于每个问题,它看起来要看在答案数组中是否有答案。如果存在,则将id,问题和答案压入,否则将ID和问题以及空白答案压入结果数组。请注意,它使用了一些偏执的代码(在array_combine的键和值上使用$answers),但这意味着它不管$questions和{{1 }}:

$answers

输出:

$questions = array (
    array (
      'id' =>  '4',
      'question' =>  'what food do you like?'),
    array (
      'id' =>  '6',
      'question' =>  'are you busy?'),
    array (
      'id' =>  '5',
      'question' =>  'where do you want to go?'),
    array (
      'id' =>  '7',
      'question' =>  'are you enjoy the party?')
    );

$answers = array (
    array (
      'id' =>  '6',
      'answer' =>  'no im not'),
    array (
      'id' =>  '4',
      'answer' =>  'burger'),
    array (
      'id' =>  '5',
      'answer' =>  'go to mall')
    );

$qanda = array();
foreach ($questions as $question) {
    $id = $question['id'];
    $akey = array_search($id, array_combine(array_keys($answers), array_column($answers, 'id')));
    $qanda[] = array('id' => $id,
                     'question' => $question['question'],
                     'answer' => ($akey !== false) ? $answers[$akey]['answer'] : '');
}
print_r($qanda);

答案 1 :(得分:0)

您可以使用以下简单代码:

$arrayFirst = array(
    0 => array('id' => '4', 'question' => 'what food do you like?'),
    1 => array('id' => '5', 'question' => 'where do you want to go?'),
    2 => array('id' => '6', 'question' => 'are you busy?'),
    3 => array('id' => '7', 'question' => 'are you enjoy the party?')
);

$arraySecond = array(
    0 => array('id' => '4', 'answer' => 'burger'),
    1 => array('id' => '5', 'answer' => 'go to mall'),
    2 => array('id' => '6', 'answer' => 'no im not')
);

$arrayThird = array();

foreach ($arrayFirst as $key => $value) {

    $arrayThird[$key] = $value;
   if (isset($arraySecond[$key])) {

    $arrayThird[$key]['answer'] = $arraySecond[$key]['answer'];

}else{

    $arrayThird[$key]['answer'] = "";

}
}

echo "<pre>";
print_r($arrayThird);

输出

   Array
    (
    [0] => Array
        (
            [id] => 4
            [question] => what food do you like?
            [answer] => burger
        )

    [1] => Array
        (
            [id] => 5
            [question] => where do you want to go?
            [answer] => go to mall
        )

    [2] => Array
        (
            [id] => 6
            [question] => are you busy?
            [answer] => no im not
        )

        [3] => Array
            (
                [id] => 7
                [question] => are you enjoy the party?
                [answer] => 
         )

    )

答案 2 :(得分:0)

您可以简单地遍历数组,然后根据答案ID检查问题ID,将匹配的集合推送到新数组中。

  $qList = array
  (
  array("id"=>"1", "question"=>"What is your name?"),
  array("id"=>"2", "question"=>"What is your age?"),
  array("id"=>"3", "question"=>"What is your hobbies?")
  );

$aList = array
  (
  array("id"=>"1", "answer"=>"tjandra"),
  array("id"=>"2", "answer"=>"..."),
);


$final = array();
foreach($qList as $x => $x_value) {
  $qId = $x_value["id"];
  $aFound = false;
   foreach($aList as $ans => $ans_value) {
       $aId = $ans_value["id"];
        if($aId == $qId){
            $aFound = true;
            $innerCont = array();
            $innerCont["id"] = $aId;
            $innerCont["question"] = $x_value["question"];
            $innerCont["answer"] = $ans_value["answer"];
            array_push($final,  $innerCont);
            break;
         }
   }
   if(!$aFound) {
            $innerCont = array();
            $innerCont["id"] = $aId;
            $innerCont["question"] = $x_value["question"];
            $innerCont["answer"] = "";
            array_push($final,  $innerCont);

   }
}
var_dump($final);