Push associative array from for-loop in php

后端 未结 2 1710
自闭症患者
自闭症患者 2021-01-19 12:54

The for loop is supposed to loop 10 times. When I just print the $fname, $lname and $email I am getting the 10 data (Working fine)

相关标签:
2条回答
  • 2021-01-19 13:05

    This assignment:-

    $assocDataArray += [ "First Name" => $fname ];
    $assocDataArray += [ "Last Name" => $lname ];
    $assocDataArray += [ "Email" => $email ];
    

    Is wrong.Change it like below

    $assocDataArray[] = [ "First Name" => $fname ,"Last Name" => $lname ,"Email" => $email ];
    
    0 讨论(0)
  • 2021-01-19 13:17

    You can assign the key

    $assocDataArray = array();
    foreach ($data as $key => $value) {
        foreach ($value['emails'] as $lead) {
            $fname = $lead['first_name'];
            $lname = $lead['last_name'];
            $email = $lead['value'];
    
            //put objects in array
            $assocDataArray[ $key ]['First Name'] = $fname;
            $assocDataArray[ $key ]['Last Name'] = $lname;
            $assocDataArray[ $key ]['Email'] = $email;
        }
    }
    
    var_dump($assocDataArray);
    

    ```

    0 讨论(0)
提交回复
热议问题