来自array_chunk的php echo作为字符串

时间:2017-03-21 04:11:32

标签: php arrays

我想将array_chunk作为字符串回显,我该怎么做? 这是代码

$rt = $this->db->query("SELECT id_reg_pd FROM 043104_kuliahmhs_20152_2a0dc380_temp");
$_datao = array_chunk($rt->result(), 3);
foreach($_datao as $batman => $robin) {
    print_r($robin);
}

我想将echo id_reg_pd作为字符串。 我试过尝试过:

echo $robin->id_reg_pd;

但是像这样得到php错误

A PHP Error was encountered
Severity: Notice
Message:  Trying to get property of non-object

这里是print_r($ robin);

的数组
Array
(
    [0] => stdClass Object
        (
            [id_reg_pd] => 001be76b-4e58-4cea-96cf-fee2d8e0abdc
        )

    [1] => stdClass Object
        (
            [id_reg_pd] => 001d4fe5-73f5-4bae-b126-1f787ea0104e
        )

    [2] => stdClass Object
        (
            [id_reg_pd] => 002ab28b-e0b9-464a-89fb-12552512a5d0
        ) 
)

3 个答案:

答案 0 :(得分:0)

循环$robin,然后检查

foreach($robin as $value)
{
    echo $value->id_reg_pd;
}

答案 1 :(得分:0)

试试这个

   for($i=0;$i<count($_datao);$i++){ 
    $newarr = (array) $robin[$i];
    echo $newarr['id_reg_pd'];
  }

答案 2 :(得分:0)

Sahil不正确。 必须使用for / foreach循环才能获得所需的结果。 array_column()适用于一组对象。如果您可以使用简单的implode()调用将数组转换为字符串,那么这是一个简单的单行代码:

代码(Demo):

$robin=[
    (object)['id_reg_pd'=>'001be76b-4e58-4cea-96cf-fee2d8e0abdc'],
    (object)['id_reg_pd'=>'001d4fe5-73f5-4bae-b126-1f787ea0104e'],
    (object)['id_reg_pd'=>'002ab28b-e0b9-464a-89fb-12552512a5d0']
];
//print_r($robin);  // uncomment to see for yourself
//var_export(array_column($robin,'id_reg_pd'));  // uncomment to see for yourself
echo implode(', ',array_column($robin,'id_reg_pd')); // implode with whatever glue you wish

输出:

001be76b-4e58-4cea-96cf-fee2d8e0abdc, 001d4fe5-73f5-4bae-b126-1f787ea0104e, 002ab28b-e0b9-464a-89fb-12552512a5d0