Im trying to put multiple posts into one session. Heres the line of code that i use to do it.
$_SESSION['answers'][] = array_push($_SESSION['answers'], $_POST);
As i do so, the following array comes out once i try to add the second array to the session:
Array
(
[0] => Array
(
[checkbox] => Optie 2
[category] => Dieren en natuur
[subcategory] => Wilde dieren
[vraagid] => 116
[type] => 3afbeeldingen
[media] => image
[submit] =>
)
[1] => Array
(
[checkbox] => Optie 1
[category] => Dieren en natuur
[subcategory] => Wilde dieren
[vraagid] => 117
[type] => 3afbeeldingen
[media] => image
[submit] =>
)
[2] => 2
)
I am talking about the last array,
[2] => 2.
This is automattically added. Now when i try to get another array in to the session, it gives me the same issue, it adds the correct array, with checkbox and other vars, but also makes a
[4] => 4
Is this an issue with the array_push function? Because the array that i push is correct, i already checked that.
Either add the value to your array:
$_SESSION['answers'][] = $_POST;
or use array_push
array_push($_SESSION['answers'], $_POST);
You try to do to much at once :)
Won't it work, when you just do:
array_push($_SESSION['answers'], $_POST);
or
$_SESSION['answers'][] = $_POST;
array_push() is basically the same as $array[] = .... array_push() returns the new number of elements in the array, so basically you are adding the new element to your array and then you are adding the amount of elements in the array to the array again (hence the 2).
Related
I have an array of image data like this:
[other-image] => Array
(
[img] => Array
(
[0] => 1526973657.jpg
[1] => 1526973661.jpg
[2] => 1526973665.jpg
)
[path] => Array
(
[0] => ../post-upload/1/
[1] => ../post-upload/1/
[2] => ../post-upload/1/
)
[type] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
[thumb] => Array
(
[0] => thumb_1526973661.jpg
[1] => thumb_1526973665.jpg
[2] => thumb_1526973668.jpg
)
)
Now I want to delete an image and it's all related data from sub arrays. (path, type, thumb data)
This is how I tried it in php:
$delkey = '1526973657.jpg';
if(in_array($delkey, $_SESSION['other-image']['img'])){
$imgkey = array_search($delkey, $_SESSION['other-image']['img']);
if($imgkey) unset($_SESSION['other-image']['img'][$imgkey]);
}
But problem is I can't delete related data from other arrays.
Can anybody tell me how to do this?
Thank you.
You should use !==false after array_search() because it may return first index i.e. 0 in some cases, so your condition will not executed. And regarding delete related data from other arrays, you have to unset other data related to that key.
if($imgkey!==false){
unset($_SESSION['other-image']['img'][$imgkey]);
unset($_SESSION['other-image']['path'][$imgkey]);
unset($_SESSION['other-image']['type'][$imgkey]);
unset($_SESSION['other-image']['thumb'][$imgkey]);
}
Is the related data has same key with img?
If they are same, I think you only need to add some codes to delete other data like the way was used to delete img.
if($imgkey) unset($_SESSION['other-image']['path'][$imgkey]);
if($imgkey) unset($_SESSION['other-image']['type'][$imgkey]);
if($imgkey) unset($_SESSION['other-image']['thumb'][$imgkey]);
If the keys in img sub-array are related with the same key(index) in sub-arrays(path, type and thumb, you can also unset those keys. e.g.
$delkey = '1526973657.jpg';
if(in_array($delkey, $_SESSION['other-image']['img'])){
$imgkey = array_search($delkey, $_SESSION['other-image']['img']);
if($imgkey){
unset($_SESSION['other-image']['img'][$imgkey]);
unset($_SESSION['other-image']['path'][$imgkey]);
unset($_SESSION['other-image']['type'][$imgkey]);
unset($_SESSION['other-image']['thumb'][$imgkey]);
}
}
I'm trying to access a piece of data in an array of arrays that (I believe) is in an object (this may not be the right term though).
When I do print_r on this: $order_total_modules->process() I get...
Array (
[0] => Array (
[code] => ot_subtotal
[title] => Sub-Total:
[text] => $49.99
[value] => 49.99
[sort_order] => 1
)
[1] => Array (
[code] => ot_total
[title] => Total:
[text] => $0.00
[value] => 0
[sort_order] => 12
)
)
If I run echo $order_total_modules->process()[1][3];, I should get "0", because that is the 3rd element of the 2nd array... right? Yet, I get an error.
Can anyone help with this?
Even though it is the third element counting from 0, the index is not 3 it is an associative array with the index value:
Available in PHP >=5.4.0:
echo $order_total_modules->process()[1]['value'];
Or PHP < 5.4.0:
$result = $order_total_modules->process();
echo $result[1]['value'];
You cannot access an associative array via an integer index(unless the index is an actial integer).
So in this case use :
[1]['code'] to access what woulde be [1][0] with a 'normal' array.
Try putting it in a var first:
$ar = $order_total_modules->process();
echo $ar[1]['value'];
The second level array is an assoc, which means that the key is not numeric, which means that you need to call the name of the key, hence the 'value'.
I know this won't take much time for experts here. But still please help me out
My Array output is like this
Array ( [0] => 1 [1] => 37 [2] => 1035 ) 1
Array ( [0] => 1 [1] => 37 [2] => 1035 ) mystatusmessage1
Array ( [0] => 4 [1] => 37 [2] => 2925 ) 2
Array ( [0] => 4 [1] => 37 [2] => 2925 ) mystatusmessage2
What I would like to get it is in a single string value like this so that I can insert into database.
1,37,1035,1,mystatusmessage1
4,37,2925,2,mystatusmessage2
How can I achieve that. I'm trying to do with foreach but still I'm not able to do it.
Thanks,
Kimz
use implode function to make string from array for example
if you have array like Array('a','b','c');
implode(',',array('a','b','c') )
will return a,b,c as string
here first argument is your glue by which you want to join string
Here you go.
// Original array
$array = array(0 => 1, 1 => 37, 2 => 1035);
// $_POST array
$_POST = array(1,'mystatusmessage1');
// Jump to the end of array
end($array);
// Merge the post with original array
$newArr = array_merge($array,$_POST);
// Impode
echo implode(",",$newArr);
Repeat with other array.
I'm building a 3 page submission form, and I'd quite like all of the $_POST results to be stored in a single session variable.
So page 1 starts by setting up the array and adding the first lot of post data:
$_SESSION['results'] = array();
$_SESSION['results'] = $_POST // first lot of post data
This works great and returns an array like:
Array
(
[name] => bob
[address] => 1 foobar way
[age] => 100
)
So when I get the resuts from page 2, I want to simply append them to the existing array without invoking a new array+key
array_push($_SESSION['results'], $_POST); //second lot of post data
To get something like this:
Array
(
[name] => bob
[address] => 1 foobar way
[age] => 100
[job] => rubbish php dev
[salary] => 1000
)
But instead I get:
Array
(
[name] => bob
[address] => 1 foobar way
[age] => 100
[0] => Array
(
[job] => rubbish php dev
[salary] => 1000
)
)
Even more annoying is that I'm sure I had this working properly before I tweaked the code. What am I doing wrong?
You can also use the + operator:
$combined = $_SESSION['results'] + $_POST;
array_merge() is the function you're after.
array_merge() is your answer see http://php.net/manual/en/function.array-merge.php
You have to use array_merge(), look at this: array_merge()
I have a two dimensional array and wish to always delete/unset the last array item (in this case Array[3]) in the code sample below, before I put it into a SESSION.
I am still a novice with php and have tried the following with no success.
Any help would be greatly appreciated.
if (is_array$shoppingCartContents)) {
foreach($shoppingCartContents as $k=>$v) {
if($v[1] === 999999) {
unset($shoppingCartContents[$k]);
}
}
}
$shoppingCartContents = Array
(
[0] => Array
(
[productId] => 27
[productTitle] => Saffron, Dill & Mustard Mayonnaise
[price] => 6.50
[quantity] => 3
)
[1] => Array
(
[productId] => 28
[productTitle] => Wasabi Mayonnaise
[price] => 6.50
[quantity] => 3
)
[2] => Array
(
[productId] => 29
[productTitle] => Chilli Mayo
[price] => 6.50
[quantity] => 2
)
[3] => Array
(
[productId] => 999999
[productTitle] => Postage
[price] => 8.50
[quantity] => 1
)
)
Just use array_pop()
$last_array_element = array_pop($shoppingCartContents);
// $shoppingCartContents now has last item removed
So in your code:
if (is_array($shoppingCartContents)) {
array_pop($shoppingCartContents); // you don't care about last items, so no need to keep it's value in memory
}
Your code will fail as you're using strings for keys, not numbers, so the comparison
if($v[1] === 999999)
will never match, and should be checking $v['productId'].
For your use case, rather than looping through the array, you can just pop the last item off:
array_pop($shoppingCartContents);
array_pop removes the last item from an array. It returns that last item, but since you don't want to keep the last item, we're not saving the return value.
Alternatively, if you still wanted to use unset, you could get the last key, and then unset using that.
Finally, as it looks like you've got a true list (i.e. consecutive, numerical indices), you could get away with something like unset($shoppingCartContents[count($shoppingCartContents)-1]);
All that being said, array_pop is the way to go.