-1
A
回答
0
你有一個json格式的數組,你需要首先使用json_decode
進行解碼。之後循環數組來檢查你想要的id。
因此,代碼應該是這樣的:
$json = '[{"ID":1},{"ID":2}]';
$id = 1;
$data = json_decode($json, true);
foreach($data as $item){
if($item['id'] == $id) {
echo 'it exists';
}
}
+0
json_decode90期望參數1是字符串,數組給定 –
0
迭代數組使用for循環,並使用值作爲參數去json_decode
。
$id = 1;
$arr = array('{"ID":1}', '{"ID":2}');
foreach($arr as $val) {
if (in_array($id, json_decode($val, TRUE))) {
echo "id present";
}
}
2
您可以嘗試Laravel的Collection::contains方法,例如:
$collection = collect(json_decode($jsonString, true));
if ($collection->contains(1) {
// Exists...
}
此外,您還可以使用鍵/值對這樣的:
if ($collection->contains('ID', 1) {
//...
}
另外,如果你想獲得該項目從集合然後你可以嘗試where
是這樣的:
$id = $collection->where('ID', 1)->first(); // ['ID' => 1]
0
試試這個,如果值是存在的,它會給陣列
的關鍵$jsondata = '[{"ID":1},{"ID":2}]';
$array = json_decode($jsondata,true);
$key = array_search(1, array_column($array, 'ID'));
0
只是檢查如果字符串是JSON數組中,很少計算。
我認爲這是更有效的方法。檢查結果here。
<?php
$id = 1;
$array = ['{"ID":1}', '{"ID":2}'];
echo in_array(json_encode(["ID" => $id]), $array) ? 'Yes' : 'No';
相關問題
- 1. 檢查數組是否存在於json中Javascript/jquery
- 2. PHP檢查數組鍵是否存在
- 3. 檢查整數是否存在於變量幾何序列中
- 4. 如何檢查數組中是否存在變量?
- 5. 檢查數組中是否存在多次變量?
- 6. 檢查數組是否存在於php mongodb中?
- 7. 檢查數組中是否存在多於一個相同的變量
- 8. 檢查變量是否爲數組
- 9. 檢查變量是否存在於bash中的for循環中
- 10. 檢查mysql數組是否存在於數組中
- 11. Php - 檢查文件中是否存在變量值
- 12. 檢查數組中是否存在textbox.text
- 13. 檢查數組中是否存在值
- 14. 如何檢查JSON數組中是否存在元素?
- 15. PHP - 檢查是否存在變量在其他文件
- 16. WHEN有可能檢查變量是否屬於數組?
- 17. 檢查數組中的所有值是否存在於表中
- 18. 檢查是否存在定義變量 - 警告錯誤php
- 19. 如何檢查目標變量是否在數組中?
- 20. 檢查變量是否公開php
- 21. 在檢查值之前檢查變量是否存在
- 22. 檢查表中顯示的數組中是否存在變量內容?
- 23. 檢查數組中是否存在多個變量中的一個
- 24. Ansible:檢查是否變量等於串
- 25. Powershell:檢查變量是否小於15.0.0.152
- 26. 使用PHP檢查數據庫是否存在於postgreSQL中
- 27. 如何檢查是否變量是在PHP字母或數字?
- 28. 檢查php數組中是否存在值 - 不工作?
- 29. 如何檢查數組中的對象是否存在? [PHP]
- 30. PHP - 如何檢查數組中是否存在值?
檢查'json_decode($字符串,TRUE);'http://php.net/manual/en/function.json-decode.php – jitendrapurohit
json_decode需要字符串,在這裏我有一個數組 –
遍歷數組使用循環,並使用該值作爲參數json_decode – jitendrapurohit