The data column of user table is as follows.
data | {"user": [["id", true], ["name", true], ["email", true], ["address", true], ["dob", true]], "prime_user": [["id", true], ["name", true], ["email", true], ["address", true], ["dob", true]], "premium_user": [["id", true], ["name", true], ["email", true], ["address", true], ["dob", true]]}
Now for this data, I want to add another value ["first_name", true]
next to the id, to each array(user, prime_user, premium_user) of these data. i.e. I want to update the above value as below
{
"user":[
[
"id",
true
],
[
"first_name",
true
],
[
"name",
true
],
[
"email",
true
],
[
"address",
true
],
[
"dob",
true
]
],
"prime_user":[
[
"id",
true
],
[
"first_name",
true
],
[
"name",
true
],
[
"email",
true
],
[
"address",
true
],
[
"dob",
true
]
],
"premium_user":[
[
"id",
true
],
[
"first_name",
true
],
[
"name",
true
],
[
"email",
true
],
[
"address",
true
],
[
"dob",
true
]
]
}
I have tried something like this, but it is not working. Can anyone help me out? Thanks.
$data = $user->data;
$inserted = array("first_name", true);
foreach ($data as $d) {
array_splice($d, 1, 0, $inserted );
}
$user->data = $data;
$user->save();
CodePudding user response:
array_splice uses the reference but you are using $d which is a local variable for the loop and it doesn't affect the $data variable.
try using array_splice($data[$key] , ... )
instead , add $key
in loop like this : foreach($data as $key => $d)
CodePudding user response:
Change your loop like this and you will get your result
foreach($data as $key => $value)
{
$data[$key] = array_merge(array_slice($value, 0, 1),[$inserted ],array_slice($value, 1));
}
may it helps....