Skip to content
Advertisement

Merge every 2 arrays in a Multidimensional array PHP

So, this is a complicated question for me. Let’s say we have an array that goes like this:

{
    "data": [
        
        {
            "id": "339",
            "post_id": "57",
            "meta_key": "faq_list_0_question",
            "meta_value": "Lorem ipsum dolor sit amet, consectetur adipiscing elit?"
        },
        {
            "id": "341",
            "post_id": "57",
            "meta_key": "faq_list_0_answer",
            "meta_value": "In at neque at nisl fringilla egestas sit amet tincidunt sem. Nunc rutrum risus sit amet metus viverra efficitur pharetra et ante. Aenean at lobortis nisl. "
        },
]
}

supposed to be FAQs question and answer are more than 1, the arrays above separates the faq question and faq answer in 2 different arrays and given that the array is a result of a 3rd party API (which you cannot control), how can I merge every 2 arrays so that faq answer and question will be in 1 array?

Sorry for this question, I am really trying to understand this problem but no luck. I appreciate any answers.

Advertisement

Answer

Just loop through your values and add them to a new array.

Something like this

$new_data = [];
foreach($array['data'] as $values) {
    
    //break meta_key into parts
    $key_explode = explode('_', $values['meta_key']);

    //the last part of $key_explode is the "type" (answer, question)
    //array_pop will remove the last array value of $key_explode
    $type = array_pop($key_explode);

    //implode values back together without "type", will be something like "faq_list_0"
    $key = implode('_', $key_explode);
    
    //add to new array. Group by $key, then $type
    $new_data[$key][$type] = $values;
    
}

For the example you gave, this would be the output of the above loop.

{
   "faq_list_0":{
      "question":{
         "id":"339",
         "post_id":"57",
         "meta_key":"faq_list_0_question",
         "meta_value":"Lorem ipsum dolor sit amet, consectetur adipiscing elit?"
      },
      "answer":{
         "id":"341",
         "post_id":"57",
         "meta_key":"faq_list_0_answer",
         "meta_value":"In at neque at nisl fringilla egestas sit amet tincidunt sem. Nunc rutrum risus sit amet metus viverra efficitur pharetra et ante. Aenean at lobortis nisl."
      }
   }
}

Here is something closer to the format you mentioned in your comment.

$new_data = [];
foreach($array['data'] as $values) {
    
    //break meta_key into parts
    $key_explode = explode('_', $values['meta_key']);

    //the last part of $key_explode is the "type" (answer, question)
    //array_pop will remove the last array value of $key_explode
    $type = array_pop($key_explode);
    
    //key will be the number at the end of $key_explode (before the type)
    $key = array_pop($key_explode);
    
    //add to new array. Group by $key, then $type
    $new_data[$key]['id'] = $key;
    $new_data[$key][$type] = $values['meta_value']; 
    
}

Output is something like this

[
   {
      "id":"0",
      "question":"Lorem ipsum dolor sit amet, consectetur adipiscing elit?",
      "answer":"In at neque at nisl fringilla egestas sit amet tincidunt sem. Nunc rutrum risus sit amet metus viverra efficitur pharetra et ante. Aenean at lobortis nisl. "
   }
]
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement