0

私のコードは次のとおりです。

POST の HTML 部分:

<form action='key.php' method='POST'> 
<input type='number' name='consumervar[]' value='512'/>
<input type='number' name='consumervar[]' value='256'/>
<input type='number' name='consumervar[]' value='1024'/>
<input type='submit'/>
</form>

key.php の PHP コード:

<?PHP
  foreach ($_POST as $key => $value) {
    $consumervar = $value*64;
  }
  print_r($consumervar); // this is for for debug (see array contents)
?>

しかし、すべてを実行すると、再現されます:

Fatal error: Unsupported operand types in /var/blahblah/blahblah/key.php on line 3

助けてください。正しく行う方法は?投稿されたすべての値に整数 64 を掛ける必要があります。

4

2 に答える 2

5

the loop should be

foreach($_POST['consumervar'] as $key => $value) {
              ^^^^^^^^^^^^^^^

as written, your code pulls out the ARRAY of consumervar values, which you try to multiply. You cannot "multiply" arrays in php.

As well, note that the $key/$value produced by the loop are simply copies of what exists in the array. You are not changing the array's values. For that, you should be doing

 $_POST['consumervar'][$key] = $value * 64;
于 2012-11-18T01:56:31.653 に答える
0

Try this:

<?php
    $arr = isset($_POST['consumervar']) ? $_POST['consumervar'] : array();
    if(!is_array($arr)) die('some_error');

    foreach($arr as $key => $value) {
        $arr[$key] = $value*64;
    }
    print_r($arr); // this is for for debug (see array contents)
?>

The $key corresponds to the "name" in the input element in HTML.

于 2012-11-18T01:56:42.197 に答える