我有以下数组
$users = Array ( [0] => Array ( [id] => 16 [name] => Dr. Arti Luthra [email] => artiluthra@dsingh.com [gender] => female [mobile] => 123456789 [status] => 0 [addresses] => Array ( [0] => Array ( [id] => 16 [clinic_name] => Luthra Medical Center [address] => A-65/2,Meera Bhagh [contact_no] => 2342345234 [formatted_address] => A-65/2,Meera Bhagh Rohini,West Delhi,Delhi [timings] => Array ( [0] => Array ( [start] => 09:00 AM [end] => 02:00 PM ) [1] => Array ( [start] => 05:00 PM [end] => 08:00 PM ) ) ) [1] => Array ( [id] => 17 [clinic_name] => Luthra Medical Center [address] => A-65/2,Chanakyapuri [contact_no] => 123456789 [formatted_address] => A-65/2,Chanakyapuri Chanakyapuri,South Delhi,Delhi [timings] => Array ( [0] => Array ( [start] => 09:00 AM [end] => 02:00 PM ) ) ) ) ) [1] => Array ( [id] => 39 [name] => Sudhir Seth [email] => sudhirseth@gmail.com [gender] => male [mobile] => 65565656565 [status] => 0 [addresses] => Array ( [0] => Array ( [id] => 54 [clinic_name] => Dr. Sudhir Seth's Orthopoint [address] => D-595,Chittranjan Park,Landmark: Besides Deshbandhu College,Delhi [contact_no] => [formatted_address] => D-595,Delhi Greater Kailash,Delhi [timings] => Array ( [0] => Array ( [start] => 10:30 AM [end] => 01:00 PM ) [1] => Array ( [start] => 06:00 PM [end] => 09:00 PM ) ) ) [1] => Array ( [id] => 55 [clinic_name] => Fortis C-Doc [address] => B-16,Chirag Enclave,Nehru Place. Landmark: Opp. to Nehru Place,Delhi [contact_no] => [formatted_address] => B-16,Delhi Lajpat Nagar,Delhi [timings] => Array ( [0] => Array ( [start] => 09:00 AM [end] => 11:00 AM ) ) ) ) ) )
我想使用点符号来获取所选键
getKeys($用户,阵列( ‘名称’,‘电子邮件’,‘addresses.address’,‘addresses.clinic_name’,‘addresses.timings.start’))
Array ( [0] => Array ( [name] => Dr. Arti Luthra [email] => artiluthra@dsingh.com [addresses] => Array ( [0] => Array ( [clinic_name] => Luthra Medical Center [address] => A-65/2,Meera Bhagh [timings] => Array ( [0] => Array ( [start] => 09:00 AM ) [1] => Array ( [start] => 05:00 PM ) ) ) [1] => Array ( [clinic_name] => Luthra Medical Center [address] => A-65/2,Chanakyapuri [timings] => Array ( [0] => Array ( [start] => 09:00 AM ) ) ) ) ) [1] => Array ( [name] => Sudhir Seth [email] => sudhirseth@gmail.com [addresses] => Array ( [0] => Array ( [clinic_name] => Dr. Sudhir Seth's Orthopoint [address] => D-595,Delhi [timings] => Array ( [0] => Array ( [start] => 10:30 AM ) [1] => Array ( [start] => 06:00 PM ) ) ) [1] => Array ( [clinic_name] => Fortis C-Doc [address] => B-16,Delhi [timings] => Array ( [0] => Array ( [start] => 09:00 AM ) ) ) ) ) )
似乎这个问题非常接近xPath for xml结构.
原文链接:https://www.f2er.com/php/445234.html还有什么有趣的json为json例如:
PHP中的JsonPath – https://github.com/Skyscanner/JsonPath-PHP
Myqsl中的JsonPath – https://dev.mysql.com/doc/refman/5.7/en/json-path-syntax.html
但是,不幸的是,对于PHP阵列,这项技术还没有实现.
编写自己的库(PHPPath)是个好点!
特别针对你的一个不同的案例:
/** * Search values by PHPPath * * @param array $aData * @param string|array $PHPPath * @param array &$aReturn * @return array */ function getKeys($aData,$PHPPath = [],&$aReturn = []) { //Support arrays of $PHPPath if (is_array($PHPPath)) { foreach ($PHPPath as $path) { getKeys($aData,$path,$aReturn); } return $aReturn; } //Get next sought-for key $aParts = explode('.',$PHPPath); $sNeedle = array_shift($aParts); $sRemain = implode('.',$aParts); foreach ($aData as $k => $v) { //skip numeric keys //@todo need to thinking about //needs to add * (wildcard) into PHPPath for that purpose if (is_numeric($k) && $PHPPath) { getKeys($v,$PHPPath,$aReturn[$k]); continue; } //Is it key that we want if ($k !== $sNeedle) { continue; } //Checking needs deeper search if (is_array($v) && $sRemain) { getKeys($v,$sRemain,$aReturn[$k]); continue; } //Need to save fully-qualified found value $aReturn[$k] = $v; break; } return $aReturn; }
使用示例:
var_dump( getKeys($users,array( 'name','email','addresses.address','addresses.clinic_name','addresses.timings.start' )) );