本文介绍了unserialize()值,并把它放在json_encode后foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用函数 serialize()插入数据库值(数组) $ row-> units > [$ row-> units] ,如何回应 unserialize() > json_encode 与 $ row-> name ? (在jQuery中返回ajax调用)



数据库中的列:



  $ query = $ this-> db-> query(SELECT * FROM arraha WHERE name LIKE'%$ search%'ORDER BY name asc); 

$ data = array();
foreach($ query-> result()as $ row)
{
$ data [] = array('name'=> $ row-& => unserialize($ row-> units)); // Line 22
}
return json_encode($ data)

错误



严重性:注意事项:



消息:unserialize()[function.unserialize]:
281字节偏移277处的错误



文件名:model.php



行号:22



解决方案

您对字符编码有一些问题:

  s:15:Coffee

15表示以字节为单位的长度。因此,您必须将从DB提取的数据的编码转换为serialize()使用的编码



您可以使用json_encode而不是serialize:

  $ arr = array('Coffee','Satellite',/*...*/); 
$ row-> units = json_encode($ arr);


I insert in database values (array) $row->units with use function serialize()=>[$row->units], how can echo they with unserialize() in json_encode with $row->name? (return send for ajax call in jQuery)

Columns in database:

$query = $this->db->query("SELECT * FROM arraha WHERE name LIKE '%$search%' ORDER BY name asc");

$data = array();
foreach ($query->result() as $row)
{
   $data[] = array('name' => $row->name, 'units' => unserialize($row->units)); // Line 22
}
return json_encode($data)

The error for code above is:

解决方案

You have some issues with character encoding:

s:15:"Coffee"

15 means length in bytes. So you have to translate encoding of data fetched from DB into encoding that was used with serialize()

You can use json_encode instead of serialize:

$arr = array('Coffee', 'Satellite', /*...*/);
$row->units = json_encode($arr);

这篇关于unserialize()值,并把它放在json_encode后foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:06