[英]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? 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每 2 个 arrays 以便常见问题解答和问题将在 1 个数组中?
Sorry for this question, I am really trying to understand this problem but no luck.对不起这个问题,我真的很想理解这个问题,但没有运气。 I appreciate any answers.
我很感激任何答案。
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.对于您给出的示例,这将是上述循环的 output。
{
"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 Output 是这样的
[
{
"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. "
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.