add key dynamically to a php array - php

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

Related

Array always re-create itself php

My programm read info from textbox and show it to user, so the problems is in middle part of that operation. How to make $myArray don't re-create itself when i update page?
session_start();
if (!isset($myArray)) $myArray = array();
$myArray[] = $_POST['nameAuthor'];
foreach($myArray as $key=>$value)
{
echo "$key->$value";
}
//$_SESSION['arr'] = $myArray;
Try this:
session_start();
if(!isset($_SESSION['arr'])) $_SESSION['arr'] = array();
$_SESSION['arr'][] = $_POST['nameAuthor'];
foreach($_SESSION['arr'] as $key => $value)
{
echo "$key->$value";
}

how to split stored variables name inside foreach

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'];
}
}

Updating an multi dimensional array in 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;

PHP add key while a foreach without using '&' (reference)

Suppose I am looping an array like this:
foreach($cursor as $obj) { ... }
and the values inside are as following:
$obj['name'] => "foo"
$obj['surname'] => "test"
is there a way to add another key and value inside it, directly from the foreach, without using '&'? Something like this:
foreach($cursor as $obj) { $obj['age'] = 24; }
but without using this:
foreach($cursor as &$obj) { $obj['age'] = 24; }
Thanks in advance.
foreach($cursor as $k => $obj) {
$cursor[$k]['age'] = 24; //or whatever else you want to change it to
}

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