$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';
}
Related
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;
}
});
its easy for sure..
i have code like this:
$indeks = 0;
foreach ($list as $k => $v)
{
$data['fname'] = $customer->firstname;
$data['lname'] = $customer->lastname;
$data['code'] = $code['code'];
$tablica[$indeks] = $data;
$indeks++;
and i want to read only 'code' value for each array.
i try:
foreach($tablica as $k => $v){
foreach ($v as $key => $value ) {
echo $value
}
}
but i get all arrays values.
when i try
foreach($tablica as $k => $v){
foreach ($v['code'] as $key => $value ) {
echo $value
}
}
i have nothing...
thx for help
You can use array_column function to get all values of column, for example:
foreach (array_column($tablica, 'code') as $value) {
echo $value;
}
I think a For loop should help
for($i=0;$i<count($tablica);$i++){
echo $tablica[$i]['code'];
}
or get all Codes into an Array
$code = array();
for($i=0;$i<count($tablica);$i++){
$code[$i] = $tablica[$i]['code'];
}
You don't need nested loops.
foreach ($tablica as $value) {
echo $value['code'];
}
DEMO
How can i know the variables ?
foreach($tree as $key => $value){
if (in_array($value['name'], $arr_folders)) {
${'id_'.$value['name']} = $value['id'];
}
//how to know variables?
$id_???
Currently I know the $value['name'] ie. it may be one,two, three, etc. but how to use them
echo $id_one;
I wanted to know here to split them in an array. So i can use
print_r($vars); which would result $id_one, $id_two, etc..
Something like this?
<?php
$array = [];
foreach($tree as $key => $value){
if (in_array($value['name'], $arr_folders)) {
$array[] = $value['id'];
}
}
print_r($array);
You can find variables by code:
foreach (get_defined_vars() as $var_name => $var_value) {
if(strpos($var_name, 'id_') ===0 ){
//it's your variable
}
}
But store variable in local scope look wrong.
May be better store to an other array:
$arIds = array();
foreach($tree as $key => $value){
if (in_array($value['name'], $arr_folders)) {
$arIds['id_'.$value['name']] = $value['id'];
}
}
I'm passing an array inside a GET(url) call like this:
&item[][element]=value
Then I do my stuff in PHP:
$item = $_GET['item'];
foreach ($item as $aValue) {
foreach ($aValue as $key => $value) {
echo '$key $value';
The problem I'm facing is that I need to have(echo) a third 'value':
echo '$key $value $thirdValue';
Do I have to change my the URL I'm passing or the foreach? And how do I do that? I Googled but I can't make heads nor tails out of it.
$item = $_GET['item'];
$item_temp=array_values($item);
foreach ($item as $aValue) {
foreach ($aValue as $key => $value) {
echo '$key $value'.$item_temp[2];
}
}
<?php
$item = $_GET['item'];
$r=array();
foreach($item as $rt){
array_push($r,array(key($rt)=> $rt));
}
foreach($r as $rt){
foreach($rt as $rt2){
$k = key($rt2);
echo $k.'__'.$rt2[$k] ;
echo "<br>";
}
}
?>
it's Work .
I have two array.
1st array is $newarray = ('489289', '536516', '332833', '536516')
2nd array is
$rockin = array(
'489289' => array('536516','value1'),
'332833' => array('536516'),
);
I want to delete some value of $newarray.
Suppose we are looping from $newarray
Initially 489289 is assigned value.
I want to check whether the value associated to 489289 from $rockin array (i.e. value1 or 536516) also exist in $newarray.
If there is exist 'value1' or '536516' in $newarray then, delete 489289 from array!
So in above case 489289 would be deleted (from $newarray)
AS 536516 is associated value of 489289 in $rockin array AND 536516 also exist in $newarray
Till now I have tried this code
foreach ($newarray as $group_id) {
foreach ($rockin as $myfrcikingcl) {
foreach ($myfrickingcl as $myfrickingleader) {
if($group_id==$myfrickingleader)
{
unset($newarray[$group_id]);
}
}
}
}
This is what I understood you want to do:
$newarray = array('489289', '536516', '332833', '536516');
$rockin = array(
'489289' => array('536516','332833'),
'332833' => array('536516'),
);
foreach ($rockin as $array) {
foreach ($array as $value) {
if (in_array($value, $newarray)) {
$key = array_search($array, $rockin);
$newarray = array_diff($newarray, array($key));
}
}
}
foreach ($newarray as $k => $v) {
if(is_array($rockin[$v])){
foreach ($rockin[$v] as $key => $value) {
if(in_array($value, $newarray)){
unset($newarray[$k]);
}
}
}
}
You're using $group_id as a key, but it's a value. You have to unset by key, like this:
foreach ($i = 0; $i < count($newarray); $i++) {
foreach ($rockin as $myfrcikingcl) {
foreach ($myfrickingcl as $myfrickingleader) {
if ($newarray[$i] == $myfrickingleader) {
unset($newarray[$i]);
}
}
}
}