如何将多个关联数组合并为一个?
问题描述:
how can I merge this assoc arrays to one which will have the array(title, description)? GetContent function sort of data to my requirements.
function getContent($data) {
$tabs = $data->result->data->tab;
$type = findByType($tabs,'content');
$content = array();
foreach ($type->unified_content->item as $item) {
if($item->type->name == 'header') {
$content[] = array(
'title' => $item->text
);
} else {
$content[] = array(
'description' => $item->text
);
}
}
return $content;
}
vardump of foreach result, how can i merge title + description:
array(1) {
["title"]=>
string(13) "Test článku"
}
array(1) {
["description"]=>
string(20) "Nový článek test."
}
array(1) {
["title"]=>
string(15) "Test článku 2"
}
array(1) {
["description"]=>
string(22) "Nový článek test 2."
}
array(1) {
["title"]=>
string(15) "Test článku 3"
}
array(1) {
["description"]=>
string(22) "Nový článek test 3."
}
如何将这个assoc数组合并到一个具有数组(标题,描述)的数组? GetContent函数按我的要求排序数据。 p>
function getContent($ data){
$ tabs = $ data-> result-> data-> tab;
$ type = findByType($ tabs,'content');
$ content = array();
foreach($ type-> unified_content-> item as $ item){
if($ item-> type-> name =='header'){
$ content [] = array(\ n'title'=> $ item-> text
);
} else {
$ content [] = array(
'description'=> $ item-> text
);
}
}
返回$ content;
}
code> pre>
foreach结果的vardump,如何合并标题+说明: p>
array(1 ){
[“title”] =>
string(13)“Testčlánku”
}
array(1){
[“description”] =>
string(20)“Novýčlánek 测试。“
}
array(1){
[”title“] =>
string(15)”Testčlánku2“
}
array(1){
[”description“] = >
string(22)“Novýčlánektest2。”
}
array(1){
[“title”] =>
string(15)“Testčlánku3”
}
array (1){
[“description”] =>
string(22)“Novýčlánektest3。”
}
code> pre>
div>
答
If you content have strict order, you can use a temp array to store the title
and description
like this,
foreach ($type->unified_content->item as $item) {
if($item->type->name == 'header') {
$arr = [];
$arr['title'] = $item->text;
} else {
$arr['description'] = $item->text;
$content[] = $arr;
}
}
If not, you need to merge the array.
答
you can user array_merge
such as
foreach ($type->unified_content->item as $item) {
if($item->type->name == 'header') {
$content = array_merge($content,array(
'title' => $item->text
));
} else {
$content = array_merge($content,array(
'description' => $item->text
));
}
}
答
You can store title and description in one array and pass it to your $content array.
Replace this:
foreach ($type->unified_content->item as $item) {
if($item->type->name == 'header') {
$content[] = array(
'title' => $item->text
);
} else {
$content[] = array(
'description' => $item->text
);
}
}
With this:
$i = 0;
$content_temp = array();
foreach ($type->unified_content->item as $item) {
if($item->type->name == 'header') {
$content_temp['title'] = $item->text;
} else {
$content_temp['description'] = $item->text
}
$i++;
if($i % 2 == 0){
$content[] = $content_temp;
}
}
The Whole Code:
function getContent($data) {
$tabs = $data->result->data->tab;
$type = findByType($tabs,'content');
$content = array();
$i = 0;
$content_temp = array();
foreach ($type->unified_content->item as $item) {
if($item->type->name == 'header') {
$content_temp['title'] = $item->text;
} else {
$content_temp['description'] = $item->text
}
$i++;
if($i % 2 == 0){
$content[] = $content_temp;
}
}
return $content;
}
答
array_merge() is more efficient but there are a couple of options:
$array1 = array("id1" => "value1");
$array2 = array("id2" => "value2", "id3" => "value3", "id4" => "value4");
$array3 = array_merge($array1, $array2/*, $arrayN, $arrayN*/);
$array4 = $array1 + $array2;
echo '<pre>';
var_dump($array3);
var_dump($array4);
echo '</pre>';