I've to update data for array which has 4 foreach loops,
foreach ($dta['hotels']['hotels'] as $key => &$value) {
foreach ($value['rooms'] as $key1 => $value1) {
foreach ($value1['rates'] as $key2 => $value2) {
foreach ($value2['shiftRates'] as $key3 => &$value3) {
$value3['net'] = 0.000072*$value3['net'];
$value3['sellingRate'] = 0.000072*$value3['sellingRate'];
var_dump($value3['sellingRate']);
}
}
}
$value['currency'] = 'USD';
}
I want to update data of very deep 4th foreach loop, which isn't updating data, where as first loop data update was possible.
i've tried to put "&" but in first loop it worked and in 4th loop it's not working.
Any possible solutions ?
You have all keys, you can use these to modify your values :
$dta['hotels']['hotels'][$key]['rooms'][$key1]['rates'][$key2]['shiftRates'][$key3]['sellingRate'] = 0.000072 * $value3['sellingRate'];
As long as there are no other net or sellingRate keys somewhere else in the array that you don't want to modify, you can do this more simply with array_walk_recursive.
array_walk_recursive($dta, function(&$value, $key) {
if ($key === 'net' || $key === 'sellingRate') {
$value *= 0.000072;
}
});
Related
I want to remove or hide duplicate values in loop , It's not array.
You can see in picture 1/3, & 2/2 is repeating so I want only once using loop. It's not return any array it's simple data so we can't use array_unique
$i=1;
$result = array();
foreach ($boxes as $key => $value) {
foreach ($result as $k => $val) {
echo $i."/".count($value).'</br>';
}
$i++;
}
Expected Output
REG-Pre-Cut Short 1/3
REG-Pre-Cut Long -
PREM-Pre-Cut Short -
PREM-Pre-Cut Short 2/2
PREM-Pre-Cut Long -
Try something like this.
First you create an array in the expected format.
Then use array_unique to remove duplicates.
Then you print out the array.
$i=1;
$result = array();
$outputResult = array();
foreach ($boxes as $key => $value) {
foreach ($result as $k => $val) {
$outputResult[] = $i."/".count($value).'</br>';
}
$i++;
}
$outputResult = array_unique($outputResult);
foreach ($outputResult as $result) {
echo $result;
}
Language : PHP,
Framework : Laravel,
I have a collection of array.
I want to create a new array for each key and push all the variables to the array.
I have done the following code which looks ugly.
Is there some better way to do this.
I have created an new array for key using foreach loop and then passed the value to the array again using foreach loop.
$resultLabels = [];
foreach ($results as $result) {
foreach($result as $key => $value){
array_push($resultLabels,'ward_no ' .$value);
}
}
foreach ($results as $result){
foreach($result as $key => $value){
if($key != 'ward_no'){
array_push($arrays[$key],$value);
}
}
}
You don't need another nested loop. Have a look here:
$resultLabels = [];
foreach ($results as $result){
foreach($result as $key => $value){
array_push($resultLabels,'ward_no ' .$value);
if($key != 'ward_no'){
array_push($arrays[$key],$value);
}
}
}
I have this array that has two $rows:
I'd like the second array's display to be based on the first array but I don't think I have it set up properly:
$rows = &$vars['rows'];
foreach ($rows[0] as $key => $value) {
if (strpos($key, 'views') === 0 && empty($value)) {
$rows[1][$key] = '';
unset($vars['header'][$key]);
}
}
This is the output from the code, you can see the table doesn't seem aligned properly:
You need to loop over the whole array and then loop over the inner data as well. Simply you need two foreach loops.
$rows = $vars;
foreach ($rows as $occ => $outer ) {
foreach ($outer as $key => $value) {
if (strpos($key, 'views') === 0 && $value =='') {
unset($vars[$occ][$key]);
}
}
}
I got a site that executes the following code
$keywords = ($_SESSION[$_POST['ts']]);
print_r($keywords);
foreach ($keywords as $keyword) {
foreach ($keyword['whitelist'] as $entry) {
foreach ($_POST as $key => $value) {
if ($key == $entry['encoded_url']) {
$entry['ignore'] = $value;
$decodedURL = $this->base64->url_decode($entry['encoded_url']);
if ($value == 'noignore') {
echo "found!<br />";
$this->blacklist_model->remove($decodedURL);
$html = $this->analyse->getHTML($decodedURL);
$entry['html'] = $html[0];
$entry['loading_time'] = $html[1];
}
if($value == 'alwaysignore') {
$this->blacklist_model->add($decodedURL);
}
}
}
}
}
print_r($keywords);
The output looks like this:
http://pastebin.com/B3PtrqjB
So, as you see, there are several "found!"s in the output, so the if clause actually gets executed a few times and I expected the second array to contain new data like 'html', but, as you see, nothing changes. Is there anything to attend when changing values in multidimensional foreach() loops?
foreach creates a copy of the array and loops through that. Modifying values doesn't work.
You can get around this, though, by using references.
foreach ($keywords as &$keyword) {
foreach ($keyword['whitelist'] as &$entry) {
foreach ($_POST as $key => &$value) {
...
}
}
}
With that you can modify $value and it WILL affect the original array.
You suppose to change the Original array.
$entry is just an instance / unconnected node.
you need to change $keywords, and not its on the fly created nodes.
$array = array('lastname', 'email', 'phone');
How do I run foreach for this array and write updated value to its place?
Like:
foreach ($array as $item) {
// do something
// replace old value of this item with a new one
}
Example:
foreach ($array as $item) {
if (item == 'lastname')
$item = 'firstname';
// replace current $item's value with a 'firstname'
}
Array should become:
$array = array('firstname', 'email', 'phone');
An alternative is to loop by reference:
foreach ($array as &$value) {
$value = strtoupper($value);
}
unset($value);// <=== majorly important to NEVER forget this line after looping by reference...
In your example, a foreach loop would seem unnecessary if you know exactly what key maps to lastname, but if you need to you can still loop through the values. There are two very common ways: set the value according to the key:
foreach ($array as $key => $value) {
if ($value == 'lastname') {
$array[$key] = 'firstname';
}
}
Or (PHP 5 only) use references:
foreach ($array as &$item) {
if ($item == 'lastname') {
$item = 'firstname';
}
}
// Clean up the reference variable
unset($item);
You could do it like this:
foreach ($array as $key => $item) {
$array[$key] = "updated item";
}
foreach ($array as $i => $value) {
if ($value == 'lastname')
$array[$i] = 'firstname';
}