I have following array
$users = Array
(
    [0] => Array
        (
            [id] => 16
            [name] => Dr. Arti Luthra
            [email] => [email protected]
            [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] => [email protected]
            [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, Chittranjan Park, Landmark: Besides Deshbandhu College, Delhi Greater Kailash, South Delhi, 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, Chirag Enclave, Nehru Place. Landmark: Opp. to Nehru Place, Delhi Lajpat Nagar, South Delhi, Delhi
                            [timings] => Array
                                (
                                    [0] => Array
                                        (
                                            [start] => 09:00 AM
                                            [end] => 11:00 AM
                                        )
                                )
                        )
                )
        )
)
I want to get selected keys using dot notation like
getKeys($users,array('name','email','addresses.address','addresses.clinic_name','addresses.timings.start'))
Using above getKeys() method the output should be:
Array
(
    [0] => Array
        (
            [name] => Dr. Arti Luthra
            [email] => [email protected]
            [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] => [email protected]
            [addresses] => Array
                (
                    [0] => Array
                        (
                            [clinic_name] => Dr. Sudhir Seth's Orthopoint
                            [address] => D-595, Chittranjan Park, Landmark: Besides Deshbandhu College, 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, Chirag Enclave, Nehru Place. Landmark: Opp. to Nehru Place, Delhi
                            [timings] => Array
                                (
                                    [0] => Array
                                        (
                                            [start] => 09:00 AM
                                        )
                                )
                        )
                )
        )
)
                Seems that question very approximate to xPath for xml structures.
Also what interesting that there is jsonPath for json for example:
JsonPath in PHP - https://github.com/Skyscanner/JsonPath-PHP
JsonPath in Myqsl - https://dev.mysql.com/doc/refman/5.7/en/json-path-syntax.html
But, unfortunately, for php array this technology have not implemented yet.
It is good point for writing your own library (phpPath)!
Regarding specifically your a little different case:
/**
 * 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;
}
Example of using:
var_dump(
    getKeys($users, array(
        'name', 'email', 'addresses.address', 
        'addresses.clinic_name', 'addresses.timings.start'
    ))
);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With