Home > Back-end >  Empty key converts into array in multidimentional array in php?
Empty key converts into array in multidimentional array in php?

Time:10-14

I am uploading one xml and trying to convert that data into php array and have to store it in db. My problem is when i get empty key inside loop it automatically converts into array. but i want it as a string only. As i am getting array so its difficult me to store it in db.Please help me with the solution.

Current Output :

array(19) {
  ["EMP_NAME"]=>
  string(12) "ABC"
  ["EMP_ADDRESS"]=>
  string(1) "MUMBAI"
  ["DEPARTMENT"]=>
  string(1) "IT"
  ["LOCATION"]=>
  array(0) {
  }
}

Expected Output :

array(19) {
  ["EMP_NAME"]=>
  string(12) "ABC"
  ["EMP_ADDRESS"]=>
  string(1) "MUMBAI"
  ["DEPARTMENT"]=>
  string(1) "IT"
  ["LOCATION"]=>
  string(1) ""
}

This is my php code to get data from xml and looping through array.

 $xml = file_get_contents('uploads/data.xml');
    $xml = simplexml_load_string($xml);
    $xml_array = json_decode(json_encode((array) $xml), 1);
    $data = ($xml_array);
    foreach($data as  $val){
       //var_dump($val);
    }

CodePudding user response:

I tried to fix it after getting.

<?php
    function fixContent(&$val, $key = null) {
        if (is_array($val)) {
            if (!$val) $val = '';
            else fix($val);
        }
    }
    
    function fix(&$arr) {
        array_walk($arr, 'fixContent');
        array_walk_recursive($arr, 'fixContent');
    }

    $xml = "<?xml version='1.0'?> 
<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body></body>
</document>";
    $xml = simplexml_load_string($xml);
    $xml_array = json_decode(json_encode((array) $xml), 1);
    fix($xml_array);
    $data = $xml_array;
    var_dump($data);
?>

Output:

array(4) {
  ["title"]=>
  string(11) "Forty What?"
  ["from"]=>
  string(3) "Joe"
  ["to"]=>
  string(4) "Jane"
  ["body"]=>
  string(0) ""
}

Demo: https://paiza.io/projects/qQC5pfvhGz_FniCIK6S_9g

CodePudding user response:

Generic conversions are often not the best solution. They allow the source to control the output - especially if you use debug features like serializing a SimpleXMLElement instance.

Read the data from XML and add it to a result. This puts your code in control of the output. You can change the keys, validate and convert values, add defaults, ...

$xml = <<<'XML'
<EMP>
  <EMP_NAME>ABC</EMP_NAME>
  <EMP_ADDRESS>MUMBAI</EMP_ADDRESS>
  <DEPARTMENT>IT</DEPARTMENT>
  <LOCATION></LOCATION>
</EMP>
XML;

$employee = new SimpleXMLElement($xml);

$result = [
  'EMP_NAME' => (string)$employee->EMP_NAME,
  'EMP_ADDRESS' => (string)$employee->EMP_ADDRESS,
  'DEPARTMENT' => (string)$employee->DEPARTMENT,
  'LOCATION' => (string)$employee->LOCATION 
];

var_dump($result);

Output:

array(4) {
  ["EMP_NAME"]=>
  string(3) "ABC"
  ["EMP_ADDRESS"]=>
  string(6) "MUMBAI"
  ["DEPARTMENT"]=>
  string(2) "IT"
  ["LOCATION"]=>
  string(0) ""
}

Or with DOM:

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

$result = [
  'EMP_NAME' => $xpath->evaluate('string(/EMP/EMP_NAME)'),
  'EMP_ADDRESS' => $xpath->evaluate('string(/EMP/EMP_ADDRESS)'),
  'DEPARTMENT' => $xpath->evaluate('string(/EMP/DEPARTMENT)'),
  'LOCATION' => $xpath->evaluate('string(/EMP/LOCATION)')
];

var_dump($result);
  • Related