发布请求数组返回

时间:2018-12-14 10:23:01

标签: php laravel

post方法返回此数组:

"aff":{
    "12621":"on",
    "9897":"on",
    "13022":"on",
    "3441":"on",
    "5994":"on"
}

我需要使用该数组的唯一数字。 如果我将此数组放入更新查询中,则会出现此错误:

update [table] set [arrived] = 1 where [CandidatureID] in (on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on, on))

如您所见,我需要将aff数组中的数字id放入查询中。有什么建议吗?

3 个答案:

答案 0 :(得分:0)

您需要使用数组键。为此使用array_keys

$aff = json_decode($_POST['aff'], true); // {"12621":"on","9897":"on","13022":"on","3441":"on","5994":"on"}
$keys = array_keys($aff); // ["12621" => "on","9897" => "on","13022" => "on","3441" => "on","5994" => "on"]

var_dump($keys); // ["12621", "9897", "13022", "3441", "5994"]

答案 1 :(得分:0)

使用PHP array_keys方法。

http://php.net/manual/en/function.array-keys.php

whereIn('CandidatureID', array_keys($onArray))

答案 2 :(得分:0)

您可以遍历$aff并获取其key

@foreach ($aff as $key => $data)
    <li>{{ $key }}: {{ $data }}</li>
@endforeach