I have an array called $test_data, and I want to update a key ['test_duration']. However, I am unable to do this update. Consider the following array:
Array
(
[0] => Array
(
[test_id] => 1116
[test_name] => ques stats
[test_no_questions] => 50
[test_duration] => 28800
)
[1] => Array
(
[test_id] => 1112
[test_name] => Own Test 1
[test_no_questions] => 2
[test_duration] => 7200
)
)
I tried the following, but it didn't work out:
foreach ($test_data as $key => $value) {
$value[$key]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}
If I print the array after this manipulation, it's printing the same array as before. What is the problem here?
update $test_data instead of $value
foreach ($test_data as $key => $value) {
$test_data[$key]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}
You need to nest a furthermore.
foreach ($test_data as $arr)
{
foreach($arr as $k=>$v)
{
$value[$k]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}
}
Use like this,
foreach ($test_data as $key => $value) {
$test_data[$key]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}
Related
I have been trying to get this working for the past day, with no luck. Finally, I did it with a bad hack and would like how to do it right.
I need to find the key for a search on an array with the following structure
(
[2] => Array
(
[0] => Array
(
[total] => 52
[date] => 2017-02-08
[nickname] => AAAA
)
[1] => Array
(
[total] => 53
[date] => 2017-02-09
[nickname] => AAAA
)
)
[3] => Array
(
[8] => Array
(
[total] => 11
[date] => 2017-02-08
[nickname] => XXXX
)
[9] => Array
(
[total] => 14
[date] => 2017-02-09
[nickname] => XXXX
)
)
)
I need to search by date for each array inside the array, for that I am using the following code
$key = array_search($value, array_column($sales, 'date'));
Which goes inside a foreach looping the big array.
The problem I have is that $key instead of returning me the id it seems to be returning me the position. So for example for $value = '2017-02-09' it will always return me 1 instead of 1 and 9
Is it just that I don't understand how array_seach works or is there any way I can get 1 and 9 as a result of the search inside the foreach
Any tip in the right direction will be appreciated,
You can use a nested loop to append all the keys that match your date value to an array.
foreach ($your_array as $set) {
foreach ($set as $key => $item) {
if ($item['date'] == $value) $keys[] = $key;
}
}
For your example array, this would result in $keys = [1, 9].
array_search — Searches the array for a given value and returns the
first corresponding key if successful
The solution using The RecursiveIteratorIterator class :
$keys = [];
$value = '2017-02-09';
$it = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($data), \RecursiveIteratorIterator::SELF_FIRST);
foreach ($it as $k => $v) {
if (is_array($v) && isset($v['date']) && $v['date'] == $value) {
$keys[] = $k;
}
}
I have the feeling I am missing something simple. I need to change the value of a key in a multi dimensional array based on the result of a function. Here is my array $exports
Array (
[0] => Array (
[captain] => Yes
[uniform] => 3
[fname] => Sally
[lname] => Smith
[position1] => OH
[position2] =>
[position3] =>
[bio] =>
[classyear] => 2015
[hft] => 5
[hin] => 7
)
[1] => Array (
[captain] => Yes
[uniform] => 2
[fname] => Danielle
[lname] => Smith
[position1] => L
[position2] => S
[position3] => OH
[bio] =>
[classyear] => 2016
[hft] => 5
[hin] => 2
)
[2] => Array (
[captain] => No
[uniform] => 4
[fname] => Erica
[lname] => Smith
[position1] => RS
[position2] =>
[position3] =>
[bio] =>
[classyear] => 2018
[hft] => 5
[hin] => 9
)
)
This is the code I am using.
foreach($exports as $key => &$value)
{
foreach($value as $key1 => &$value1)
{
if( $key1 == "classyear") $value1=JHtml::_('helper.gradenumber', $value1, $season);
}
unset($value1);
}
unset($value);
return $exports;
This is within Joomla so the JHml line is my function. If I replace this with a string, then my array is updated correctly, but using the function, my classyear key is empty. I have tested and know the function is returning the correct value.
It seems like what you have should mostly work, but in this part:
$value1=JHtml::_('helper.gradenumber', $value1['classyear'], $season)
I think $value1['classyear'] should just be $value1,
because if( $key1 == "classyear") then $value1 will just be a number.
Also, if you are seeing all the results of the classyear function first, you may have it echoing the value instead of returning it.
You could probably use array_walk for this.
array_walk($exports, function (&$value) use ($season) {
$value['classyear'] = JHtml::_(
'helper.gradenumber', $value['classyear'], $season);
});
You referenced to your $value and $value1, and then unset them. Do not use references, instead of that, update the value like this:
foreach ($exports as $key => $value) {
foreach ($value as $key1 => $value1) {
if ($key1 == "classyear") {
$exports[$key1] = JHtml::_('helper.gradenumber', $export['classyear'], $season);
}
}
}
Can use array_key_exists() to check key exist or not. No need to use nested foreach() . Example:
$finalArray = array();
foreach($exports as $key => $value)
{
if(array_key_exists("classyear", $value)){
$value["classyear"] = JHtml::_('helper.gradenumber', $export['classyear'], $season);
}
$finalArray[] = $value;
}
I haven't tested this code, but array_walk_recursive() seems to me to be the way to handle this:
function changeKey(&$item, $key) {
if($key == 'classyear') {
$classYear = $item;
$item = JHtml::_('helper.gradenumber', $classYear, $season);
}
}
array_walk_recursive($exports, 'changeKey');
This is my first question here so i dont exactly know the normal style.
I have a problem with multiple arrays. My arrays are sorted this way:
Array
(
[count] => 2
[gebruikerData] => Array
(
[gebruiker1] => Array
(
[merken] => Array
(
[0] => merk1
[1] => merk10
[2] => merk19
)
[loginnaam] => testfasdfasd
[geslacht] => Man
[persoonlijkheidsType] => TEST
[beschrijving] => fasdfasdfasd
[gebruikerID] => 19
[leeftijd] => 21
)
[gebruiker2] => Array
(
[merken] => Array
(
[0] => merk1
[1] => merk9
[2] => merk36
)
[loginnaam] => test1233
[geslacht] => Man
[persoonlijkheidsType] => TEST
[beschrijving] => safasfd
[gebruikerID] => 20
[leeftijd] => 21
)
)
)
I need to retrieve all the information in this array. There can be as many fields gebruiker(number) as the database output, so i tried to use multiple foreach loops in eachother. My problem is that it is not possible to use the key from one foreach loop as index in another foreach loop like this:
foreach ($gebruikerData as $key => $value)
{
foreach ($key as $key2 => $value2)
{
echo $key2;
}
}
Does anyone have another idea how i could retrieve the information from the array? Or is if could use my own way with a slight change?
Try like this
foreach ($gebruikerData as $key => $value)
{
if(is_array($key))
{
foreach ($key as $key2 => $value2)
{
if(is_array($key2))
{
foreach($key2 as $key3=>$value3)
echo $key3.'-'.$value3;
}
else
echo $key2.'-'.$value2;
}
}
else
echo $key.'-'.$value;
}
Check for the $key is "array or not" each time,if it is array then it will fo to for loop orelse it will echo it directly
I have the following array. how can I get the value of 'installed' key i.e 1. which value I have to check in my application.
Array
(
[0] => Array
(
[id] => 53686899
)
[1] => Array
(
[installed] => 1
[id] => 542813519
)
[2] => Array
(
[installed] => 1
[id] => 567790764
)
[3] => Array
(
[id] => 567570764
)
)
using foreach loop how can i do this job? anybody can plz help me?
foreach ($array as $value)
{
echo $value['installed']. "<br />";
}
will output
1
1
Try this :
foreach ($array as $value){
if(array_key_exists('installed',$value)){
echo $value['installed']. "<br />";
}
}
If you are not checking for array_key_exists it will show error in first loop.
Absolutely the same way like when you iterate 1 dimensional array:
foreach ($array as $value) {
var_dump($value);
var_dump($value['installed'];
}
Loop through the array and get the 'installed' key's value:
foreach ($array as $innerArray) {
echo $innerArray['installed'];
}
Ok, I'm still struggling with my arrays... I have created a two-dimensional array and saved into a session to get results on another page: $_SESSION['myARRAY']
print_r($_SESSION['myARRAY']);
// will output:
Array ( [Car] => Array ( [0] => 1 [1] => 9 [2] => 0 )
[Truck] => Array ( [0] => 2 [1] => 10 [2] => 0 )
[Bus] => Array ( [0] => 1 [1] => 8 [2] => 2 ))
Now I need to output the data into the following format:
$xls->addRow(Array("Car",1,9,0));
$xls->addRow(Array("Truck",2,10,0));
$xls->addRow(Array("Bus",1,8,2));
I tried to do something like this:
foreach($_SESSION['myARRAY'] AS $key => $value) {
$arr[$key] = $key;
foreach($value AS $k => $v) {
$arr[$key] = $v;
}
$xls->addRow($arr[$key]);
}
but it did not really work. I think I'm close but not quite there...
$value is already an array. Now you only need to prepend the $key to it. You could use array_unshift:
foreach($_SESSION['myARRAY'] AS $key => $value) {
array_unshift($value, $key);
$xls->addRow($value);
}
Of course if you do this more than once, you should consider storing the consolidated array.
I'd probably use array_unshift as it seems like a more appropriate way to solve this problem, but you could also do it like:
foreach($_SESSION['myARRAY'] AS $key => $value) {
$xls->addRow(array_merge(array($key), $value));
}