dongzi4030 2014-07-31 23:18
浏览 10
已采纳

PHP关联数组

I'm having some problems assigning values to a PHP array. I've read the help files and numerous articles here and I'm even more confused.

I have an HTML order form that's processed by PHP. The user enters the quantity, and, if necessary, a free text description of the item. Not all items have a free text description. The POST output is:

[_POST] => Array
    (
        [items_1] => 
        [items_2] => 
        [items_21] => 
        [items_68] => 
        [items_94] => 
        [items_501] => 2
        [txt_501] => Laserjet
        [items_510] => 
        [txt_510] => Item Description
        [items_511] => 
        [txt_511] => Item Description
        [PlaceOrder] => Place Your Order
    )

The value side of the items element is non-zero if the user entered a quantity on the form. The value side of the txt element can be user-entered or the default field description.

The object is to end up with an array of items which I can add to an orders database, ideally in the format "array(itemno)=count,txt" where itemno is the part to the right of the underscore, count is > 0 and txt is the value part of the txt element if there is a corresponding count. For this sample, the desired output would be items("501")="2,Laserjet"

My PHP code is:

foreach($_POST as $key => $value) {
    $_POST[$key] = filter($value); //POST variables are filtered for bad input
    $x = explode("_", $key);
    $itemno=$x[1];
    if ($x[0] = "txt") { $desc = $value; }
    if ($x[0] = "items") { $count = $value; }
    echo $itemno,"|", $count,"|",$desc,"|<br/>";
    if (($count > 0) and ($itemno > 0)) { $items[$itemno] = $count;}
}

I have reduced this down to that last line of code, and it doesn't work.

What the heck am I doing wrong?

  • 写回答

2条回答 默认 最新

  • doushan2811 2014-07-31 23:30
    关注

    Changing these two lines:

    if ($x[0] = "txt") { $desc = $value; }
    if ($x[0] = "items") { $count = $value; }
    

    to something like:

    if ($x[0] == "txt") { $items[$itemno][1] = $value; }
    elseif ($x[0] == "items") { $items[$itemno][0] = $value; }
    

    and completely dropping that last line should make you get the items array like this:

    $items[501] = array(2, 'Lasetjet');
    

    Which is then easy to convert into 2, Laserjet.


    So, the full code would look like this:

    foreach($_POST as $key => $value) {
        $_POST[$key] = filter($value); //POST variables are filtered for bad input
        $x = explode("_", $key);
        $itemno = $x[1];
        if ($x[0] == "txt") { $items[$itemno][1] = $value; }
        elseif ($x[0] == "items") { $items[$itemno][0] = $value; }
    }
    
    var_dump($items); // see the whole array
    

    UPDATE

    Noticed a huge error - you were using the assignment operator (=) instead of comparison (==).

    See the full working example here: http://ideone.com/kETOvZ

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示