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条)

报告相同问题?

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?