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);
}
}
}
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;
}
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;
}
});
I am processing a foreach(first) which gives the full array in the data, when I make another foreach(second) and pass the first foreach into second foreach the first value of the first foreach never appears in the second foreach. Does anyone have any idea why the foreach behaves this way? is there a solution for this?
I tried it this way no luck
$userIdsPerRoom = chatRoomUsersId($_SESSION['currentRoomID']);
foreach ($userIdsPerRoom as $value) {
$list[] =$value['UserID'];
}
foreach ($list as $value) {
$userInfo = chatRoomUsersEmail($value);
foreach ($userInfo as $info) {
echo $info['userEmail'];
}
}
echo count($list); ///gives the full list
}
I noticed it with this code
$userIdsPerRoom = chatRoomUsersId($_SESSION['currentRoomID']); //return an array of intergers with five values example array(1,2,3,4,5)
foreach ($userIdsPerRoom as $value){
$userInfo = chatRoomUsersEmail($value['UserID']);
foreach ($userInfo as $info){
echo $info['userEmail']; /// I only get four values example array(b,c,d,e)
}
}
Can someone please tell me which syntax to use to build the "directory" array from a mysql recordset? Thank you!
$directory = array();
foreach ($resultSetFromDB as $row){
foreach ($row as $field => $value){
$directory[$field][] = $value;
}
}
I can print $field and $value, no problem
Try this:
$directory = array();
foreach ($resultSetFromDB as $i => $row) {
foreach ($row as $field => $value) {
$directory[$i][$field] = $value;
}
}
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.