Answer the question
In order to leave comments, you need to log in
Processing json request in php?
Hi all. Probably I have already sat out the project, but I stumbled on one thing.
The form sends a string in json format
[0: {user: 1; numflow: 1}, 1:{user: 2; numflow: 2} и т.д.]
public static function SaveFlow(Request $request){
$fnumber = 0; // очередь
$i = 0;
$num = array($request->all());
foreach ($num as $u) {
if(is_array($num)){
$fnumber++;
$user = User::find($u[$i]['id']);
$user->numflow = $u[$i]['numflow'];
$user->save();
$i++;
}
}
}
$number = 0; // очередь
$num = $value;
foreach ($num as $u) {
if(is_array($u)){
foreach ($u as $key => $value2){
foreach ($value2 as $key2 => $item) {
$number++;
$user = User::find($item);
$user->numflow = $number;
$user->save();
}
}
}
}
Answer the question
In order to leave comments, you need to log in
1. You don't need to iterate, remove $i from your script
foreach ($num as $u) {
if(is_array($num)){
$user = User::find($u->id);
$user->numflow = $u->numflow;
$user->save();
}
}
User::find($u[$i]['id']); -> Here $u is already an element of $num, ie ['user'=>1, 'numFlow'=>1]. No need for $u[$i], just $u['user']
Error - local.ERROR: Undefined index: id
dd($request->all()) outputs
array:2 [▼
"_token" => "dXOYleK8Ev0b51JgnGjVrCLk9Xj8W2Lkto2gKB4w"
"data" => array:3 [▼
0 => array:2 [▼
"id" => "3"
"numflow" => "1"
]
1 => array:2 [▼
"id" => "4"
"numflow" => "2"
]
2 => array:2 [▼
"id" => "6"
"numflow" => "3"
]
]
]
Trying to get property 'id' of non-object
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question