skip to Main Content

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


  1. This is one approach – you can make a new array and add to it as you go along.

    $totalAgeChecks[] = [
            0 => [
                    "AgeCheck" => 0
                 ],
            1 => [
                   "AgeCheck" => 1
                 ],
           ];
        
    $newArr = [];
    
    foreach ($totalAgeChecks[0] as $key => $arr)
    {
        $text = "";
        $newArr[] = [
            "AgeCheck" => $arr["AgeCheck"],
            "AgeCheckValue" => ($arr["AgeCheck"] == 1 ? "Yes" : "No")
        ];
    }
    
    var_dump($newArr);
    

    outputs:

    array(2) {
      [0]=>
      array(2) {
        ["AgeCheck"]=>
        int(0)
        ["AgeCheckValue"]=>
        string(2) "No"
      }
      [1]=>
      array(2) {
        ["AgeCheck"]=>
        int(1)
        ["AgeCheckValue"]=>
        string(3) "Yes"
      }
    }
    

    Working demo: https://3v4l.org/qm6Wk

    If you wanted to assign that back to $totalAgeChecks again afterwards you could of course easily do so.

    Login or Signup to reply.
  2. In an associative array inserting new data work a little different:

    $totalAgeChecks =array(
      ["AgeCheck" => 0],
      ["AgeCheck" => 1]
    );
    $x=0;
    foreach($totalAgeChecks as $row){
       if($row['AgeCheck'] == 1)
       {
            $totalAgeChecks[$x] += array("AgeCheckValue" => "Yes");
        }
      elseif($row['AgeCheck'] == 0)
       {
             $totalAgeChecks[$x] += array("AgeCheckValue" => "No");
       }
       $x++;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search