dsgdsf12312 2017-07-13 21:56
浏览 67
已采纳

PHP:获取带有Tag Name的元素索引

I'm trying to get the index of an HTML element and pass this to a PHP variable to eventually upload to a database.

<form method="post" action="insert.php" enctype="multipart/form-data">
    ....
    <div><textarea name="paragraph[]"></textarea></div> //index 0
    <div><textarea name="paragraph[]"></textarea></div> //index 1
    <div><textarea name="paragraph[]"></textarea></div> //index 2
    ....
</form>

if(isset($_POST['paragraph'])) {

    foreach ( $_POST['paragraph'] as $paragraph){

    //get index of container div for this paragraph and store it in a variable

    }
}

Looking at this Stack Overflow answer, I'm guessing I should do something like:

$divs = $dom->getElementsByTagName('div');

However, instead of getting all the <div> elements and storing them in an array, is there any way I can get the current <div> element from within the foreach loop and store its index in a variable?

Some clarification: The form is dynamic. So it could look like this, too:

<form>
    <div><textarea name="paragraph[]"></textarea></div> //index 0
    <div><textarea name="something_else"></textarea></div> index 1
    <div><textarea name="paragraph[]"></textarea></div> //index 2
    <div><textarea name="paragraph[]"></textarea></div> //index 3
</form>
  • 写回答

2条回答 默认 最新

  • dongre1907 2017-07-13 22:14
    关注

    You could use a base array to hold all your items such as the below HTML:

    <div><textarea name="items[][paragraph]"></textarea></div> //index 0
    <div><textarea name="items[][something_else]"></textarea></div> index 1
    <div><textarea name="items[][paragraph]"></textarea></div> //index 2
    <div><textarea name="items[][paragraph]"></textarea></div> //index 3
    

    Then when you access $_POST['items'] you will have them listed with their indexes:

    array (size=1)
      'items' => 
        array (size=4)
          0 => 
            array (size=1)
              'paragraph' => string 'test' (length=4)
          1 => 
            array (size=1)
              'something_else' => string 'test 2' (length=6)
          2 => 
            array (size=1)
              'paragraph' => string 'test 3' (length=6)
          3 => 
            array (size=1)
              'paragraph' => string 'test 4' (length=6)
    

    You can get all the information held in this array easily using the below loop:

    foreach($_POST['items'] as $index => $item) {
      echo "Index is: " . $index;
      echo "Key is: " . key($item);
      echo "Value is: " . $item[key($item)];
    }
    

    Which will print:

    Index is: 0
    Key is: paragraph
    Value is: test
    
    Index is: 1
    Key is: something_else
    Value is: test 2
    
    etc.
    

    If you want to get all values for paragraph in one easy swoop you can use array_column to access these, for example the below:

    array_column($_POST['items'], 'paragraph');
    

    Will print:

    array (size=3)
      0 => string 'test' (length=4)
      1 => string 'test 3' (length=6)
      2 => string 'test 4' (length=6)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题