K
K
Konstantin Timosheno2019-04-12 13:36:40
PHP
Konstantin Timosheno, 2019-04-12 13:36:40

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} и т.д.]

I accept in the controller (I know what to do in the model, I want to figure it out, and then put it into the model)
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++;
                }
        }


    }

I get the error
local.ERROR: Undefined offset: 0
I tried to put json_decode(), so the error appears json_decode() expects parameter 1 to be string, array given
Please poke your nose like a kitten, what am I doing wrong?
Thank you in advance for your response.
UPD
Solved the problem. Since there was a multidimensional array, it was necessary to make nested foreach loops
$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

4 answer(s)
P
php_raper, 2019-04-12
@kastembay

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();
                }
        }

This is how it should work, your $request->all() should return an object, check with
dd($request->all())
Json is an object, not an array, to work with an object you need to use the $objname->key operator
where key is the key

I
Ivan Bragin, 2019-04-12
@aywan

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']

K
Konstantin Timosheno, 2019-04-12
@kastembay

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"
    ]
  ]
]

I removed the iteration, but the error is still present Trying to get property 'id' of non-object
. Something I don’t understand at all (((

V
vism, 2019-04-12
@vism

array($request->all());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question