Updating an multi dimensional array in php - php

Hello I am trying to update an value in an multi dimensional array which was not working can any one tell me what is the issue in the below code.
<?php
$array_m= array();
array_push($array_m,array('md5'=>'a','count'=>1));
array_push($array_m,array('md5'=>'b','count'=>1));
foreach ($array_m as $key=>$val)
{
if($val['md5']=='a') {
$val['count'] =5;
break;
}
}
print_r($array_m);

foreach ($array_m as $key=>$val)
This just loops through the values, you can't update them. You need to use a reference, so you can update the array.
foreach ($array_m as $key=>&$val)
Note the &, that will make it a reference.

You have the $key so you can reference the array using that:
if($val['md5']=='a') {
$array_m[$key]['count'] = 5;
break;
}

You should Chnage your Code like following,
<?php
$array_m= array();
$array_m[] = array('md5'=>'a','count'=>1);
$array_m[] = array('md5'=>'b','count'=>1);
foreach ($array_m as $key=>$val)
{
if($val['md5']=='a') {
$val['count'] =5;
break;
}
}
print_r($array_m);

You can set the value of 5 like this:
$array_m[$key]['count'] = 5;

Related

PHP Serialized Data output inside foreach loop

I've been stuck on this for like 2 days, and I know it's much more simpler than I think it is..
I have a foreach loop that goes like this.
foreach($appointments as $appointment){
$list = $appointment['techs']
}
The $appointment['techs']; comes out of the database like this.
a:2:{i:0;s:1:"1";i:1;s:2:"12";}
My question is, how to I get loop through appointments and then show the users that are assigned to each appointment...
The desired output should look like this,
{ resource : 1, event : 1},{ resource : 12, event : 1}
I've literally tried everything! Any help would be greatly appreciated.
$array = [];
$string = 'a:2:{i:0;s:1:"1";i:1;s:2:"12";}';
$arr = (unserialize($string));
foreach ($arr as $item){
array_push($array, json_encode(['resource'=> $item, 'event'=>1]));
}
$i = 0;
$numItems = count($array);
foreach ($array as $item) {
if (++$i === $numItems) {
echo $item;
}
else{
echo $item.',';
}
}
// Output: {"resource":"1","event":1},{"resource":"12","event":1}
The PHP command unserialize it's what you are looking for.
foreach($appointments as $appointment){
$list = unserialize($appointment['techs']);
}

Outputting odd and even number in array

Using below array, i'm trying to use foreach loop to iterate through each item. Then i need to apply if condition to check if the given number is even and odd. I also need to create two arrays one for even and one for odd and push each number in their respective category.
So i have done this so far:
These are the two arrays i created to push through the values to.
}
$numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
$array_odd = [];
$array_even = [];
foreach ($numbers as $value)
{
if (value %2 == 0)
{
$array_even = $value;
echo $array_even;
}
else
{
$array_odd = $value;
echo $array_odd;
}
I'd like to know if i'm using the correct solution or are there major errors im committing?
It will surely work like charms.
$numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
$array_odd = [];
$array_even = [];
foreach ($numbers as $value)
{
if ($value %2 == 0)
{
$array_even[] = $value;
}
else
{
$array_odd[] = $value;
}
}
echo "<pre>";
print_r($array_odd);
echo "<pre>";
print_r($array_even);

add key dynamically to a php array

I have got following array in php:
theArray('id':'123','akey':'a';
'id':'234','akey':'b';
'id':'567','akey':'c';)
I would like to dynamically add another key in a loop so that my array will look like:
theArray('id':'123','akey':'a', 'anotherkey':'1';
'id':'234','akey':'b'; 'anotherkey':'1';
'id':'567','akey':'c'; 'anotherkey':'1';)
The code I have written is the following:
foreach($theArray as $row)
{
$row['anotherkey'] = "1";
}
but it is not working. What am I doing wrong?
You're not actually storing your new value in $theArray, you're just assigning it to your temporary $row variable. What you want to do is this:
foreach($theArray as $key => $row) {
$theArray[$key]["anotherkey"] = "1";
}
Try with
foreach($theArray as &$row)
{
$row['anotherkey'] = "1";
}
foreach($theArray as $key => $row)
{
$theArray[$key]['anotherkey'] = "1";
}
is more robust

Copy values into another array

I have the following code:
foreach ($row as $item) {
foreach($item as $key) {
echo "<pre>";
print_r($key);
echo "</pre>";
}
}
I am trying to copy the keys ($key) into another array for further processing. How can i do this?
define some variable as array $array = array(); and just push the keys in with array_push($array, $key);
$array = array();
foreach ($row as $item) {
foreach($item as $key) {
array_push($array, $key);
}
}
$aNew = array();
foreach($row as $item) {
foreach($item as $key) {
$aNew[] = $key;
}
}
But; why would you do this? You can also just perform your commands / processing inside the second foreach().
If you want to get all keys of an array you may use
array_keys()
instead. Also, if each of your rows has the same keys in your second foreach loop, you may break both of the loops after getting all the keys from the first row.
Just use array_keys()
$a = array();
$array_of_keys = array_keys( $a );

how to read an complex array in PHP

How to read the array below. For example i would like to set the 'actual_cash' to some other value. PLease help I am new to PhP.
$Cashups[0]['actual_cash'] = 50.22;
To change the first instance of actual_cash.
$Cashups[1]['actual_cash'] = 100.22;
To chance the second instance, and so on.
To loop through this array, you can do something like:
foreach($Cashups as &$c)
{
$c['actual_cash']=500.00;
}
Which would change al the actual_cash instances to 500.00
You can use foreach to iterate array
foreach($Cashups as $key => $value)
{
$value['actual_cash']='newval';
}
if you have only two items in array you could also do this
$Cashups[0]['actual_cash']='someval';
$Cashups[1]['actual_cash']='someval';
foreach($Cashups as $item){
$item['actual_cash'] = $mynewvalue;
}
using for loop:
for($i = 0; $i < count($Cashups); $i++) {
$Cashups[$i]['actual_cash'] = $yourValue;
}
using foreach loop:
foreach($Cashups as &$c) {
$c['actual_cash'] = $yourValue;
}
Try something like this
foreach($Cashups as &$cashup){
// referencing $cashup to change it's value
$cashup = 'new value';
}

Categories