json 変数に変換して mysql テーブル フィールドに格納する必要がある配列があり、次のコードでこれを行います。
$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");
次に、このフィールドを読み取り、属性 title_it のみを表示したいので、次のように試しました。
$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;
しかし、結果はありません。また、json は奇妙な形式で保存されます。mysql フィールドは次のようになります。
{"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":"title1"},"desc_de":{"0":"beschreibung1"}," tags_de":{"0":"etikett1"}}
だから...このjsonをmysqlフィールドに挿入してから、このフィールドのパラメータのみを読み取る正しい方法は何ですか?