doumeng1897 2016-02-17 14:19
浏览 34
已采纳

PHP - 发送多维数组并解析以传递给函数

I'm currently building an MVC-ish structure on our project. My objective is to create panels with a function call. However, I'm currently stuck on how to pass a call to a function, that requires two parameters, within a foreach loop.

Function Call (not working)

        $display_panel_create->get_display_create_panel($size=6, $title='Guest',
          $field=['join_two_fields' => ['field1' => 'first_name','field2' => 'last_name'], 'email', 'phone',
              'company','address', 'comments'], $action='Create', $type='success');

The receiving method is:

function get_display_create_panel($size, $title, $field, $action, $type){

  $panel = '';

  $panel .= $this->panel_size($size);
  $panel .= $this->panel_header($title, $action, $type);
  $panel .= $this->panel_form($action, $title);
  $panel .= $this->panel_body($field);
  $panel .= $this->button($type);
  $panel .= $this->panel_footer();

return $panel;

}

As we create new "parts", the body call is a foreach loop that does the following:

protected function panel_body($field){

$body = '';


foreach ($field as $item){

  if (is_array($item)){

    foreach ($item as $k => $v){

      if ($k == 'join_two_fields'){

        $body .= $this->$k($field1 = $v['field1'], $field2=$v['field2']);
        unset ($item[$k]);

      } else { $body .= $this->$k($v); }

    }

  } else { $body .= $this->$item(); }
}

return $body;
}

However, a panel is not being created. I tried using test.php file to make sure my looping is correct, and I realized that my issue is that during the initial loop, i'm not sending both variables for $field1 and $field2.

Can anyone suggest a more elegant solution?

--------- test.php code --------

$array = [$size=6, $title='Guest',
          $field=[
            'join_two_fields' =>
              ['field1' => 'first_name','field2' => 'last_name'],
            'email', 'phone', 'company','address', 'comments'],
          $action='Create', $type='success'
         ];

foreach ($array as $item){

  if (is_array($item)){

    foreach ($item as $k => $v){

      if ($k == 'join_two_fields'){
        echo $v['field1'] . '<br>';
        echo $v['field2'] . '<br>';

      }
      else { echo $v . '<br>'; }

    }

  } else { echo $item . '<br>'; }
}

--------test.php output---------

6 Guest first_name last_name e e phone company address comments Create success

  • 写回答

3条回答 默认 最新

  • dongshao1021 2016-02-17 15:54
    关注

    The problem is the way you are iterating. Lets break it down:

    you first run this:

    foreach ($field as $item) {
    

    This will set your $item as all the array's values such as this mocukup:

    "join_two_fields" => $item,
    0 => $item,
    1 => $item,
    2 => $item,
    3 => $item,
    4 => $item
    

    Then you check if $item is an array , and if it is you loop through $item with another foreach' inside:

    foreach ($field as $item) {
            if (is_array($item)) {
                foreach ($item as $k => $v) {
    

    which then $item would be something like this:

         $k => $v
    
         OR
    
        'field1' => 'first_name' 
        'field2' => 'last_name' 
    

    so far so good, ts working, here comes the problem. After your iterations, you have your condition if ($k == 'join_two_fields') { which checks if the current key equals "join_two_fields", well this will never happen . What you mean to check here is the original key of your first iteration, not the key in the second iteration.

    To fix this, redo your iteration in this manner, remove your second foreach as you do not need to iterate over the fields:

        foreach ($field as $field_key => $item) {
            if (is_array($item)) {
                if ($field_key == 'join_two_fields') {
                    $body .= $this->$field_key($field1 = $item['field1'], $field2 = $item['field2']);
                } else {
                    $body .= $this->$field_key($item);
                }
            } else {
                $body .= $this->$item();
            }
        }
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程