-2

I have the below data in my user column.

{
   "user_details":[
      [
         "First Name",
         true
      ],
      [
         "Middle Name",
         true
      ],
      [
         "Phone Number",
         true
      ],
      [
         "Email",
         false
      ],
      [
         "Address",
         true
      ],
   ],
   "user_payment_details":[
      
   ]
}

Now I want to add check if Middle Name is present in the data and if it present then I want to add Last Name after Middle Name with the 2nd array value with the value(true or false) of Middle Name. I have tried the below method, but it is not working as expected. Can anyone suggest the changes or new code?

$datas = Test::all();
foreach ($datas as $data)
{
    foreach ($data->user['user_details'] as $key => $u) {
        if ($u[0] == 'Middle Name' && $u[1]) {
            array_splice($data->user['user_details'][$key], $key+1, 0, [["Last Name", true]]);
        } else {
            array_splice($data->user['user_details'][$key], $key+1, 0, [["Last Name", false]]);
        }
        $data->save();
    }
};
Benk I
  • 185
  • 13

1 Answers1

0
$array = [];
foreach ($data->user['user_details'] as $key => $value) {
    $array[] = $value;
    if ($value[0] == 'Middle Name') {
        $array[] = ['Last Name', $value[1]];
    }
}
$data->user['user_details'] = $array;
JS TECH
  • 1,556
  • 2
  • 11
  • 27