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条)

报告相同问题?

悬赏问题

  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿