Is there way to get value of CDATA and convert in json or array format in php?

80
October 16, 2021, at 02:50 AM

Here is what it looks like after getting value from CDATA via simplexmlelement

$data = "{customertype=New, Telephone=09832354544, CITY=Henfield, LASTNAME=C, TicketNo=123456, FIRSTNAME=Alex, Id=10001273, testfield=123456, COMPANY=Camp1}"

I tried looking into solutions in google but i am not able to find one which would convert this set of strings to array.

I want this data to convert into array something like this

["customertype"] = ["New"]
["Telephone"] = ["09832354544"]

so and so forth or something similar as how array looks like. Thanks in advance

Answer 1

Given your data string format, you may do as follows:
First of all, remove the brackets { }
Then explode the string using the , delimiter.
Now, you have array of strings containing for example customertype=New.
Next is the tricky part, the result string looks like a query, so we're gonna use parse_str() to make an associative array:

The result will be something similar to this:

array:9 [▼
  0 => array:1 [▼
    "customertype" => "New"
  ]
  1 => array:1 [▼
    "Telephone" => "09832354544"
  ]
  2 => array:1 [▼
    "CITY" => "Henfield"
  ]

And here's the code:

    $data = "{customertype=New, Telephone=09832354544, CITY=Henfield, LASTNAME=C, TicketNo=123456, FIRSTNAME=Alex, Id=10001273, testfield=123456, COMPANY=Camp1}";
    $rippedData = str_replace(["{", "}"],"", $data);
    $singleData= explode(",", $rippedData);
    $finalArray = [];
    foreach($singleData as $string){
        parse_str($string, $output);
        $finalArray[] = $output;
    }
Answer 2
$data = "{customertype=New, Telephone=09832354544, CITY=Henfield, LASTNAME=C, TicketNo=123456, FIRSTNAME=Alex, Id=10001273, testfield=123456, COMPANY=Camp1}";
$stripped = str_replace(['{','}'], '', $data);
$explode = explode(', ', $stripped);
$output = [];
foreach ($explode as $value) {
    $inner_explode = explode('=', $value);
    $output[$inner_explode[0]] = $inner_explode[1];
}
var_dump($output);

Results in

array(9) {
    ["customertype"]=>
    string(3) "New"
    ["Telephone"]=>
    string(11) "09832354544"
    ["CITY"]=>
    string(8) "Henfield"
    ["LASTNAME"]=>
    string(1) "C"
    ["TicketNo"]=>
    string(6) "123456"
    ["FIRSTNAME"]=>
    string(4) "Alex"
    ["Id"]=>
    string(8) "10001273"
    ["testfield"]=>
    string(6) "123456"
    ["COMPANY"]=>
    string(5) "Camp1"
  }
Answer 3

This should work, though there's probably a better/cleaner way to code this.

$data = "{customertype=New, Telephone=09832354544, CITY=Henfield, LASTNAME=C, TicketNo=123456, FIRSTNAME=Alex, Id=10001273, testfield=123456, COMPANY=Camp1}";
// Replace squiggles
$replace_this = array("{","}");
$replace_data = str_replace($replace_this,"",$data);
// Explode and create array
$data_array = explode(',',$replace_data);
// Loop through the data_array
foreach($data_array as $value) {
    // Explode the value on the equal sign
    $explode_again = explode('=', $value);
    // Create new array with correct indexes and values
    $CDTA[trim($explode_again[0])] = $explode_again[1];
}
echo '<pre>' . var_export($CDTA, true) . '</pre>';

Result:

array (
  'customertype' => 'New',
  'Telephone' => '09832354544',
  'CITY' => 'Henfield',
  'LASTNAME' => 'C',
  'TicketNo' => '123456',
  'FIRSTNAME' => 'Alex',
  'Id' => '10001273',
  'testfield' => '123456',
  'COMPANY' => 'Camp1',
)
READ ALSO
Get multiple data from same table with different Id [closed]

Get multiple data from same table with different Id [closed]

Want to improve this question? Add details and clarify the problem by editing this post

36
Jackson ObjectMapper not able to parse string as hashmap

Jackson ObjectMapper not able to parse string as hashmap

I want to parse a Json String to Map<String, Object> and use the same in the existing application

66
How to convert wasm back to C++ if I also have the original code

How to convert wasm back to C++ if I also have the original code

I'm not the best at explaining this stuff but here I go

66
Module not found: Error: Can&#39;t resolve &#39;fsevents&#39; in &#39;/var/www/html/node_modules/chokidar/lib&#39; [closed]

Module not found: Error: Can't resolve 'fsevents' in '/var/www/html/node_modules/chokidar/lib' [closed]

Want to improve this question? Add details and clarify the problem by editing this post

33