从foreach循环构建一个对象

时间:2014-03-20 16:31:31

标签: php json foreach

我想从结果集中构建一个数组,对其进行编码然后将其放入单个对象中。我的问题是我制作了很多对象,但我希望我的所有数据都在一个对象中。问题是我在我的foreach循环中回显了json编码中的多个对象。我如何从foreach循环中获取所有数据并将其放入一个对象中?任何帮助表示赞赏。以下是我的代码。基本上,我需要的是这个。

{"item1":"itemdata","category":"mycategory"}

但是在一个对象中。我不想要多个{} {} {}

$counter = 0;
$itemID = '';
foreach ($resultsTwo as $result) {
        if ($counter >= 0 && $itemID != $result['item_id']) {
                $description = $result['item_desc'];
                $ID = substr($result['item_id'], 3, 6);
                if ($result['bidder'] == 9999999999) {
                        $bid = $result['amount_bid'] + $result['min_bid_increment'];
                } else {
                        $bid = preg_replace('~\.0+$~','',$result['amount_bid']);
                }
                //echo $ID . ' ' . $bid . '<br />';
                $build['bid'] = $bid;
                $build['id'] = $ID;
                $build['item_desc'] = $description;         
        }
        $itemID = $result['item_id'];
        $counter++;

        echo json_encode($build);
}

2 个答案:

答案 0 :(得分:2)

创建一个数组以在循环之前保存较小的数组。

$fullData = array();

然后,在完成构建数组后,在循环内部将构建数组添加到fullData数组。

$fullData[] = $build;

删除当前的json_encode(),然后在循环外删除。

echo json_encode($fullData);

这将改为:

<?php
$counter = 0;
$itemID = '';

$fullData = array();

foreach ($resultsTwo as $result) {
    if ($counter >= 0 && $itemID != $result['item_id']) {
        $description = $result['item_desc'];
        $ID = substr($result['item_id'], 3, 6);
        if ($result['bidder'] == 9999999999) {
            $bid = $result['amount_bid'] + $result['min_bid_increment'];
        } else {
            $bid = preg_replace('~\.0+$~','',$result['amount_bid']);
        }
        //echo $ID . ' ' . $bid . '<br />';
        $build['bid'] = $bid;
        $build['id'] = $ID;
        $build['item_desc'] = $description;         
    }
    $itemID = $result['item_id'];
    $counter++;

    $fullData[] = $build;

}
echo json_encode($fullData);
?>

答案 1 :(得分:0)

更改此

$counter = 0;
$itemID = '';
foreach ($resultsTwo as $result) {
        if ($counter >= 0 && $itemID != $result['item_id']) {
                $description = $result['item_desc'];
                $ID = substr($result['item_id'], 3, 6);
                if ($result['bidder'] == 9999999999) {
                        $bid = $result['amount_bid'] + $result['min_bid_increment'];
                } else {
                        $bid = preg_replace('~\.0+$~','',$result['amount_bid']);
                }
                //echo $ID . ' ' . $bid . '<br />';
                $build['bid'] = $bid;
                $build['id'] = $ID;
                $build['item_desc'] = $description;         
        }
        $itemID = $result['item_id'];
        $counter++;

        echo json_encode($build);
}

$counter = 0;
$itemID = '';
foreach ($resultsTwo as $result) {
        if ($counter >= 0 && $itemID != $result['item_id']) {
                $description = $result['item_desc'];
                $ID = substr($result['item_id'], 3, 6);
                if ($result['bidder'] == 9999999999) {
                        $bid = $result['amount_bid'] + $result['min_bid_increment'];
                } else {
                        $bid = preg_replace('~\.0+$~','',$result['amount_bid']);
                }
                //echo $ID . ' ' . $bid . '<br />';


                 $build[] = array('bid'=>$bid,'id'=>$ID,'item_desc'=>$description);  


        }
        $itemID = $result['item_id'];
        $counter++;
         }




        echo json_encode($build);