Given the following code:
session_start();
$cart = $_SESSION['cart'];
print_r($_SESSION['cart']);
I can then see what I want to access:
Array ( [153c740f526f2fa8aac9e1ddfdce2716] => Array ( [deal_id] => 38 [variation_id] => [variation] => [quantity] => 6 [data] =>......
There is still more but that is the basics...
What I want to be able to do is get and set the quantity:
So I've tried:
$cart = $_SESSION['cart'];
for ($i = 0 ; $i < count($cart) ; $i ++)
{
echo "The session variable you want" . $_SESSION['cart'][$i]['deal_id'];
echo "<br>";
}
But there is no output, what am I doing wrong?
foreach ($_SESSION['cart'] as $k => $data) {
echo "The session variable you want" . $data['deal_id'];
$_SESSION['cart'][$k]['deal_id'] = 'new id';
}
According to the data you just printed, within 'cart', the array is associative, and not numerical.
To iterate through an associative array, use the foreach with $someArray as $key => $val expression
The cart is not indexed by sequencial indexes, you can not loop it that way, you need to use a foreach loop:
foreach($_SESSION['cart'] as $index => $value)
echo "Var = " . $value['deal_id'];
If you want to set the value, loop the values by reference
foreach($_SESSION['cart'] as $index => &$value)
{
echo "Var = " . $value['deal_id'];
$value['deal_id'] = 'newValue';
}
You will simplify things by using foreach loop
foreach ($_SESSION['cart'] as $k => $data) {
echo "The session variable you want" . $data['deal_id'];
echo "<br>";
$_SESSION['cart'][$k] = "new Value";
}
Use the foreach loop to iterate through the $_SESSION array:
foreach($cart as $k=> $value){
echo "The session variable you want" . $data['deal_id']. "<br>";
$_SESSION['cart'][$k] = "newValueGoesHere"; //setting the new value
}
you use the foreach statement
foreach($cart as $key=>$value){
echo "The session variable you want" . $value['deal_id'];
echo "<br>";
}
Related
I am having a array like so and I am looping trough it like this:
$options = array();
$options[0] = 'test1';
$options[1] = 'test2';
$options[2] = 'test3';
foreach($options as $x)
{
echo "Value=" . $x ;
echo "<br>";
}
It outputs as expected:
Value=test
Value=test2
Value=test3
Now I want to add some options to my array and loop trough them:
$options = array();
$options['first_option'] = 'test';
$options['second_option'] = get_option('second_option');
$options['third_option'] = get_option('third_option');
foreach($options as $x)
{
echo "Value=" . $x ;
echo "<br>";
}
But it does not work as I want. Because it outputs:
Value=first_option
Value=second_option
Value=third_option
So now I do not know how to access stored values using foreach from these guys?
Something like:
Value=first_option='test'
So when I use print_r($options)
Output is:
Array
(
[first_options] => test
[second_option] =>
[third_option] =>
)
1
your loop should look like this:
foreach($options as $key => $val){
echo "Val: ".$val;
echo "<br/>";
}
Your code is working just as expected and producing the desired result. You must have something else changing the values in $options. Correction: now that I see your edit, your functions are not returning any values, so options 1 and 2 are blank. Make sure that function returns something. Other than that, all of this code is good.
By the way, I recommend this:
$options = [
'first_option' => 'test',
'second_option' => get_option('second_option'),
'third_option' => get_option('third_option')
];
foreach($options as $key) {
echo "Value = {$key}<br>";
}
you can also use:
foreach($options as $key => $value) {
echo "Value - {$value} = {$key}<br>";
}
or you could at least replace array() with just []. Those are just some suggestions for neatness.
below is a php object which is retrieving some values from mysql db through a php method
$query = "SELECT imgpath from images";
$oMySQL->ExecuteSQL($query);
Now when i use this
$result=$oMySQL->ExecuteSQL($query);
it prints "Array"
How to iterate through the array
Regards Jane
A simple foreach loop.
foreach ($result as $key => $val) {
foreach ($val as $label => $item) {
echo $label . " - " . $item;
}
}
$key will hold the associative name the array element, and $val will hold the value of the element.
You mean it print array when you do echo $result?
Then:
foreach($result as $index => $value) {
echo $index . '=' $value;
}
I have an array called $topProductIdResults and it looks like the following:
Array ( [11497522] => 2 )
The keys are prodcuct ID's and the value is the number of 5 star ratings that the product has recieved.
I want it to echo out this data using a loop. However I can't work our how to echo out both the key and value. Sometimes there will be several product ID's and number pairs in this array. Please let me know where I'm going wrong. My code so far is:
foreach ($topProductIdResults as $prod) {
echo $prod[0];
echo $prod[1];
}
which just echo's 22 at the moment. I want it to echo 11497522 2
foreach ($topProductIdResults as $key => $value) {
echo $key;
echo $value;
}
Try this :
foreach ($topProductIdResults as $key=>$prod) {
echo $key;
echo $prod;
}
Ref: http://php.net/manual/en/control-structures.foreach.php
If you just have a single dimensional array with key and value Array ( [11497522] => 2 ) , then you can use this :
$array = array(11497522=>2);
$key = key($array);
$value = $array[$key];
Use this
foreach ($topProductIdResults as $key => $value)
{
echo $key;
echo $value;
}
Try this
foreach ($topProductIdResults as $prodid => $prod) {
echo $prod[0];
echo $prod[1];
}
I have an associative array. Two dimensions which I am iterating through like this
foreach ( $order_information as $sector_index => $sector_value ){
echo 'sector : ' . current($order_information) ;
echo '<br>';
foreach ( $sector_value as $line_index => $line_value ){
}
}
The current() is an attempt to get the iteration the loop is in. It seems like this should give me that. However, elsewhere on that page there is the suggestions that you just do like
$index = 0
foreach ( $array as $key => $val ) {
echo $index;
$index++;
}
I wonder if I am using current incorrectly, as echo 'sector : ' . current($order_information); just prints sector : Array
Is $index++ bad syntax? Is there a better way to do this?
Answer
As far as I know there is no build in numeric counter in a foreach loop in PHP.
So you need your own counter variable. Your example code looks quite good to do that.
$index = 0;
foreach($array as $key => $val) {
$index++;
}
By the way, $index++; is fine.
Example
Here is an example illustrating which variable stores which value.
$array = array(
"first" => 100,
"secnd" => 200,
"third" => 300,
);
$index = 0;
foreach($array as $key => $val) {
echo "index: " . $index . ", ";
echo "key: " . $key . ", ";
echo "value: " . $val . "\n";
$index++;
}
The output will be that.
index: 0, key: first, value: 100
index: 1, key: secnd, value: 200
index: 2, key: third, value: 300
Current-Function
I think you misunderstood current($array). It gives you the value pointed by an internal array pointer, which can be moved using next($array), prev($array) and end($array).
Take a look at the manual to make thinks clear.
How to echo out the values individually of this array?
Array ( [0] => 20120514 [1] => My Event 3 )
so
echo $value[0]; etc
I have this so far:
foreach (json_decode($json_data_string, true) as $item) {
$eventDate = trim($item['date']);
// positive limit
$myarray = (explode(',', $eventDate, 2));
foreach ($myarray as $value) {
echo $value;
}
This echo's out the whole string no as an array. and if i do this?
echo $value[0};
Then I only get 2 characters of it??
The print_r :
Array ( [0] => 20120430 [1] => My Event 1 )
foreach ($array as $key => $val) {
echo $val;
}
Here is a simple routine for an array of primitive elements:
for ($i = 0; $i < count($mySimpleArray); $i++)
{
echo $mySimpleArray[$i] . "\n";
}
you need the set key and value in foreach loop for that:
foreach($item AS $key -> $value) {
echo $value;
}
this should do the trick :)
The problem here is in your explode statement
//$item['date'] presumably = 20120514. Do a print of this
$eventDate = trim($item['date']);
//This explodes on , but there is no , in $eventDate
//You also have a limit of 2 set in the below explode statement
$myarray = (explode(',', $eventDate, 2));
//$myarray is currently = to '20'
foreach ($myarray as $value) {
//Now you are iterating through a string
echo $value;
}
Try changing your initial $item['date'] to be 2012,04,30 if that's what you're trying to do. Otherwise I'm not entirely sure what you're trying to print.
var_dump($value)
it solved my problem, hope yours too.