如何循环json编码的数据? [重复]

问题描述 投票:0回答:2

我有一个功能

public function get_detail($subscriberid)
{
    $bigArray = array();

    $result = mysql_query("SELECT *  from prospect_requests WHERE subscriber_id = '$subscriberid'");
    //$user_data = mysql_fetch_array($result);
    $no_rows = mysql_num_rows($result);

    while( $row = mysql_fetch_assoc( $result))
    {
        $bigArray[] = $row;
    }
    return json_encode($bigArray);
}

我通过使用 html 中的用户类对象来调用此函数,它以表单数组的形式返回数据。

我正在尝试在我的 HTML 表中填充这些值,我是否使用以下方法。

<table>
    <?php
    $ary = json_decode($user->get_detail($uid));
        echo "<tr>"         
            foreach($ary as $key=>$value) {
                echo "<td>";
                    print  $key['$value'];
                echo "</td>";
            }
        echo "</tr>";
    ?>

上面的

foreach()
不是为我打印的,但是当我打印
print_r($ary);
时,我得到了想要的数组。

请告诉我为什么值没有填充到表中。

更新 print_r($ary) 的输出

Array (
    [0] => stdClass Object (
        [id] => 133
        [customer_id] => caason
        [subscriber_id] => ELECTRO_SUBS_12800
        [extension_id] => 
        [vendor_number] => 
        [product_code] => 
        [prospect_email1] => [email protected]
        [prospect_phone1] => +575445
        [prospect_email2] =>
        [prospect_phone2] =>
        [prospect_title] => IT Engineer
        [prospect_company] => gggg
        [prospect_name] => ggdf
        [prospect_message] => MESSAGE
        [create_timestamp] => 2012-12-20 16:28:46
        [update_timestamp] => 2012-12-20 16:33:15
        [status] => 0
        [track_id] => 201660a2-4a66-11e2-abeb-00151771ff65
        [initiated_by] => SMS
        [subscriber_email] => [email protected]
        [vendor_email] => [email protected]
    )
    [1] => stdClass Object (
        [id] => 134
        [customer_id] => caason
        [subscriber_id] => ELECTRO_SUBS_12800
        [extension_id] =>
        [vendor_number] =>
        [product_code] => 12800
        [prospect_email1] => [email protected]
        [prospect_phone1] => +756767863
        [prospect_email2] => 
        [prospect_phone2] => 
        [prospect_title] => TESTING232 
        [prospect_company] => Sadhana Systems
        [prospect_name] => 7dfgfg
        [prospect_message] => MESSAGE
        [create_timestamp] => 2012-12-22 02:43:51
        [update_timestamp] => 2012-12-22 22:42:36
        [status] => 0
        [track_id] => 380024bc-4b85-11e2-abeb-00151771ff65
        [initiated_by] => SMS
        [subscriber_email] => [email protected]
        [vendor_email] => [email protected]
    )
    [2] => stdClass Object (
        [id] => 135
        [customer_id] => caason
        [subscriber_id] => ELECTRO_SUBS_12800
        [extension_id] => 
        [vendor_number] => 
        [product_code] => 12800 
        [prospect_email1] => [email protected] 
        [prospect_phone1] => +25454 
        [prospect_email2] => 
        [prospect_phone2] => 
        [prospect_title] => Sw Engineer 
        [prospect_company] => Talisman 
        [prospect_name] => Naveen 
        [prospect_message] => MESSAGE 
        [create_timestamp] => 2013-01-02 16:32:38 
        [update_timestamp] => 2013-01-02 16:35:48 
        [status] => 0 
        [track_id] => d1da0adc-549d-11e2-abeb-00151771ff65 
        [initiated_by] => SMS 
        [subscriber_email] => [email protected] 
        [vendor_email] => [email protected]
    ),
    ...
)
php arrays json loops
2个回答
1
投票

试试这个:

json_decode($user->get_detail($uid), true);

参考:http://php.net/manual/en/function.json-decode.php

当为 TRUE 时,返回的对象将被转换为关联数组。


0
投票

json_decode - 获取 JSON 编码字符串并将其转换为 PHP 变量。

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

assoc - 当

TRUE
时,返回的对象将被转换为关联数组。

json_decode($user->get_detail($uid), TRUE);

如果您要以数组形式访问这些值。

© www.soinside.com 2019 - 2024. All rights reserved.