0

I want to convert this hashtable into array or object. is any php method available to convert hashtable into array or object or anything else.

<hashtable>
  <entry>
    <string>actiontypedesc</string>
    <string>Deletion of IP Address [148.56.25.133] from Child Nameserver ns4.pankajwelcome.com</string
>
  </entry>
  <entry>
    <string>entityid</string>
    <string>61389936</string>
  </entry>
  <entry>
    <string>actionstatus</string>
    <string>Success</string>
  </entry>
</hashtable>
1

2 Answers 2

3

This looks a lot like XML, which is why you could use PHP's SimpleXML extension:

$hash = '<hashtable>
  <entry>
    <string>actiontypedesc</string>
    <string>Deletion of IP Address [148.56.25.133] from Child Nameserver ns4.pankajwelcome.com</string
>
  </entry>
  <entry>
    <string>entityid</string>
    <string>61389936</string>
  </entry>
  <entry>
    <string>actionstatus</string>
    <string>Success</string>
  </entry>
</hashtable>';

$asXML = new SimpleXMLElement($hash);
var_dump($asXML);

This creates an object and you can easily access its fields.

Sign up to request clarification or add additional context in comments.

Comments

0

A generic conversion will not provide the best result. XML is a data structure, a generic conversion usually results in the loss of information and functionality.

If you like to read this specific data into a simple array, you can use DOM+XPath:

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);

$array = [];
foreach ($xpath->evaluate('/hashtable/entry') as $entry) {
  $key = $xpath->evaluate('string(string[1])', $entry);
  $value = $xpath->evaluate('string(string[2])', $entry);
  $array[$key] = $value;
}

var_dump($array);

Output:

array(3) {
  ["actiontypedesc"]=>
  string(82) "Deletion of IP Address [148.56.25.133] from Child Nameserver ns4.pankajwelcome.com"
  ["entityid"]=>
  string(8) "61389936"
  ["actionstatus"]=>
  string(7) "Success"
}

/hashtable/entry returns a list of all entry nodes. string[1] would return the first string element child node (in a list), but the function string() casts it into a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.