I have this array
$totalAgeChecks[] = (
[0] => Array
(
[AgeCheck] => 0
)
[1] => Array
(
[AgeCheck] => 1
)
)
I want to insert another value with each element so that the final array becomes like this
$totalAgeChecks[] = (
[0] => Array
(
[AgeCheck] => 0
[AgeCheckValue] => No
)
[1] => Array
(
[AgeCheck] => 1
[AgeCheckValue] => Yes
)
)
What function shall I use/ How to achieve that?
i want to do it with foreach loop like following. But what function shall I use so the the correct data is inserted in the correct position?
foreach ($totalAgeChecks as $row)
{
if ($totalAgeChecks[$row['AgeCheck']] == 1)
{
Insert---> $totalAgeChecks[$row['AgeCheckValue']] = "Yes";
}
elseif($totalAgeChecks[$row['AgeCheck']] == 0)
{
Insert ---> $totalAgeChecks[$row['AgeCheckValue']] = "No";
}
}
I have tried
foreach ($totalAgeChecks as $key => $row)
{
if ($totalAgeChecks[$key]['AgeCheck'] == 1)
{
$totalAgeChecks[$key]['AgeCheckValue'] = "Yes";
}
elseif($totalAgeChecks[$key]['AgeCheck'] == 0)
{
$totalAgeChecks[$key]['AgeCheckValue'] = "No";
}
}
Also
foreach ($totalAgeChecks as $i => $value) {
switch ($value) {
case 0:
$value['Name'] = 'No';
break;
case 1:
$value['Name'] = 'yes';
break;
}
}
2
Answers
This is one approach – you can make a new array and add to it as you go along.
outputs:
Working demo: https://3v4l.org/qm6Wk
If you wanted to assign that back to
$totalAgeChecks
again afterwards you could of course easily do so.In an associative array inserting new data work a little different: