How to merge the array elements in between?

97 Views Asked by At

I have these arrays for my navbar.

$all_pages = array(
    'home'      => 'Home',
    'about'     => 'About us',
    'gallery'   => array(
        'photo-gallery' => 'Photo Gallery',
        'video-gallery' => 'Video Gallery'
    ),
    'contact'   => 'Contact us'
);

I want to obtain an array like this

    'home'      => 'Home',
    'about'     => 'About us',
    'photo-gallery' => 'Photo Gallery',
    'video-gallery' => 'Video Gallery'
    'contact'   => 'Contact us'

So far I've tried this code

$gall_pages = $all_pages['gallery'];
$pages = $all_pages;
unset($pages['gallery']);
$pages = array_merge($pages, $gall_pages);

But I get this kind of array

    'home'      => 'Home',
    'about'     => 'About us',
    'contact'   => 'Contact us'
    'photo-gallery' => 'Photo Gallery',
    'video-gallery' => 'Video Gallery'

Is there any way to insert those gallery pages in between 'about' & 'contact'?

5

There are 5 best solutions below

0
On BEST ANSWER

Solution with array_walk_recursive which eliminates the need for if check in foreach:

$all_pages = array(
    'home'      => 'Home',
    'about'     => 'About us',
    'gallery'   => array(
        'photo-gallery' => 'Photo Gallery',
        'video-gallery' => 'Video Gallery'
    ),
    'contact'   => 'Contact us'
);
$new = [];
array_walk_recursive(
    $all_pages, 
    function ($value, $key) use (&$new) { $new[$key] = $value; }
);
print_r($new);

Fiddle here.

2
On

Can you try the below code

<?php
$all_pages = array(
    'home'      => 'Home',
    'about'     => 'About us',
    'gallery'   => array(
        'photo-gallery' => 'Photo Gallery',
        'video-gallery' => 'Video Gallery'
    ),
    'contact'   => 'Contact us'
);

$result = array();
foreach($all_pages as $key => $menu){
    if(is_array($menu)){
        $result = array_merge($result, $menu);
    }else{
        $result[$key] = $menu;
    }
}
print_r($result ); 
0
On
<?php
$all_pages = array(
    'home'      => 'Home',
    'about'     => 'About us',
    'gallery'   => array(
        'photo-gallery' => 'Photo Gallery',
        'video-gallery' => 'Video Gallery'
    ),
    'contact'   => 'Contact us'
);

$pages=[];
foreach($all_pages as $key=>$page){

    if(is_array($page)){
        foreach($page as $key_1=>$sub_page){

                $pages[$key_1]=$sub_page;
        }

    }
    else{
    $pages[$key]=$page;

    }
}
echo "<pre>";
print_r($pages);
0
On

you can do this with one line of code using array_slice

$gall_pages = $all_pages['gallery'];
$pages = $all_pages;
unset($pages['gallery']);
$offset = 2 ;
$new = array_slice($pages, 0, $offset, true) + $gall_pages +array_slice($pages, $offset, NULL, true);

you will get your desired array

0
On

You can try the below function :

function flatten($arrayIn) {
  $arrayOut = array();
  foreach(array_keys($arrayIn) as $key) {
    if(is_array($arrayIn[$key]))
      $arrayOut = array_merge($arrayOut, flatten($arrayIn[$key]));
    elseif(is_string($arrayIn[$key])) {
      $arrayOut[$key] = $arrayIn[$key];
    }
  }
  return $arrayOut;
}

Explanation: It extracts recursively (whatever the number of recursion is needed to flatten the whole array) out of the array the string values and their respective keys. And as a whole script :

<?php
$all_pages = array(
  'home'      => 'Home',
  'about'     => 'About us',
  'gallery'   => array(
    'photo-gallery' => 'Photo Gallery',
    'video-gallery' => 'Video Gallery'
  ),
  'contact'   => 'Contact us'
);

function flatten($arrayIn) {
  $arrayOut = array();
  foreach(array_keys($arrayIn) as $key) {
    if(is_array($arrayIn[$key]))
      $arrayOut = array_merge($arrayOut, flatten($arrayIn[$key]));
    elseif(is_string($arrayIn[$key])) {
      $arrayOut[$key] = $arrayIn[$key];
    }
  }
  return $arrayOut;
}
echo "<pre>";
print_r(flatten($all_pages));
echo "</pre>";
?>