将序列化数据从数据库转换为PHP数组

时间:2017-08-07 13:44:41

标签: php json

我已经为每个WooCommerce订单输入了一些自定义数据输入到postmeta表中。

<?php

    include 'db.inc.php';

    function upvoteImage($conn) {
        if (isset($_POST['upvoteImage'])){

            $imageId = $_POST['id'];

            $sql2 = "UPDATE image SET upvotes = upvotes + 1 WHERE id='$imageId'";
            $result2 = mysqli_query($conn, $sql2);

            header("Location: ../index.php");

        }
    }

?>

- 编辑---

我最初假设这是用JSON编码的,但是按照下面的答案理解它实际上是序列化数据。

我想将这些数据转换为PHP数组 - 如下所示 - 所以我可以使用它。

    [SwaggerResponse(HttpStatusCode.OK, "List of customers", typeof(IEnumerable<int>))]
    [SwaggerResponse(HttpStatusCode.BadRequest, Type = typeof(BadRequestErrorMessageResult))]
    [SwaggerResponse(HttpStatusCode.NotFound, Type = typeof(NotFoundResult))]
    public IHttpActionResult GetById(int id)
    {
        if (id > 0)
            return Ok(new int[] { 1, 2 });
        else if (id == 0)
            return NotFound();
        else
            return BadRequest("id must be greater than 0");
    }

任何想法都非常感激!

1 个答案:

答案 0 :(得分:1)

根据上面的评论,这些数据是序列化的而不是JSON编码的。对于其他阅读的人来说,以下效果很好。

// Pull serialized data 
$serializeddata = 'a:2:{i:6517;a:2:{i:0;a:5:{s:10:"first_name";s:5:"Roger";s:9:"last_name";s:6:"Rabbit";s:5:"email";s:19:"roger@benefacto.org";s:7:"is_lead";b:1;s:12:"is_cancelled";b:0;}i:1;a:5:{s:10:"first_name";s:7:"Jessica";s:9:"last_name";s:6:"Rabbit";s:5:"email";s:21:"Jessica@benefacto.org";s:7:"is_lead";b:0;s:12:"is_cancelled";b:0;}}i:6518;a:2:{i:0;a:5:{s:10:"first_name";s:6:"Mickey";s:9:"last_name";s:5:"Mouse";s:5:"email";s:20:"mickey@benefacto.org";s:7:"is_lead";b:0;s:12:"is_cancelled";b:0;}i:1;a:5:{s:10:"first_name";s:6:"Donald";s:9:"last_name";s:4:"Duck";s:5:"email";s:20:"donald@benefacto.org";s:7:"is_lead";b:0;s:12:"is_cancelled";b:0;}}}';

// Unserialize it into a standard array
$array = unserialize($serializeddata);

// Print Array
print_r($array);