Im trying to add additional arrays into my session variable such as...
$_SESSION[cart] .= array($_POST[name],$_POST[price],$_POST[quantity]);
All i get when i do this 3 times and var_dump is string(15) "ArrayArrayArray"
Youre using .= "." is for string concat so youre arrays are getting converted to strings you should use one of the following:
$_SESSION['cart'][] = array($_POST[name],$_POST[price],$_POST[quantity]);
$_SESSION['cart'] += array($_POST[name],$_POST[price],$_POST[quantity]);
array_push(array($_POST[name],$_POST[price],$_POST[quantity]), (array) $_SESSION['cart'];
You can use print_r and see the content of the array.
i.e., print_r($_SESSION)
Related
My session variables are
$_SESSION["listofIds"]=163,164;
$_SESSION["listofVals"]=4,3;
I want to select session values like
$_SESSION["listofIds"][0]=163;
$_SESSION["listofIds"][1]=164;
$_SESSION["listofVals"][0]=4;
$_SESSION["listofVals"][1]=3;
I tried
echo $_SESSION["listofIds"][0];
then it prints '1' i.e. first character. How can I handle this?
Also how can I get 1st element of $_SESSION["listofIds"] and 1st element of $_SESSION["listofVals"] after placing in while loop for mysql query.
Your session variables seem to be strings. Use
$IdsArr = explode(",", $_SESSION["listofIds"]);
echo $IdsArr[0];
With explode splitting the string by "," to an array.
$listofIds = array();
$listofIds = explode(",",$_SESSION["listofIds"]);
now you can access the variables using listofIds[0] or listofIds[1].
First of all you should start by defining from scratch an array while placing values in the $_SESSION array. This is a better way to write code.
Anyway as all the other answers said:
$listofIds = array();
$listofIds = explode(",",$_SESSION["listofIds"]);
will convert your string into an array and you will be able to access each value in different ways. Most commons:
using a foreach loop
getting the index of the item you want to use.
If you want to use the first item in a query you should get its value using the index (array starts from 0) so your value will be $listofids[0]. The same logic will apply to the other $_SESSION value.
Another good practice for programming: when you put an array value in a query string assign it to a variable before:
$id_to_search = $listofids[0];
and then use it into the query:
$sql = "SELECT * FROM your_table WHERE id='$id'";
This will save you a lot of headache with singe and double quotes.
Inside a loop where I'm dealing with variables related to a product and a number of units, I'm trying to add these two to an array:
$pedido = array();
So,
foreach($_POST as $post_key => $post_value){
if ($post_value=="on") {
$nombreProducto = mysql_fetch_assoc($mySQL->query("SELECT nombre from productos WHERE id_producto='$post_key'"));
$cantidad = $_POST[$post_key."Number"];
echo "<h1>".$nombreProducto['nombre']."</h1>"." Cantidad: ".$cantidad." <br><br>";
$pedido["$nombreProducto"] = $cantidad;
}
}
It's right in:
$pedido["$nombreProducto"] = $cantidad;
Where I try to perform the adding, however the output of var_dump is like:
array(1) { ["Array"]=> string(1) "3" }
Not exactly what I wanted neither the format.
Use $pedido[$nombreProducto['nombre']] = $cantidad; instead of $pedido["$nombreProducto"] = $cantidad;
Remove quotes and
$pedido[$nombreProducto['nombre']] = $cantidad;
EDITED
It seems $nombreProducto is an array so you need to indicate the key field, so i changed to use the field "nombre"
If you see your var_dump it's an Array with the key "Array" this is why you are trying to convert the array to string and it return the word "Array"
You shouldn't be putting a variable by itself in quotes. Remove the quotes.
Also, since you were just accessing $nombreProducto['nombre'] on the previous line, it's fairly obvious that that variable is an array. You cannot use an array as a key, only integers and strings are allowed. So use something that identifies it, such as its ID number.
The following makes no sense to do:
$pedido["$nombreProducto"] = $cantidad;
What are you trying to do here? I think what you want to do is this:
$pedido[$nombreProducto['nombre']] = $cantidad;
Also you may want to try to output the array like this:
print_r($pedido);
I recommend you re-write the mysql query so you don't need to loop it. That is for safety and efficiency reasons. I also recommend you use mysqli instead of mysql, because mysql is deprecated and unsafe to use. You don't check if $_POST[$post_key."Number"] is set, at least not in this code. I hope you sanitize/validate the input from $_POST before using it against the database?
I have the following code :
$results = $Q->get_posts($args);
foreach ($results as $r) {
print $r['trackArtist'];
}
This is the output :
["SOUL MINORITY"]
["INLAND KNIGHTS"]
["DUKY","LOQUACE"]
My question is, if trackArtist is an array, why can't I run the implode function like this :
$artistString = implode(" , ", $r['trackArtist']);
Thanks
UPDATE :
Yes, it is a string indeed, but from the other side it leaves as an array so I assumed it arrives as an array here also.
There must be some processing done in the back.
Any idea how I can extract the information, for example from :
["DUKY","LOQUACE"]
to get :
DUKY, LOQUACE
Thanks for your time
It's probably a JSON string. You can do this to get the desired result:
$a = json_decode($r['trackArtist']); // turns your string into an array
$artistString = implode(', ', $a); // now you can use implode
It looks like it's not actually an array; it's the string '["DUKY","LOQUACE"]' An array would be printed as Array. You can confirm this with:
var_dump($r['trackArtist']);
To me content of $r['trackArtist'] is NOT an array. Just regular string or object. Instead of print use print_r() or var_dump() to figure this out and then adjust your code to work correctly with the type of object it really is.
hi
this is pushan
I am using $_SESSION['name']='the 2d array name', to assign a dynamically created 2d array to a session variable in php.when I am accessing the session variable in the other page anfd printing the values only the values of the last row of the 2d array is getting printed . the rest is all blank.Please help me I am under tremendous pressure.
thanks
It looks like you assign only a string to the session variable. But you have to assign the array itself:
$_SESSION['name'] = array(...);
// or a reference of the array
$_SESSION['name'] = $array2;
You can use serialize() to save the array to the session as a string, then unserialize() to recover it later.
Page 1:
$_SESSION['name'] = serialize($arrayName);
Page 2:
$arrayName = unserialize($_SESSION['name']);
var_dump($arrayName);
I have a multi array that looks like this:
$_SESSION['cartItems']['quantity']
How do I print out its values? print_r won't work, unless $_SESSION doesn't support multi-dimensional arrays?
print_r($_SESSION['cartItems']); should work.
you can use var_dump($_SESSION). However, print_r should work. Make sure you are doing print_r($_SESSION), and not trying to print_r your variable that may not exist.
If you want to get the quantity of each card item in the array, you can do this:
$quantities = array_map(create_function('item', "$item['quanitity']"), $_SESSION['cardItems']);
// PHP 5.3
$quantities = array_map(function($item) {
return $item['quanitity'];
}, $_SESSION['cardItems']);