donglvlao8367 2012-11-26 05:11
浏览 51

多维数组和array_push?

I have a huge multidimensional array, let's name it $big_array.

In addition I have this set of data that I have to put into above array:

$input = array('one','two','three','four');

That's what I need to push into $big_array (based on $input from above):

 $value = array(
   'id' => $number,
   'title' => 'foo',
   'type' => 'options',
   'options' => array('one','two','three','four'),
   'page' => 'font_manager',
   );

  array_push($big_array, $value);

$big_array structure looks like this:

$big_array = array(
(...)

array(
 'id' => 'Bar',
 'title' => 'Foo',
 'type' => 'select',
 'page' => 'font_manager',
 ), 

 array(
  'id' => 'ss_font',
  'title' => __("Main font:",'solidstyle_admin'), 
  'type' => 'select',
  'page' => 'font_manager',
  'description' =>  __("This font will be used for all pages.",'solidstyle_admin'),
  'default' => '',
  'options' => array(
     'option1' => 'option1',
     'option2' => 'option12,
   ),
  ),

(...)
);

What I exactly want to do will look like that if it will be possible to include loops in arrays (yes, I know how wrong is that, just trying to explain it better):

$input = array('one','two','three','four');
$value = array(
       'id' => $number,
       'title' => 'foo',
       'type' => 'options',
       'options' => array(
         foreach($input as $number) {
           echo $number.',';
         };
        ),
       'page' => 'font_manager',
       );
  • 写回答

4条回答 默认 最新

  • drzfnr0275 2012-11-26 05:20
    关注

    Have a look at the code below. I have commented it so you can understand what I'm doing.

    $input = array('one','two','three','four');
    
    // set the base info here, i.e., info that is common to each push
    $baseInfo = array(   
       'title' => 'foo',
       'type' => 'options',   
       'page' => 'font_manager',
       );
    
    // now loop
    foreach($input as $number) {
        // fill in base info with data that is specific to this iteration
        $baseInfo['id'] = $number;
        $baseInfo['options'] = $input;
    
        // do the push
        array_push($big_array, $baseInfo);
    }
    

    I'm not entirely sure I got your question correct. If you were asking something else, please clarify and I will edit my answer.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度