2012-07-31 73 views
0

原來這裏是JSON,我要搜索得到的特定按鍵PHP找到所有的項目在JSON

{services: 
[ 
{ 
servicetypeid: "26", 
serviceid: "50", 
servicename: "Tax Evasion", 
description: "Tax Evasion", 
optioncode: { }, 
inputid: { }, 
price: { }, 
categoryidentifier: { } 
}, 
{ 
servicetypeid: "27", 
serviceid: "51", 
servicename: "Parking", 
description: "Parking Related Payments", 
optioncode: { }, 
inputid: { }, 
price: { }, 
categoryidentifier: { } 
}, 
{ 
servicetypeid: "27", 
serviceid: "52", 
servicename: "Markets", 
description: "Markets Related Payments", 
optioncode: { }, 
inputid: { }, 
price: { }, 
categoryidentifier: { } 
}, 
{ 
servicetypeid: "27", 
serviceid: "53", 
servicename: "PSV", 
description: "Public Service Vehicles", 
optioncode: { }, 
inputid: { }, 
price: { }, 
categoryidentifier: { } 
}, 
{ 
servicetypeid: "5", 
serviceid: "54", 
servicename: "Vehicle Bill", 
description: "Check any Bill Attached to Your Vehicle", 
optioncode: "212", 
inputid: "216", 
price: { }, 
categoryidentifier: { } 
} 
] 
} 

我一直想寫像\功能的specifiv值的所有出現 findInjson($ jsonObj,$ field,value)

返回 findInjson($ jsonObj,'servicetypeid','27');

{services:[ 
{ 
servicetypeid: "27", 
serviceid: "51", 
servicename: "Parking", 
description: "Parking Related Payments", 
optioncode: { }, 
inputid: { }, 
price: { }, 
categoryidentifier: { } 
}, 
{ 
servicetypeid: "27", 
serviceid: "52", 
servicename: "Markets", 
description: "Markets Related Payments", 
optioncode: { }, 
inputid: { }, 
price: { }, 
categoryidentifier: { } 
}, 
{ 
servicetypeid: "27", 
serviceid: "53", 
servicename: "PSV", 
description: "Public Service Vehicles", 
optioncode: { }, 
inputid: { }, 
price: { }, 
categoryidentifier: { } 
} 
] 
} 
+0

它是否會在數組內進行兩次? (我的意思是'[[{objectToSearch}]] – Lumbendil 2012-07-31 10:59:13

+0

只是糾正它...對不起, – kmarima 2012-07-31 11:04:59

回答

0

要由數組中的這些鍵搜索,你可以做到以下幾點:

解決方案PHP 5.3

function findinjson($jsonContent, $field, $value) 
{ 
    $jsonObject = json_decode($jsonContent, true); 
    // This is to search inside the first array, which only has one element 
    $searchArray = $jsonObject['services']; 

    // Array filter applies the function given as a parameter to each element of $searchArray 
    $filteredArray = array_filter($searchArray, function($element) use ($field, $value) { 
     return array_key_exists($field, $element) && $element[$field] == $value; 
    }); 

    // Output the data the same way it was given, but filtered. 
    return json_encode(array('services' => $filteredArray)); 
} 

希望這就夠了。如果您需要一個PHP 5.2解決方案,您可以通過迭代$searchArray而不是使用陣列過濾器來構建$filteredArray,並且只能將通過該條件的元素添加到$filteredArray。因此,撥打array_filter的電話將被以下代碼所取代:

$filteredArray = array(); 

foreach ($searchArray as $element) { 
    if (array_key_exists($field, $element) && $element[$field] == $value) { 
     $filteredArray[] = $element; 
    } 
} 
+0

得到一個意想不到的T_DOUBLE_ARROW'代碼'返回json_encode('services'=> array($ filteredArray)); – kmarima 2012-07-31 11:23:02

+0

修正了它。現在一切正常嗎? – Lumbendil 2012-07-31 11:45:49

+0

不知道我在做什麼錯在這裏...得到這個錯誤 不能使用stdClass類型的對象作爲'code'中的數組$ searchArray = $ jsonObject ['services']; – kmarima 2012-07-31 12:08:27