使用不同的结构构建另一个阵列

时间:2013-01-11 01:18:51

标签: php multidimensional-array

我有这个数组如下:

 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 ) 

              )

我需要使用以下结构构建另一个数组:

  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 
                                              ) 
          )

更新了更多详情:

我现在构建第二个阵列的方式是这样的:

$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"]; 
  }

  }
    }
}

等......  但这种方式需要大量编码,而内容可以更多......

1 个答案:

答案 0 :(得分:1)

首先,您可以循环您的数组,获取所有值和键,第二个,构建另一个数组,将所有键和值推送到新数组中。就像

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

希望它可以帮到你