Below is array stored in session. It works finely.
$_SESSION['cart'][$sum_1]=array('car_id'=>$car_id,'location'=>$location,'dropoff'=>$d_location,'date_value'=>$date_value,'date_value_2'=>$date_value_2,'total_days'=>$total_days,'addon'=>$addon);
?>
And I call them out like this,
foreach($_SESSION['cart'] as $total=>$id)
{
echo $total;
echo $id['addon'];
}
Now I want to add another array inside the above array to include date_from and date_to for
addon items ($id['addon']).
How can I do that and then call them out according to addon_id?
This is the part that stores addon items id in the array for now:
'addon'=>$addon
EDITED
$_SESSION['cart'][$sum_1]=array('car_id'=>$car_id,'location'=>$location,'dropoff'=>$d_location,'date_value'=>$date_value,'date_value_2'=>$date_value_2,'total_days'=>$total_days,'addon'=>$addon,array('d_from'=>$date_from,'d_to'=>$date_to))
This will give you the wanted output
foreach($_SESSION['cart'] as $total=>$id)
{
if ($id['addon'] == $wanted_addon_to_add_dates) {
$array['date_from'] = $date_from;
$array['date_to'] = $date_to;
$id[(whatever your want here)] = $array;
}
}
And just a suggestion to give more sense to your code.
It will gives you an array like
array ('var'=>value, ..., array ('addon'=>addon_id, 'date_from'=>date_from, 'date_to'=>date_to))
foreach($_SESSION['cart'] as $total=>$id)
{
if ($id['addon'] == $wanted_addon_to_add_dates) {
$array['date_from'] = $date_from;
$array['date_to'] = $date_to;
$array['addon'] = $id['addon'];
$id['addon'] = $array;
}
}
foreach($_SESSION['cart'] as $total=>$id)
{
echo $total;
foreach( $id['addon'] as $key=>$value)
{
echo "$key - $value";
}
}
I assumed the dates were in $addon already. If not you can just add a variable to the array you already have, like
$_SESSION['cart'][$sum_1]=array('car_id'=>$car_id,'location'=>$location,'dropoff'=>$d_location,'date_value'=>$date_value,'date_value_2'=>$date_value_2,'total_days'=>$total_days,'addon'=>$addon,'dates'=> array('d_from'=>$date_from,'d_to'=>$date_to))
or
$dates = array('d_from'=>$date_from,'d_to'=>$date_to);
$_SESSION['cart'][$sum_1]=array('car_id'=>$car_id,'location'=>$location,'dropoff'=>$d_location,'date_value'=>$date_value,'date_value_2'=>$date_value_2,'total_days'=>$total_days,'addon'=>$addon,'dates'=> $dates)
In that case you have to replace 'addon' with 'dates' in the first code example.
Related
My Script :
<?php
$values='Product1,54,3,888888l,Product2,54,3,888888l,';
$exp_string=explode(",",$values);
$f=0;
foreach($exp_string as $exp_strings)
{
echo "".$f." - ".$exp_string[$f]." ";
if ($f%3==0)
{
print "<br><hr><br>";
}
$f++;
}
?>
With this code i want show data inside loop, the idea it´s show all information in groups the elements in each group it´s 4 elements and must show as this :
Results :
Group 1 :
Product1,54€,3,green
Group 2:
Product2,56€,12,red
The problem it´s i don´t know why, don´t show as i want, and for example show separate some elements and not in group, thank´s , regards
It looks like you are trying to combine elements of a for loop and a foreach loop.
Here is an example of each, pulled from the php manual:
For Loop
for($index = 0, $index < size($array), $index++ {
//Run Code
//retrieve elements from $array with $array[$index]
}
Foreach
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
It's hard for me to understand what your input looks like. If you post an example of $exp_strings, I could be of better assistance. But from the sound of your question, if $exp_strings is multidimensional if you need to loop through groups of items, you could try nesting one loop inside another loop like so:
$groups_of_data = array(array(...), array(...), array(...));
for($i = 0, $i < size($groups_of_data), $i++) {
$group = $i;
for($j = 0, $j < size($groups_of_data[$i]), $j++) {
// print out data related to group $i
}
}
This is really all guesswork on my part though as I can't see what your input string/array is. Can you post that? Maybe I can be of more help.
here is code i worked out. it was kind of since i did't know what object $exp_string is. if it's a string you should tokenize it, but i think it's array from database. there was another problem, with your code where it tries to output $exp_string[$f] it should be $exp_strings what changes in the loop.
my code
$exp_string=array("Product"=>54,"price"=>3,"color"=>"green");
$f=0;
foreach($exp_string as $key => $exp_strings)
{
if($f%3==0)
{
print "<br><hr><br>";
echo "Product ".$exp_strings."<br> ";
}
else if($f%3==1)
{
echo "Price ".$exp_strings."<br> ";
}
else if($f%3==2)
{
echo "Color ".$exp_strings."<br> ";
}
$f++;
}
hope it's any help, maybe not what you wanted.
$values='Product1,1,2,3,Product2,1,2,3,Product3,1,2,3';
$products = (function($values) {
$exp_string = explode(',', $values);
$products = [];
for ($i=0; $i+3<count($exp_string); $i+=4) {
$product = [
'title' => $exp_string[$i],
'price' => $exp_string[$i+1],
'color' => $exp_string[$i+2],
'num' => $exp_string[$i+3],
];
array_push($products, $product);
}
return $products;
})($values);
/* var_dump($products); */
foreach($products as $product) {
echo "{$product['title']},{$product['price']},{$product['color']},{$product['num']}<br>";
}
I want to know what is the value of the 2nd index of my array.
I have something like this:
$a[][1]= 10;
$a[][0]= 20;
$a[][1]= 12;
$a[][0]= 25;
I want to get only the values from $a wich 2nd index is 1.
I need to do a comparison and if the answer is correct, get the value
From this example, elements will be:
$a[0][1]
$a[2][1]
I know about array_key_exists but i dont know how to use it with Multidimensional arrays.
Any help?
Thanks.
Solution I was looking for.
$a = array();
$a[][1] = b1;
$a[][0] = b2;
$a[][0] = c1;
$a[][1] = c2;
foreach ($a AS $key => $aVal )
if (array_key_exists(1,$aVal))
echo $a[$key][1] . "<br>";
Have you tried isset($a[$specified_index])?
Here is some code:
foreach ( $a AS $key => $aVal ) {
if ( array_key_exists(1,$aVal) ) {
var_dump($key,$aVal);
}
}
foreach($a as $key => $value)
{
if(isset($value[1]))
{
if($value[1] == 1)
{
echo "value is 1";
}
else
{
echo "value is not 1 but is ".$value[1];
}
}
}
thx to #Havelock
I have two arrays $pq and $rs. please see them below:
$pq = array ('page-0'=>array ('line-0'=>array('item-0'=>array('name'=>"item-00",'value'=>"123"),
'item-1'=>array('name'=>"item-01",'value'=>"456")
),
'line-1'=>array('item-0'=>array('name'=>"item-10",'value'=>"789"),
'item-1'=>array('name'=>"item-11",'value'=>"012")
)),
'page-1'=>array ('line-0'=>array('item-0'=>array('name'=>"item-100",'value'=>"345"),
'item-1'=>array('name'=>"item-101",'value'=>"678")
),
'line-1'=>array('item-0'=>array('name'=>"item-110",'value'=>"901"),
'item-1'=>array('name'=>"item-111",'value'=>"234")
),
'line-2'=>array('item-0'=>array('name'=>"item-210",'value'=>"567"),
'item-1'=>array('name'=>"item-211",'value'=>"890")
))
);
$rs = array ('1'=>array('name'=>'item-00', 'value'=>"abc"),
'2'=>array('name'=>'item-01', 'value'=>"def"),
'3'=>array('name'=>'item-10', 'value'=>"ghi"),
'4'=>array('name'=>'item-11', 'value'=>"jkl"),
'5'=>array('name'=>'item-100', 'value'=>"mno"),
'6'=>array('name'=>'item-101', 'value'=>"pqr"),
'7'=>array('name'=>'item-110', 'value'=>"stu"),
'8'=>array('name'=>'item-111', 'value'=>"vwx")
);
What I am trying to do is to replace the values in $pq for items with the values from $rs.
for example item-01 in $pa to be replaced with abc from $rs.
What I tried is this:
foreach($rs as &$rs1) {
echo "first count :".$firstCount."<br>";
foreach($pq as $pages) {
foreach($pages as $lines) {
foreach($lines as &$item) {
if ($item['name'] == $rs1['name']) { echo "matching </p>";
$item['value']=$rs1['value'];
echo '<pre>';
print_r($item);
echo '</pre>';
echo "<hr>";
}
}
}
}
}
When I print the values of $item from $pq, it prints the values from $rs, but when I print the whole array $pq, the values seem to be unchanged.
Can anyone please help me find out what I am missing ?
You're correctly looping through the items in each line by reference, but you're not doing it for the lines or pages themselves. So you're updating the value of an item in a copy of the line, instead of the line itself. It should be:
foreach($rs as $rs1) {
echo "first count :".$firstCount."<br>";
foreach($pq as &$pages) {
foreach($pages as &$lines) {
foreach($lines as &$item) {
if ($item['name'] == $rs1['name']) { echo "matching </p>";
$item['value']=$rs1['value'];
echo '<pre>';
print_r($item);
echo '</pre>';
echo "<hr>";
}
}
}
}
}
Note that the & in front of &$lines and &$pages. Note also that $rs1 doesn't need to be passed by reference, since you aren't changing anything in that array.
You’ve assigned $item by reference but haven’t done the same for $pages and $lines. There will be no effect on the actual values of $pq unless you assign $pages by reference; similarly, the actual values of $pages will remain unchanged unless you assign $lines by reference. Therefore, in order to achieve what you want, change foreach($pq as $pages) to foreach($pq as &$pages) and foreach($pages as $lines) to foreach($pages as &$lines).
You can build a search array first so that you can match items easier:
$search = array_reduce($rs, function(&$prev, $current) {
$prev[$current['name']] = $current;
return $prev;
}, []);
This creates another array with the item name as the key. Then, you iterate over each item in $pq and modify the leaves where necessary:
foreach ($pq as &$page_data) {
foreach ($page_data as &$line_data) {
foreach ($line_data as &$item_data) {
if (isset($search[$item_data['name']])) {
$item_data = $search[$item_data['name']];
}
}
}
}
Make sure to use references at each level of iteration.
I'm relatively new to PHP and I hope you can help me solve my problem. I am selecting out data from a database into an array for timekeeping. Ultimately, I would like to calculate the total number of hours spent on a project for a given customer.
Here is the code to populate a multi-dimensional array:
...
foreach ($record as $data) {
$mArray = array();
$name = $data['user'];
$customer = $data['customer'];
$project = $data['project'];
$hours = $data['hours'];
$mArray[$name][$customer][$project] += $hours;
}
...
I would now like to iterate over $mArray to generate an xml file like this:
...
foreach ($mArray as $username) {
foreach ($mArray[$username] as $customerName) {
foreach ($mArray[$username][$customerName] as $project ) {
echo '<'.$username.'><'.$customerName.'><'.$project.'><hours>'.
$mArray[$username][$customerName][$project].'</hours></'.$project.'>
</'.$customerName.'></'.$username.'>';
}
}
}
This nested foreach doesn't work. Can someone give me a couple of tips on how to traverse this structure? Thank you for reading!
UPDATE:
Based on the comments I've received so far (and THANK YOU TO ALL), I have:
foreach ($mArray as $userKey => $username) {
foreach ($mArray[$userKey] as $customerKey => $customerName) {
foreach ($mArray[$userKey][$customerKey] as $projectKey => $projectName) {
echo '<name>'.$userKey.'</name>';
echo "\n";
echo '<customerName>'.$customerKey.'</customerName>';
echo "\n";
echo '<projectName>'.$projectKey.'</projectName>';
echo "\n";
echo '<hours>'.$mArray[$userKey][$customerKey][$projectKey].'</hours>';
echo "\n";
}
}
}
This is now only providing a single iteration (one row of data).
Foreach syntax is foreach($array as $value). You're trying to use those values as array keys, but they're not values - they're the child arrays. What you want is either:
foreach($mArray as $username) {
foreach($username as ...)
or
foreach($mArray as $key => $user) {
foreach($mArray[$key] as ...)
i've a 2-dimensional array and i want to push values to it with a while loop like;
$arr[0][1] = 1. value
$arr[0][2] = 2. value
i ve tried
while($zRow = mysql_fetch_array($zQuery))
{
$props[]['name'] =$zRow['name'];
$props[]['photo'] =$zRow['thumbnail'];
}
this loop pushes name to $props[0][name] and thumbnail to $props[1][photo]
i also tried
$j = 0;
while($zRow = mysql_fetch_array($zQuery))
{
$props[$j]['name'] =$zRow['name'];
$props[$j]['photo'] =$zRow['thumbnail'];
$j+=1;
}
that works but with this i when i use foreach loop later, it makes trouble like "Illegal offset type"
and here is my foreach loop
foreach($props as $no)
{
echo $props[$no]['name'];
}
now my questions;
1) are there any other way than while loop with $j variable like array_push for 2-dimensional arrays
2)how can i use foreach loop for 2-dimensional arrays
You could change the first loop to the following:
while($zRow = mysql_fetch_array($zQuery))
{
$row = array();
$row['name'] = $zRow['name'];
$row['photo'] = $zRow['thumbnail'];
$props[] = $row;
}
Your method also works, but you need that extra variable.
In your second loop, what you actually need to be doing is:
foreach($props as $index => $array)
{
echo $props[$index]['name'];
// OR
echo $array['name'];
}
Pushing anything onto an array with $myArray[] = 'foo' will increment the array's counter.
For multidimensional array, you need to populate the "inner" array, then push it to the "outer" (in your case $props) array.
while($zRow = mysql_fetch_array($zQuery)) {
$data = array('name' => $zRow['name'], 'photo' => $zRow['thumbnail']);
$props[] = $data;
}
To iterate over multidimensional arrays whose depth is known:
foreach ($props as $prop) {
foreach ($prop as $key => $value) {
echo "{$key} => {$value}" . PHP_EOL;
}
}
If the depth of the nesting is not known, you may have to use a recursive function to gather the data.