Search code examples
phplumen

Looping through nested arrays in php 7.2


I am trying to access values in a nested array that I send to my API but when do something like $data['inputs']{0} it only gives me back the first letter of the key when what I want is the whole value of the value and not the key but if I try something like $data['inputs']['type'] it gives me an offset error I ament sure how to correctly access the values the way I need to

public function saveEdit(Request $request)
{
    try {
        $unsanitizedData = $request->all();
        $data = [];
        $fid = $unsanitizedData['formId'];
        $data['form_name'] = json_encode($unsanitizedData['cred']['name']);
        $data['org'] = json_encode($unsanitizedData['cred']['organization']);
        $data['updated_by'] = json_encode($unsanitizedData['updatedBy']);
        $data['user_id'] = json_encode($unsanitizedData['id']);
        $data['activated'] = json_encode($unsanitizedData['cred']['activated']);
        $data['inputs'] = json_encode($unsanitizedData['cred']['inputs']);
        $pattren = array("[","]","'",'"',"/","\\");
        $data = str_replace($pattren,'', $data);
        foreach ($unsanitizedData as $st) {
            admin_form::where('id', $fid)->update([
                'form_name' => $data['form_name'],
                'org' => $data['org'],
                'updated_by' => $data['updated_by'],
                'user_id' => $data['user_id'],
                'activated' => $data['activated']
                ]);
                foreach ($data['inputs'] as $input) {
                    admin_form_fields::where('form_id', $fid)->update([
                        'type' => $input,
                        'name' => $input
                    ]);
                }
        }


        $res['status'] = true;
        $res['message'] = 'Success';
        return response($res, 200);
    } catch (\Illuminate\Database\QueryException $ex) {
        $res['status'] = false;
        $res['message'] = $ex->getMessage();
        return response($res, 500);
    }
}

I thought if I use a foreach loop inside another foreach loop it would work because its a nested array so loop through the main one and then through the nested one but that did also not work

Data Structure when I do a data dump:

array:6 [
    "form_name"  => "Testname",
    "org"        => "TestOrg",
    "updated_by" => "test",
    "user_id"    => "29",
    "activated"  => "false",
    "inputs"     => "{type:number,name:Phone},{type:input,name:Name},{type:input,name:Address},{type:email,name:Email}"
]

Solution

  • In your case, $data['inputs'] is a JSON encoded string from which you have removed the [ and ] characters so when you try to access its first element it's the first char (since strings are kind of arrays of strings in PHP, they are really array of strings in C).

    The problem is that you call json_encode() in the first place. If it's how you sanitize input, you're doing it wrong. Since you're using an ORM, there's no real need to manually sanitize the input. Just keep your input as sent by the client and perform all your operations then use them unsanitized in the QueryBuilder