dorisdong0514 2014-11-06 17:04
浏览 99
已采纳

如何从包含对象的数组中读取数据

Im having a hard time to read some data that i get from joomla 2.5. First i have created a module that stores data on DB as a json. So first i read from DB linke:

$db = JFactory::getDbo();


$query = $db->getQuery(true);

$query->select($db->quoteName(array('params')));
$query->from($db->quoteName('#__modules'));
$query->where($db->quoteName('module') . ' = '. $db->quote('mod_products'));


// Reset the query using our newly populated query object.
$db->setQuery($query);

// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();

and the result that i get as an array that contain objects, and each object has json data. below is the arrray that i get from the query:

Array
(
  [0] => stdClass Object
  (
   [params] => {
     "product_name":"Sangiovese",
     "product_subtitle":"Maremma Toscana DOC",
     "product_category":"Red",
     "isvisible":"1"
      } 
  ) 
 [1] => stdClass Object 
  ( 
   [params] => {
    "product_name":"Syrah",
    "product_subtitle":"Maremma Toscana DOC",
    "product_category":"Red",
    "isvisible":"0",
      } 
  ) 
 [2] => stdClass Object 
  ( 
   [params] => {
    "product_name":"Merlot",
    "product_subtitle":"Maremma Toscana DOC",
    "product_category":"Red",
    "isvisible":"0"
      } 
  ) 
 [3] => stdClass Object 
  ( 
   [params] => {
    "product_name":"Vermentino",
    "product_subtitle":"Maremma Toscana DOC",
    "product_category":"White",
    "isvisible":"0"
      } 
  ) 
);

So what i want to do is to access the data within each param for examle:

PS: Array name is $results. , EX: i want to access product_name of each of the products that are on this array, or subtitle and so on.

so i did something like this, but its not working, i know i am not doing it right, but i hope someone can help me, and i would really appruciate it.

foreach( $results as $result )
  { 

         echo $result->prams->product_name;
 }

Error that shows when this code gets executed:

Notice: Trying to get property of non-object in

I really would need some advice on this.

Thank you!

  • 写回答

2条回答 默认 最新

  • doumei1772 2014-11-06 17:06
    关注

    Every item in your list is an object:

    [0] => stdClass Object
    [1] => stdClass Object
    

    And every object has a params property which is a string containing JSON data.

    You need to use json_decode built-in function to convert JSON string to an object or array.

    Try this approach:

    $paramsDecoded = json_decode($result->params, true);
    print $paramsDecoded['product_name'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站