简体   繁体   中英

convert serialized data to array

I pulled serialized data from database table and i want to display it on array. a:7:{i:0;a:2:{s:8: »field_id »;i:6;s:10: »user_value »;s:4: »jack »;}i:1;a:2:{s:8: »field_id »;i:7;s:10: »user_value »;s:5: »david »;}i:2;a:2:{s:8: »field_id »;i:8;s:10: »user_value »;s:5: »45587″;}i:3;a:2:{s:8: »field_id »;i:9;s:10: »user_value »;s:3: »447″;}i:4;a:2:{s:8: »field_id »;i:10;s:10: »user_value »;s:3: »115″;}i:5;a:2:{s:8: »field_id »;i:11;s:10: »user_value »;s:6: »Agadir »;}i:6;a:2:{s:8: »field_id »;i:13;s:10: »user_value »;s:0: » »;}} Update:Thanks guys, i tried unserialize but it is not working. I fond somewhere that unserialize doesn't work with ':;," and i should use a uncode function. My question is how can i do all that in same time: uncode>>unserialize>>store in Table or array

$x=unserialize($serialized_data)

Wordpress system has very neat function for dealing with serialized data, it is called maybe_unserialize() .

The function will unserialize value only if it was serialized ,the check for serialized data is done automatically, so this will spare you some time.

<?php maybe_unserialize( $original ) ?>

Parameters

$original - string and it is required, maybe unserialized original, if is needed.

Return Values

Unserialized data can be any type.

I found what i needed here `

$json_data = [];
foreach ($subs as $sub_id => $sub) {
    $submission_data = array(); 
    $data = unserialize($sub['data']);
    $user_values = array();
    foreach ($data as $key => $value) {
        $user_values[$value['field_id']] = $value['user_value'];
    }

    foreach ( $fields as $field_id => $field ) {
        $user_value = $user_values[$field['id']];
        if ($user_value == '') { 
            continue;
        }

        $submission_data[] = array(
            "label" => $field['data']['label'],
            "value" => $user_value,
            "type" => $field['type']
        );
    }

    $json_data[] = array(
            "id" => $sub['id'],
            "items" => array_values($submission_data)
        );

    echo json_encode(array_values($json_data), JSON_PRETTY_PRINT);
}`

from this link

This following code worked in my case as, I have serialized by jquery, then assign it to an input as a string and submitting the form.Then getting data on the PHP.

parse_str($_POST, $allFormData);
print_r($allFormData['data']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM