fazary
fazary

Reputation: 5

Build an array out of another with a different structure

I have this array as follow:

 Array ( 
[pageid_01_page_name] => first Page  
[pageid_01_Style] => full 
[thePageID_1] => pageid_01 
[theElementID_1] => 1 
[source_type_1] => slideshow_box 
[pageid_01_1_slideshow_box] => Array ( [layout] => one 
                                  [content_id] => Array ( [0] => 856 ) 
                                  [slideshow_timeout] => 8 
                                  [slideshow_height] => 400
                                  [image_resize] => on 
                                  [image_crop] => on 
                                  [list_orderby] => date 
                                  [list_order] => DESC 
                                  [slideshow_buttons] => on ) 

[thePageID_2] => pageid_01 
[theElementID_2] => 2 
[source_type_2] => banner_box 
[pageid_01_2_banner_box] => Array ( [layout] => one 
                                     [text] => test 
                                     [button_text] => Button Text 
                                     [button_link] => a link ) 

              )

I need to build another array out of the one above, with this structure:

  Array ( 
  [pages] => Array ( [pageid_01] => stdClass Object 
                       ( [pageName] => first Page
                       [style] => full
                       [pageID] => pageid_001 

                       [contents] => Array ( 
                       [2] => stdClass Object  ( 
                          [element_id] => 1 
                          [content_type] => slideshow_box
                          [layout] => one 
                          [content_id] => Array ( [0] => 856 )
                          [slideshow_timeout] => 8 
                          [slideshow_height] => 400 
                          [image_resize] => on 
                          [image_crop] => on 
                          [list_orderby] => date 
                          [list_order] => DESC 
                          [slideshow_buttons] => on 
                                               ) 
                        [3] => stdClass Object ( 
                        [element_id] => 2 
                        [content_type] => banner_box 
                        [layout] => one 
                        [text] => test
                         [button_text] =>Button text 
                         [button_link] => a link 
                                              ) 
          )

Updated with more details:

The way i'm building the second array right now is like this:

$pages  =  new \stdClass();
$i=0;
$page=0;
$thepagename = '_page_name';

if(isset($thepagename))
{
  if(in_array($thepagename, $AllPages))
  { foreach($AllPages as $k => $v) {
     $page = str_replace('_page_name','',$k); 
    @$pages->pages[$page]->pageID   = $page; 
    @$pages->pages[$page]->pageName     = $v;
    if(stristr($k, 'theElementID_') == true) {          
                    $element_id = $v;
                }
    if(stristr($k, '_slideshow_box') == true) {
        $i++;
        @$pages->pages[$page]->contents[$i]->element_id           =  $element_id; 
        $pages->pages[$page]->contents[$i]->content_type       =  'slideshow_box';
        $pages->pages[$page]->contents[$i]->layout             =  $v["layout"];
        $pages->pages[$page]->contents[$i]->content_id         =  @$v["content_id"];
        $pages->pages[$page]->contents[$i]->slideshow_timeout     =  $v["slideshow_timeout"];
        $pages->pages[$page]->contents[$i]->slideshow_height      =  $v["slideshow_height"];
        $pages->pages[$page]->contents[$i]->image_resize       =  $v["image_resize"];
        $pages->pages[$page]->contents[$i]->image_crop         =  $v["image_crop"];
        $pages->pages[$page]->contents[$i]->list_orderby       =  $v["slideshow_orderby"];
        $pages->pages[$page]->contents[$i]->list_order         =  $v["slideshow_order"]; 
        $pages->pages[$page]->contents[$i]->slideshow_buttons     =  $v["slideshow_buttons"]; 
  }

  }
    }
}

and so on.... but this way takes a lot of coding while the contents can be much much more...

Upvotes: 0

Views: 54

Answers (1)

Allan
Allan

Reputation: 2096

First of all,you can loop your array, get all values and keys, The second,built another array, push all key and values into the new array. just like

$new_array = ($new_array,values1,values2,.......);

hope it can help you

Upvotes: 1

Related Questions