我有以下数组
$users = 数组
(
[0] => 数组
(
[id] => 16
[名称] => Arti Luthra 博士
[电子邮件] => artiluthra@dsingh.com
[性别] => 女性
[手机] => 123456789
[状态] => 0
[地址] => 数组
(
[0] => 数组
(
[id] => 16
[clinic_name] => Luthra 医疗中心
[地址] => A-65/2, Meera Bhagh
[contact_no] => 2342345234
[formatted_address] => A-65/2,Meera Bhagh Rohini,西德里,德里
[时间] => 数组
(
[0] => 数组
(
[开始] => 09:00 AM
[结束] => 02:00 PM
)
[1] => 数组
(
[开始] => 05:00 PM
[结束] => 08:00 PM
)
)
)
[1] => 数组
(
[id] => 17
[clinic_name] => Luthra 医疗中心
[地址] => A-65/2, Chanakyapuri
[contact_no] => 123456789
[formatted_address] => A-65/2, Chanakyapuri Chanakyapuri, 南德里, 德里
[时间] => 数组
(
[0] => 数组
(
[开始] => 09:00 AM
[结束] => 02:00 PM
)
)
)
)
)
[1] => 数组
(
[id] => 39
[名称] => Sudhir Seth
[电子邮件] => sudhirseth@gmail.com
[性别] => 男性
[手机] => 65565656565
[状态] => 0
[地址] => 数组
(
[0] => 数组
(
[id] => 54
[clinic_name] => Sudhir Seth 博士的正交点
[地址] => D-595, Chittranjan Park, Landmark: 除了 Deshbandhu 学院,德里
[contact_no] =>
[formatted_address] => D-595, Chittranjan Park, Landmark: 除了 Deshbandhu 学院,德里大凯拉什,南德里,德里
[时间] => 数组
(
[0] => 数组
(
[开始] => 上午 10:30
[结束] => 01:00 PM
)
[1] => 数组
(
[开始] => 06:00 PM
[结束] => 09:00 PM
)
)
)
[1] => 数组
(
[id] => 55
[clinic_name] => Fortis C-Doc
[地址] => B-16, Chirag Enclave, Nehru Place。地标:Opp。到尼赫鲁广场,德里
[contact_no] =>
[formatted_address] => B-16,奇拉格飞地,尼赫鲁广场。地标:Opp。到尼赫鲁广场,德里 Lajpat Nagar,南德里,德里
[时间] => 数组
(
[0] => 数组
(
[开始] => 09:00 AM
[结束] => 上午 11:00
)
)
)
)
)
)
我想使用点符号来获取选定的键,例如
getKeys($users,array('name','email','addresses.address','addresses.clinic_name','addresses.timings.start'))
使用上面的 getKeys() 方法,输出应该是:
大批
(
[0] => 数组
(
[名称] => Arti Luthra 博士
[电子邮件] => artiluthra@dsingh.com
[地址] => 数组
(
[0] => 数组
(
[clinic_name] => Luthra 医疗中心
[地址] => A-65/2, Meera Bhagh
[时间] => 数组
(
[0] => 数组
(
[开始] => 09:00 AM
)
[1] => 数组
(
[开始] => 05:00 PM
)
)
)
[1] => 数组
(
[clinic_name] => Luthra 医疗中心
[地址] => A-65/2, Chanakyapuri
[时间] => 数组
(
[0] => 数组
(
[开始] => 09:00 AM
)
)
)
)
)
[1] => 数组
(
[名称] => Sudhir Seth
[电子邮件] => sudhirseth@gmail.com
[地址] => 数组
(
[0] => 数组
(
[clinic_name] => Sudhir Seth 博士的正交点
[地址] => D-595, Chittranjan Park, Landmark: 除了 Deshbandhu 学院,德里
[时间] => 数组
(
[0] => 数组
(
[开始] => 上午 10:30
)
[1] => 数组
(
[开始] => 06:00 PM
)
)
)
[1] => 数组
(
[clinic_name] => Fortis C-Doc
[地址] => B-16, Chirag Enclave, Nehru Place。地标:Opp。到尼赫鲁广场,德里
[时间] => 数组
(
[0] => 数组
(
[开始] => 09:00 AM
)
)
)
)
)
)
最佳答案
似乎这个问题非常类似于 xml 结构的 xPath。
还有有趣的是,例如 json 有 jsonPath:
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'
))
);
关于php - 使用 DOT Notations 键数组从多维数组中获取选定的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32221650/