dpjs2005
dpjs2005
2017-11-06 08:04

如何将多个关联数组合并为一个?

已采纳

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."
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • doushi7805 doushi7805 4年前

    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.

    点赞 评论 复制链接分享
  • dpquu9206 dpquu9206 4年前

    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>';
    
    点赞 评论 复制链接分享
  • dongxingguo1978 dongxingguo1978 4年前

    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;           
    }
    
    点赞 评论 复制链接分享
  • dtrgqjcd877528 dtrgqjcd877528 4年前

    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
                        ));
                    }   
                }
    
    点赞 评论 复制链接分享