1

I have an array which i must convert to a json variable and store it in a mysql table field and I do this by the following code:

$arr = array(
        'title_it' => $category->title->attributes()->it,
        'desc_it' => $category->desc->attributes()->it,
        'tags_it' => $category->tags->attributes()->it,

        'title_es' => $category->title->attributes()->es,
        'desc_es' => $category->desc->attributes()->es,
        'tags_es' => $category->tags->attributes()->es,

        'title_fr' => $category->title->attributes()->fr,
        'desc_fr' => $category->desc->attributes()->fr,
        'tags_fr' => $category->tags->attributes()->fr,

        'title_en' => $category->title->attributes()->en,
        'desc_en' => $category->desc->attributes()->en,
        'tags_en' => $category->tags->attributes()->en,

        'title_de' => $category->title->attributes()->de,
        'desc_de' => $category->desc->attributes()->de,
        'tags_de' => $category->tags->attributes()->de
        );
    $params = mysql_real_escape_string(json_encode($arr));

    $query = mysql_query("INSERT INTO category_tags (id, params) VALUES ($id, '$params')") or die("could not connect");

Then I want to read this field and display only the attribute title_it I tried something like:

$query = mysql_query("SELECT * FROM article_tags WHERE id = $id LIMIT 0,1") or die("could not connect");    
$row = mysql_fetch_array($query);
$jsoni = json_encode($row['params']); 

$decoded = json_decode($jsoni, true);
echo $decoded->title_it;

but no result. Also, the json is stored in a strange format. the mysql field looks like this:

{"title_it":{"0":"titolo1"},"desc_it":{"0":"descrizione1"},"tags_it":{"0":"tags1"},"title_es":{"0":"titulo1"},"desc_es":{"0":"descripci\u00f3n1"},"tags_es":{"0":"etiquetas1"},"title_fr":{"0":"titre1"},"desc_fr":{"0":"description1"},"tags_fr":{"0":"balises1"},"title_en":{"0":"title1"},"desc_en":{"0":"description1"},"tags_en":{"0":"tags1"},"title_de":{"0":"titel1"},"desc_de":{"0":"beschreibung1"},"tags_de":{"0":"etikett1"}}

So... What is the correct way to insert this json into a mysql field and then to read only a parameter of this field?

4

2 回答 2

0

问题是 SimpleXML 在您获取属性值时不会返回字符串。当你这样做时,$category->title->attributes()->it你实际上是在返回一个SimpleXMLElement对象。它看起来像这样:

object(SimpleXMLElement)#3 (1) {
  [0]=>
  string(3) "Your_Value"
}

在 JSON 中序列化时,这将转换为:{0: "Your_Value"},这就是您在解码时看到的内容。

将它们添加到数组时,需要将它们转换为字符串:

$arr = array(
    'title_it' => (string)$category->title->attributes()->it,
    'desc_it' => (string)$category->desc->attributes()->it,
    'tags_it' => (string)$category->tags->attributes()->it,
    # etc.
);

当你得到你的数据时,你不再需要json_encode它,你需要json_decode它。

$query = mysql_query("SELECT * FROM category_tags WHERE id = $id LIMIT 0,1") or die("could not connect");
$row = mysql_fetch_array($query);

$decoded = json_decode($row['params'], true);
// The ",true" makes it into an array, not an object
echo $decoded['title_it'];
于 2013-10-02T15:12:11.423 回答
-1

好吧,它看起来像你的

$category->title->attributes()->it,

类型值返回数组而不是字符串。并且我们知道 php 数组总是关联的(叹气),所以你得到

Array(0=>"titolo1")

成为对象

{"0":"titolo1"}
于 2013-10-02T15:01:04.737 回答