PHP array value when unserialized i get the following …array(1) { [0]=> string(10) “California” } … how to display it properly
$sanitized_data_array = array();
foreach($grouped_data as $data_arrObj => $data_arr){
$grouped_itemObj = new stdClass();
foreach($data_arr as $data_arrObj => $data_item){
$key = $data_item->name;
$value = $data_item->value;
if(is_serialized($value)){
ob_start();
var_dump(unserialize($data_item->value));
$value = ob_get_clean();
}
$grouped_itemObj->$key = $value;
}
array_push($sanitized_data_array, $grouped_itemObj);
}
return $sanitized_data_array;
… output when shown in html array(1) { [0]=> string(10) “California” } … how to display it properly
output table
<table class="my-table">
<thead>
<tr>
<th>City</th>
<th>State</th>
<th>Company</th>
<th>Submitted on</th>
</tr>
</thead>
<tbody>
<?php foreach ($lead_data_arr as $lead_idObj => $data) { ?>
<tr>
<td><?php echo $data-> city; ?></td>
<td><?php echo $data-> state; ?></td>
<td><?php echo $data-> company; ?></td>
<td><?php echo $data-> created; ?></td>
</tr>
<?php } ?>
</tbody>
2
Answers
removed var_dump() and passed first index of unserialized array ... that did the trick.
I guess after unserializing the $data_item->value, you should check if the result is an array and then access its elements directly.
You can create a function that takes a serialized string as input, tries to unserialize it, and then returns the first element of the array if the unserialized data is an array else it will return the original value.
now you can use this function to extract values
Now to display in the table you can directly use the properties of the objects in $sanitized_data_array