简体   繁体   English

循环遍历 php 7.2 中的嵌套 arrays

[英]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我正在尝试访问我发送到 API 的嵌套数组中的值,但是当我执行 $data['inputs']{0} 之类的操作时,它只会让我返回键的第一个字母,而我想要的是整个值值而不是键,但是如果我尝试像 $data['inputs']['type'] 这样的东西,它会给我一个偏移错误,我知道如何以我需要的方式正确访问这些值

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我想如果我在另一个 foreach 循环中使用一个 foreach 循环,它会起作用,因为它是一个嵌套数组,所以循环遍历主数组,然后遍历嵌套数组,但这也不起作用

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

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).在您的情况下, $data['inputs']是 JSON 编码的字符串,您已从中删除[]字符,因此当您尝试访问其第一个元素时,它是第一个字符(因为字符串是arrays中的字符串 Z72FEC38ZDA228A ,它们实际上是 C 中的字符串数组)。

The problem is that you call json_encode() in the first place.问题是您首先调用json_encode() 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.由于您使用的是 ORM,因此无需手动清理输入。 Just keep your input as sent by the client and perform all your operations then use them unsanitized in the QueryBuilder只需保留客户端发送的输入并执行所有操作,然后在 QueryBuilder 中使用它们

As far as I can see, you just need to use json_decode($data['inputs']) since your array is in fact just a string:)据我所知,你只需要使用json_decode($data['inputs'])因为你的数组实际上只是一个字符串:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM