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
CodePudding user response:
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;
}
CodePudding user response:
$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"
}
CodePudding user response:
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',
)