donglanfu5831 2016-07-26 08:01
浏览 17
已采纳

从动态表单帖子中收集和格式化数据

I'm working on a web project and one thing I need to know is how to get the number of a array item and use that number to get an item from a different array, I've setup a page to dynamically add Bootstrap Panels with 2 input boxes in them.

<div class="panel panel-default">
    <div class="panel-heading">
        <a data-toggle="collapse" data-parent="#accordion" href="#<?php echo $index; ?>">
            <h4 class="panel-title">
                <?php echo $menu[0]; ?> <b class="caret pull-right"></b>
            </h4>
        </a>
    </div>
    <div id="<?php echo $index; ?>" class="panel-collapse collapse">
        <div class="panel-body">

            <div class="form-group">
                <label for="menu_name[]">Name:</label>
                <input id="menu_name[]" name="menu_name[]"placeholder="Name">
            </div>

            <div class="form-group">
                <label for="menu_link[]">Link:</label>
                <input id="menu_link[]" name="menu_link[]" value="<?php echo $menu[1]; ?>" placeholder="URL">
            </div>

            <div class="btn-group pull-right">
                <button class="btn btn-danger" type="submit" name="delete" value="<?php echo $index; ?>"><i class="fa fa-trash"></i> Delete</button>
            </div>

        </div>
    </div>

This form is for adding menus to create an array with each menu item's text and link and when I submit the form this is the result I get:

[["Home", "News"], ["http://localhost/", "http://localhost/news/"]]

But I want to format the array so that each menu item in an individual array instead of splitting the 2 options between arrays, here's an example of the output I want to achieve:

[["Home", "http://localhost"], ["News", "http://localhost/news"]]

I'm not sure how to do this and I've looked all over trying to find a solution or example to help but I can't find anything, I was doing some research on array_search but I'm not sure if that's what I want.

  • 写回答

1条回答 默认 最新

  • dongluan6784 2016-07-26 08:39
    关注

    If im not wrong about problem, this's the solution.

    $json = '[["Home", "News"], ["http://localhost/", "http://localhost/news/"]]';
    
    list($names, $links) = json_decode($json);
    
    for ($k=0; $k<count($names); $k++)
        echo sprintf('Link is "%s" for "%s"', $links[$k], $names[$k]).PHP_EOL;
    

    UPDATE

    $json = '[["Home", "News"], ["http://localhost/", "http://localhost/news/"]]';
    $output = array();
    
    list($names, $links) = json_decode($json);
    
    for ($k=0; $k<count($names); $k++)
       $output[] = array($names[$k], $links[$k]);
    

    Then you can see array with print_r function.

    print_r($output);
    

    And the json with json_encode

    echo json_encode($output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器