0

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."
}
Hassan Raza
  • 106
  • 12
David
  • 57
  • 8
  • Would you show us the 2 arrays to be merged? – Mark Nov 06 '17 at 08:06
  • Possible duplicate of [PHP: merge two arrays while keeping keys instead of reindexing?](https://stackoverflow.com/questions/3292044/php-merge-two-arrays-while-keeping-keys-instead-of-reindexing) – kabanus Nov 06 '17 at 08:08
  • Don't forget the good olde array `+` operator. – biziclop Nov 06 '17 at 08:10

4 Answers4

1

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.

LF00
  • 27,015
  • 29
  • 156
  • 295
0

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
                    ));
                }   
            }
long
  • 86
  • 5
  • Its bad, because the result is only last array:string(15) "Test článku 3" string(22) "Nový článek test 3." – David Nov 06 '17 at 09:36
0

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;           
}
Bluetree
  • 1,324
  • 2
  • 8
  • 25
0

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>';
Hassan Raza
  • 106
  • 12