array_replace() / array_merge() | ($_SESSION = array())参数不是数


array_replace() / array_merge() | ( $_SESSION = array() ) argument is not an array?

我有这个代码为我的学校项目,并认为代码做它的工作,我想要它做什么,我仍然不断得到错误关于$_SESSION[]不是一个数组参数时使用array_replace()array_merge()函数:

会话已经在报头上启动:

 // Start Session
 session_start();

初始化$_SESSION['cart']为数组:

// Parent array of all items, initialized if not already...
if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = array();
}

用于从下拉菜单中添加产品:-(只是为了查看会话是如何分配的:)

if (isset($_POST['new_item'])) { // If user submitted a product
 $name = $_POST['products']; // product value is set to $name
// Validate adding products:
if ($name == 'Select a product') { // Check if default - No product selected
    $order_error =  '<div class="center"><label class="error">Please select a product</label></div>';
} elseif (in_array_find($name, $_SESSION['cart']) == true) { // Check if product is already in cart:
    $order_error = '<div class="center"><label class="error">This item has already been added!</label></div>';
} else {
    // Put values into session:
    // Default quantity = 1:
    $_SESSION['cart'][$name] = array('quantity' => 1);
       }
}

当他们试图更新产品时,问题就来了:

// for updating product quantity:
if(isset($_POST['update'])) {
// identify which product to update:
$to_update = $_POST['hidden'];
// check if product array exist:
if (in_array_find($to_update, $_SESSION['cart'])) {
    // Replace/Update the values:
    // ['cart'] is the session name
    // ['$to_update'] is the name of the product
    // [0] represesents quantity
    $base = $_SESSION['cart'][$to_update]['quantity'] ;
    $replacement = $_SESSION['cart'][$to_update] = array('quantity' => $_POST['option']);
    array_replace($base, $replacement);
    // Alternatively use array merge for php < 5.3
    // array_merge($replacement, $base);
    }
}

请注意,array_replace()array_merge()函数都在更新值,并做最初的目标是什么,但问题是,我仍然继续获得一个参数($base)不是数组问题。

警告:array_replace() [function。

:参数#1不是数组 任何关于解决这个问题的更好方法的建议都将是有价值的帮助:)谢谢你的帮助:)

编辑:in_array_find()是我在替换in_array()中使用的函数,因为它不适用于在多维数组中查找值:特别是2深度数组:

从这里找到它,它适用于我

它的代码是:

// Function for searching values inside a multi array:
function in_array_find($needle, $haystack, $strict = false) {
foreach ($haystack as $item => $arr) {
    if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
        return true;
    }
}
return false;
}

array_replace返回被替换的数组。所以你需要做:

$base=array_replace($base, $replacement);

另外,我建议始终使用命名键,而不是混合使用命名键和数字键:

$base = $_SESSION['cart'][$to_update]['quantity'];
编辑:

这可能不是你想要的…

我设置了一个没有使用$_SESSION的测试场景,我得到了和你一样的错误。我更改了代码如下,不再得到错误。

$sesh=array(
    'cart'=>array(
        0=>array(
            'quantity'=>1
        )
    )
);
$to_update=0;
$new_quantity=5;
//$base = $sesh['cart'][$to_update]['quantity']; <--- changed this to line below
$base = $sesh['cart'][$to_update];
$replacement = $sesh['cart'][$to_update] = array('quantity' => $new_quantity);
$base=array_replace($base, $replacement);
echo"<pre>";print_r($base);echo"</pre>";

PHP FIDDLE: http://phpfiddle.org/main/code/mvr-shr

这已经解决了这个问题:

基本上按照结构:(让我们把它切成几位)

$_SESSION['cart'] = array();
然后

$_SESSION['cart'][$name] = array('quantity' => 1);
最后

:

$base = $_SESSION['cart'][$to_update]['quantity'] ;
$replacement = $_SESSION['cart'][$to_update] = array('quantity' => $_POST['option']);
array_replace($base, $replacement);

它说参数$base不是数组的原因是:

$base中的['quantity']不是一个数组,因为它构成了'quantity' => 1,我们需要的是array('quantity' => 1);中的array()值,以便将其识别为数组。

所以最终答案应该是:$base = $_SESSION['cart'][$to_update];因为只有一个array()记录在$to_update所在的位置,所以replace参数应该替换这个标识的数组。

$_SESSION仅在会话启动时存在。为了做到这一点,你需要在所有代码的顶部添加session_start(),否则会话将不会启动,除非你在php指令中定义它。