【问题标题】:array_replace() / array_merge() | ( $_SESSION = array() ) argument is not an array?array_replace() / array_merge() | ( $_SESSION = array() ) 参数不是数组吗?
【发布时间】:2013-04-21 03:43:51
【问题描述】:

我有这个代码用于我的学校项目,并认为代码可以按照我想要的方式完成工作,但在使用 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.array-replace]:参数 #1 不是...中的数组

任何有关解决此问题的更好方法的建议都将是宝贵的帮助:) 谢谢你的帮助:)

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

here 找到它,它对我有用

它的代码是:

// 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;
}

【问题讨论】:

  • 我不熟悉in_array_find()。这是您自己的功能还是您的意思是in_array()
  • 谢谢。 @showdev 我已经添加了该函数的代码 :),我正在使用它来替换多维数组的 in_array()
  • 明白了,谢谢。如果你在错误之前echo"&lt;pre&gt;";print_r($_SESSION);echo"&lt;/pre&gt;";,你会得到什么?
  • 首先我在没有提交时得到这个[cart] =&gt; Array() = 用于购物车,当我更新值时我得到这个( [quantity] =&gt; 1 ))) = 出现错误的地方。 - 它确实更新了购物车并替换了值,但也出现了上述错误,认为购物车已更新
  • 你在页面顶部做session_start()吗?

标签: php arrays session session-variables


【解决方案1】:

$_SESSION 仅在 Session starts 时存在。为此,您需要在所有代码之上添加 session_start() 否则会话将无法启动,除非您在 php directives 上定义它。

【讨论】:

  • 我的页面中已经有了session_start() 的第一件事,因为其他页面中使用了其他会话(例如:登录/管理员)= 如果您是,您只能访问此特定页面已登录,因此如果您的会话根据您的登录凭据为假,您将被重定向到不同的页面。如果我 var_dump ($_SESSION) 它也会显示所有当前会话。
  • 试试这个看看输出,这样你就可以理解数据了。 if(is_array($base)){ $base=array_replace($base, $replacement); } else { var_dump($base); //or echo the value }
【解决方案2】:

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

【讨论】:

  • 谢谢,是的,我已经将我的代码更新为关联值,因为我只在之前调试差异问题时才使用 [0] 并忘记更改它。呵呵。 :) 顺便说一句,我已经尝试过 $base=array_replace($base, $replacement); 但仍然给我同样的错误。
  • @GitKidd 我已经用测试场景更新了我的答案。我遇到了与您相同的错误,并且能够摆脱它。为了不使用 $_SESSION 对代码进行了一些更改,但我相信您可以将这个概念应用到您自己的代码中。
  • 谢谢,我尝试了更多的一点一点的分析,我设法解决了这个问题。呵呵。请看答案,希望它也为将来的参考提供帮助。 :)
  • 您的回答似乎与我的编辑相同。无论如何,我很高兴你能成功!
  • +1 为所有帮助.. 你是一个真正的明星! :) 希望如果我以后遇到任何问题,我可以再次跑到你身边……呵呵! :) 欢呼showdev!你是个天才。
【解决方案3】:

这已经解决了这个问题:

基本上按照结构:(让我们将其切片)

$_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' =&gt; 1,我们需要的是来自array('quantity' =&gt; 1);array() 值,以便将其识别为数组。

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

【讨论】:

  • 这与我编辑的答案有何不同? +1 很好地解释了哪里出了问题。
  • 对不起,我去吃晚饭的时候才意识到然后在吃饭的时候意识到这种情况,然后我在本地测试后尝试回答它,并没有注意到你在我开始时编辑你的答案的答案我一回来就写答案:),我给你答案,因为你从一开始就真的在帮助我。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
相关资源
最近更新 更多