我有以下陣列(稱爲$example_array
下):匹配遞歸/多維數組對另一個陣列
array(3) {
["id"]=> string(4) "123"
["name"]=>
array(1) {
["first"]=> string(3) "pete"
["last"]=> string(3) "foo"
}
["address"]=>
array(1) {
["shipping"]=>
array(1) {
["zip"]=> string(4) "1234"
["country"]=> string(4) "USA"
}
}
}
我想,我可以運行對數組這樣,尋找一個匹配功能。下面是我想能夠執行搜索:
// These should return true:
search($example_array, array('id' => '123'));
search($example_array, array('name' => array('first' => 'pete'));
search($example_array, array('address' => array('shipping' => array('country' => 'USA')));
// These don't have to return true:
search($example_array, array('first' => 'pete'));
search($example_array, array('country' => 'USA'));
有沒有我可以使用PHP內部函數或我將不得不編寫自己的東西?
是應該在陣列匹配或分開這三個陣列?匹配的邏輯是什麼?如果項目匹配,如果其中的一個數組應該被搜索或全部匹配? – Leri
另外,每個都是可以針對示例數組執行搜索的示例。 –
看起來像數據庫作業 –