skip to Main Content

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


  1. Chosen as BEST ANSWER

    removed var_dump() and passed first index of unserialized array ... that did the trick.

    $valueArr = unserialize($data_item->value);
    $value = $valueArr[0];
    

  2. 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.

    function extractValue($serializedData) {
        if (is_serialized($serializedData)) {
            $unserialized = unserialize($serializedData);
            if (is_array($unserialized) && count($unserialized) > 0) {
                return $unserialized[0];
            }
        }
        return $serializedData;
    }
    

    now you can use this function to extract values

    $sanitized_data_array = array();
    foreach ($grouped_data as $data_arr) {
        $grouped_itemObj = new stdClass();
        foreach ($data_arr as $data_item) {
            $key = $data_item->name;
            $value = extractValue($data_item->value); // Use function to extract value
            $grouped_itemObj->$key = $value;
        }
        array_push($sanitized_data_array, $grouped_itemObj);
    }
    return $sanitized_data_array;
    

    Now to display in the table you can directly use the properties of the objects in $sanitized_data_array

    <table class="my-table">
        <thead>
            <tr>
                <th>City</th>
                <th>State</th>
                <th>Company</th>
                <th>Submitted on</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($sanitized_data_array as $data) { ?>
                <tr>
                    <td><?php echo isset($data->city) ? $data->city : ''; ?></td>
                    <td><?php echo isset($data->state) ? $data->state : ''; ?></td>
                    <td><?php echo isset($data->company) ? $data->company : ''; ?></td>
                    <td><?php echo isset($data->created) ? $data->created : ''; ?></td>
                </tr>
            <?php } ?>
        </tbody>
    </table>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search