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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算