array_replace()/ array_merge()| ($ _SESSION =阵列())参数不是一个数组? [英] array_replace() / array_merge() | ( $_SESSION = array() ) argument is not an array?

查看:447
本文介绍了array_replace()/ array_merge()| ($ _SESSION =阵列())参数不是一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code为我校项目,并想到了code做什么我想它做的工作,我仍然继续收到错误约_SESSION [] $不是一个数组参数时,使用 array_replace() array_merge()功能:

会话已经在标题启动:

  //启动会话
 在session_start();

有关初始化的 $ _ SESSION ['购物车'] 作为数组:

所有项目 //父数组,如果尚未初始化...
如果(!使用isset($ _ SESSION ['购物车'])){
    $ _SESSION ['购物车'] =阵列();
}

有关将产品从下拉菜单: - (刚刚看到的会话是如何分配的:)

 如果(使用isset($ _ POST ['NEW_ITEM'])){//如果用户提交的产品
 $ NAME = $ _ POST ['产品']; //积值设置为$名//验证添加产品:
如果($名称==选择产品'){//检查是否默认 - 没有产品选择
    $ order_error ='< D​​IV CLASS =中心><标签类=错误>请选择产品< /标签>< / DIV>';
} ELSEIF(in_array_find($名称,$ _SESSION ['购物车'])==真){//检查产品已经在购物车:
    $ order_error ='< D​​IV CLASS =中心><标签类=错误>这项目已添加<!/标签>< / DIV>';
}其他{
    //把值代入会话:
    //默认数量= 1:
    $ _SESSION ['购物车'] [$名称] =阵列('量'=> 1);
       }
}

那么这里就是问题来了,当他们试图更新产品:

  //用于更新产品的数量:
如果(使用isset($ _ POST ['更新'])){
//确定要更新的产品:
$ to_update = $ _ POST ['隐藏'];
//检查产品阵列存在:
如果(in_array_find($ to_update,$ _SESSION ['购物车'])){
    //替换/更新的值:
    // ['购物车']是会话名称
    // ['$ to_update']是产品的名称
    // [0]再presesents量
    $基地= $ _SESSION ['购物车'] [$ to_update] ['量'];
    $更换= $ _SESSION ['购物车'] [$ to_update] =阵列('量'= GT; $ _ POST ['选项']);
    array_replace($基地,$更换);
    //或者使用数组合并为PHP< 5.3
    // array_merge($替换,$基地);
    }
}

请注意,无论是功能 array_replace() array_merge()更新的值,并做初步目标是,但问题是,我仍然不断获取一个参数( $基地)不是一个数组的问题。

警告:array_replace()[function.array替换]:参数#1不是......一个数组

一个更好的方式来处理这个问题将是一个有价值的帮助任何建议:)
感谢您的帮助:)

编辑: in_array_find()是我在替换 in_array()的使用功能,因为它没有按'T适用于一个多维数组发现值:具体地2深度阵列

发现它从这里 ,它为我的作品

在code因为它是:

  //为多阵列内搜索值功能:
功能in_array_find($针,干草堆$,$严格= FALSE){
的foreach($大海捞针为$项目=> $ ARR){
    如果(($ $严格项目=== $针:$项目== $针)||(is_array($项目)及和放大器; in_array_r($针,$项目,$严格))){
        返回true;
    }
}返回false;
}


array_replace 返回更换阵列。所以,你需要做的:

  $基地= array_replace($基地,$更换);

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

  $基地= $ _SESSION ['购物车'] [$ to_update] ['量'];

编辑:

这可能不完全是你要什么...

我建立了一个测试的情况,而无需使用$ _SESSION,我得到你做同样的错误。我改变了code以下,不再得到错误。

  $ sesh =阵列(
    '购物车'=>阵列(
        0 =>阵列(
            量=> 1
        )
    )
);$ to_update = 0;
$ new_quantity = 5;// $基地= $ sesh ['购物车'] [$ to_update] ['量']; < ---改变了这下面排队
$基地= $ sesh ['购物车'] [$ to_update];$更换= $ sesh ['购物车'] [$ to_update] =阵列('量'= GT; $ new_quantity);
$基地= array_replace($基地,$更换);回声< pre>中;的print_r($基地);回声< / pre>中;

PHP FIDDLE: http://phpfiddle.org/main/$c$c / MVR-SHR

I have this code for my school project and thought the code does its job on what i wanted it to do, i still keep on getting the error about $_SESSION[] is not an array argument when using the array_replace() and array_merge() functions:

Session is already initiated on the header:

 // Start Session
 session_start();

For initialising the $_SESSION['cart'] as an array:

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

For adding products from a dropdown menu: - (Just to see how the session is assigned:)

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

Then here is where the issue comes, when they try to update the product:

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

Note that both the functions array_replace() and array_merge() are updating the values and doing what the initial goal was, but the problem is that i still keep on getting that one argument($base) is not an array issue.

Warning: array_replace() [function.array-replace]: Argument #1 is not an array in ...

any suggestions for a better way to approach this issue would be a valuable help :) Thanks for your help :)

Edit: The in_array_find() is a function that i use in replacement of in_array() as it doesn't apply to finding values in a multi-dimensional array: specifically 2 depth arrays:

found it from here and it works for me

The code for it is:

// 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 returns the replaced array. So you need to do:

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

Also, I suggest using named keys consistently throughout, as opposed to mixing named and numeric:

$base = $_SESSION['cart'][$to_update]['quantity'];

EDIT:

This may not be exactly what you're going for...

I set up a test situation without using $_SESSION, and I got the same error you did. I changed the code as follows and no longer get the error.

$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

这篇关于array_replace()/ array_merge()| ($ _SESSION =阵列())参数不是一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆