John Davidson

php - I have an unserialized data. Now how can i cut the first part of array and transform it in json format?

0 comments
Message:


I have an unserialized array. Now i want to cut the first part of it the _PHP_Incomplete_Class_Name:"Payslip_Label". For example, below is the array that i want to cut


    [{"__PHP_Incomplete_Class_Name":"Payslip_Label","label":"Position","value":"HR 
Assistant","variable":"position"},

{"__PHP_Incomplete_Class_Name":"Payslip_Label","label":"Monthly
Rate","value":"10000","variable":"monthly_rate"}]

I want output like this


    [{"label":"Position","value":"HR 
Assistant","variable":"position"},

{"label":"Monthly
Rate","value":"10000","variable":"monthly_rate"}]


My Answer:
To achieve this, you can use the `array_map` function in PHP to iterate over each element of the array and remove the `__PHP_Incomplete_Class_Name` key. Then, you can use the `json_encode` function to convert the modified array into a JSON format.

Here's an example code snippet to achieve this:

php
// Your unserialized array
$data = [
["__PHP_Incomplete_Class_Name" => "Payslip_Label", "label" => "Position", "value" => "HR Assistant", "variable" => "position"],
["__PHP_Incomplete_Class_Name" => "Payslip_Label", "label" => "Monthly Rate", "value" => "10000", "variable" => "monthly_rate"]
];

// Remove the "__PHP_Incomplete_Class_Name" key from each element
$data = array_map(function($item) {
unset($item["__PHP_Incomplete_Class_Name"]);
return $item;
}, $data);

// Convert the modified array to JSON format
$jsonData = json_encode($data);

// Output the JSON data
echo $jsonData;


This code will output the modified array in JSON format without the `__PHP_Incomplete_Class_Name` key.

Rate this post

5 of 5 based on 3887 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us