【问题标题】:PHP: Remove invalid array value?PHP:删除无效的数组值?
【发布时间】:2011-08-15 22:55:23
【问题描述】:

我使用了两个数组,一个是 POST 字符串(产品),另一个是预定义的一组值(产品)。

他们一起工作:

$products[$_POST['product']]  

如果 POST 值正好是以下之一,这没关系:

 $products = array(      
     'Pineaple' => 500, 'Banana' => 50, 'Mango' => 150,       
     'Milk' => 500, 'Coffe' => 1200, 'Butter' => 300,      
     'Bread' => 450, 'Juice' => 780, 'Peanuts' => 800,      
     'Yogurt' => 450, 'Beer' => 550, 'Wine' => 2500,    
 );    

否则会弹出未定义索引消息。由于这是一个会话,我需要以某种方式保留一个项目列表,但从数组中删除无效的项目并回显一条消息。

请查看截图:http://img36.imageshack.us/img36/9121/20110430004019.jpg

这是我正在使用的脚本:

<?php
session_start();

//Getting the list
 $_SESSION['list'] = isset($_SESSION['list']) ? $_SESSION['list'] : array();    

 //stock    
 $products = array(      
     'Pineaple' => 500, 'Banana' => 50, 'Mango' => 150,       
     'Milk' => 500, 'Coffe' => 1200, 'Butter' => 300,      
     'Bread' => 450, 'Juice' => 780, 'Peanuts' => 800,      
     'Yogurt' => 450, 'Beer' => 550, 'Wine' => 2500,    
 );    

 //Saving the stuff    
 $new_item = array(      
     'item' => $_POST['product'],       
     'quantity' => $_POST['quantity'],     
     'code' => $_POST['code'],      
     'price' => $products[$_POST['product']] * $_POST['quantity'],    

 );

$new_product = true;    
foreach($_SESSION['list'] as $key => $item) {      
    if ($item['item'] == $new_item['item']) {        
    $_SESSION['list'][$key]['quantity'] += $new_item['quantity'];        
    $_SESSION['list'][$key]['price'] = 
            $products[$new_item['item']] * $new_item['quantity'];        
    $new_product = false;
    }    
}   

if ($new_product) {      
    $_SESSION['list'][] = $new_item;        
}    

//listing    
echo  "<b>SHOPPING LIST</b></br>";    
foreach($_SESSION['list'] as $key => $item) {       
    echo 'Product .'. $key. ' '. $item['item'], ' ', 
          $item['quantity'], ' units: ', $item['price']. '<br />';    
}

echo "</br> <a href='index.html'>Return to index</a> </br>";

//Printing session
var_dump($_SESSION);

//session_destroy();
?>

【问题讨论】:

  • 您没有指定使项目无效的原因。是否有包含有效项目 ID 或其他内容的数据库?
  • 我编辑了问题,希望简洁。

标签: php arrays session indexing undefined


【解决方案1】:

我想你想使用array_intersect_assoc 函数。它返回两个数组中的项目。

【讨论】:

    【解决方案2】:

    你应该在阅读之前使用issetarray_key_exists检查密钥是否存在:

    if (isset($products[$_POST['product']])) {
        // product in stock
    } else {
        // product not in stock
    }
    

    如果您在$_SESSION['list'] 中为您的产品列表使用相同的密钥,则无需遍历整个列表即可在列表中找到相同的产品:

    if (isset($_SESSION['list'][$_POST['product']])) {
        // update product in list
        $_SESSION['list'][$_POST['product']]['quantity'] += $_POST['quantity'];
        $_SESSION['list'][$_POST['product']]['price'] = $products[$_POST['product']] * $_POST['quantity'];
    } else {
        // add product to list
        $_SESSION['list'][$_POST['product']] = array(
            'item'     => $_POST['product'],
            'quantity' => $_POST['quantity'],
            'code'     => $_POST['code'],
            'price'    => $products[$_POST['product']] * $_POST['quantity'],
        );
    }
    

    请注意,这始终只会将项目添加到列表中。您应该添加一些功能来更新和删除项目。并且不要忘记在使用数据之前对其进行验证(例如,如果人们输入负数,他们就不会获得退款)。

    【讨论】:

    • 一般来说这不太正确,试试这个:$a = array('foo' =&gt; null); var_dump(isset($a['foo'])); var_dump(array_key_exists('foo', $a)); 我总是建议人们使用 array_key_exists()。
    • @Robin:你是对的。但我怀疑他是否会在一系列价格中使用null
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2016-05-23
    • 1970-01-01
    相关资源
    最近更新 更多