Retrieve keys from xml in php

  php, xml

I need to fetch all the node keys in xml. I converted to array and then done with the following code,

<?php
$array='<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<country>
    <id>18</id>
    <id_zone xlink:href="https://www.example.com/api/zones/299">299</id_zone>
    <id_currency>0</id_currency>
    <call_prefix>469</call_prefix>
    <iso_code>SE</iso_code>
    <active>1</active>
    <contains_states>0</contains_states>
    <need_identification_number>0</need_identification_number>
    <need_zip_code>1</need_zip_code>
    <zip_code_format>NNN NN</zip_code_format>
    <display_tax_label>1</display_tax_label>
    <name><language id="1" xlink:href="https://www.example.com/api/languages/1">Su├Ęde</language><language id="2" xlink:href="https://www.example.com/api/languages/2">Sweden</language></name>
</country>
</prestashop>';
$array1=json_decode(json_encode((array)simplexml_load_string($array,'SimpleXMLElement',LIBXML_NOCDATA)),1);
//print_r($array1);die();
function getUniqueObjectKeyPaths(array $array, $parentKey = "") {
    $keys = [];
    foreach ($array as $parentKey => $v) {
        if (!is_numeric($parentKey) && !is_array($v)) {
            $keys[] = $parentKey;
        }
        if (is_array($v)) {
            $nestedKeys = getUniqueObjectKeyPaths($v, $parentKey);
            foreach($nestedKeys as $index => $key) {
                if (!is_numeric($parentKey) && !is_numeric($key)) {
                    $nestedKeys[$index] = $parentKey . "->" . $key;
                }
            }
            $keys = array_merge($keys, $nestedKeys);
        }
    }
    return $keys;
}
$k=getUniqueObjectKeyPaths($array1);
print_r($k);

I got the result as follows,

Array ( [0] => country->id [1] => country->id_zone [2] => country->id_currency [3] => country->call_prefix [4] => country->iso_code [5] => country->active [6] => country->contains_states [7] => country->need_identification_number [8] => country->need_zip_code [9] => country->zip_code_format [10] => country->display_tax_label )

The expected result is,

I also need the key country->name->language->0,country->name->language->1.

Any quick help would be greatly appreciated.

Thanks,
Rekha

Source: Ask PHP

LEAVE A COMMENT