Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
}
share|improve this question
I'm not familiar with in_array_find(). Is that your own function or did you mean in_array()? – showdev Apr 26 at 21:25
Thanks. @showdev I have added the code for that function :) , i am using it in replacement of the in_array() for multi-dimensional arrays. – GitKidd Apr 26 at 21:30
Gotcha, thanks. What do you get if you echo"<pre>";print_r($_SESSION);echo"</pre>"; before the error? – showdev Apr 26 at 21:31
First i get this when there is no submission yet [cart] => Array() = for cart and when i update the value i get this ( [quantity] => 1 ))) = where the error comes out. - It does update the cart and replace the value , but theres also the said error coming with it thought the cart has been updated – GitKidd Apr 26 at 21:38
1  
Are you doing session_start() at the top of your page? – showdev Apr 26 at 21:38
show 3 more comments

3 Answers

up vote 1 down vote accepted

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

share|improve this answer
Thank you, yup i have updated my code to an associative value as i only used the[0] when i was debugging a diff issue earlier and forgot to change it. hehe. :) btw, i have tried the $base=array_replace($base, $replacement); but still gives me the same error. – GitKidd Apr 26 at 22:11
@GitKidd I have updated my answer with a test scenario. I got the same error you did and was able to get rid of it. The code is changed a bit so as not to use $_SESSION, but I'm sure you can apply the concept to your own code. – showdev Apr 26 at 22:45
Thanks, i have tried a more bit by bit analysis and i managed to solve the problem. hehe. kindly have a look at the answer , hope it provides help for future reference too. :) – GitKidd Apr 26 at 22:59
1  
Your answer seems the same as my edit. Anyway, I'm glad you got it working! – showdev Apr 26 at 23:03
+1 for all the help.. your a real star! :) hope i can run to u again if ever i get any future problems.. hehe! :) cheers showdev! ur a genius. – GitKidd Apr 26 at 23:05

This have solved this issue:

Basically as per the structure: (Lets slice it into bits)

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

then

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

finally:

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

The reason why it says that the argument $base is not an array is because:

['quantity'] in $base is not an array as it forms the 'quantity' => 1 what we need is the array() value from the array('quantity' => 1); for it to be identified as an array.

So final answer should be: $base = $_SESSION['cart'][$to_update]; as there is only one array() recorded in where the $to_update resides so the replacement argument shall replace this identified array.

share|improve this answer
How is this different from my edited answer? +1 Good explanation of what was going wrong, though. – showdev Apr 26 at 23:04
Sorry, just realised as i went to eat dinner then while eating realised this situation, then i tried answering it just after my local test and didn't notice the answer that you edited your answer as i started writing the answer as soon as i got back :) , im giving u the answer point as you've been really helping me from the start. :) – GitKidd Apr 26 at 23:09

$_SESSION only exists when a Session starts. In order to do that you need to add on top of all your code session_start() otherwise the session will not start, unless you define it on php directives.

share|improve this answer
I have already have the session_start() first thing in my page as there are other sessions that are used in other pages (eg: login / admin) = u can only access this specific page if you are logged in, so u are redirected to a different page if your session is false according to your login credentials. also if i var_dump ($_SESSION) it shows me all the current sessions. – GitKidd Apr 26 at 21:56
try this and see the output, this way you can understand the data. if(is_array($base)){ $base=array_replace($base, $replacement); } else { var_dump($base); //or echo the value } – danielcsgomes Apr 26 at 22:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.