将新数据追加到对象

时间:2018-10-15 18:34:48

标签: php json object

我一直在尝试将新数据附加到为AMchart创建的stdClass对象上

我要从数据库返回我想要的所有行,然后创建一个新对象并遍历返回的数组,但是并没有将我想要的内容附加到现有对象的末尾,而是被覆盖了。 PHP对象没有append或push方法,那么如何实现呢?

这是我的代码的样子。我想念一些简单的东西吗?

    $sql = 'SELECT 
            count(*) as clients,
            STR_TO_DATE(Appt_date, \'%m/%d/%Y\') AS date,
            SUM(wait_time) as total_wait_time
            FROM tb_by_client
            WHERE status = @qualifier
            GROUP BY Appt_date';

    $rows = $db->fetchAll($sql);
    $chartObject = new stdClass();
    foreach($rows as $row){
        $row->average = round($row->total_wait_time / $row->clients);
        $chartObject->date = $row->date;
        $chartObject->average = $row->average;
    }
    $chartArray[] = $chartObject;
    return json_encode($chartArray);

所以没有得到像这样的东西

[{"date":"2018-10-01","average":12},{"date":"2018-10-02","average":-33},{"date":"2018-10-04","average":23},{"date":"2018-10-05","average":6}]

我只回来了一个

[{"date":"2018-10-01","average":12}]

因为每个循环都会覆盖最后一个键和值

您如何附加?

3 个答案:

答案 0 :(得分:5)

您的问题是您不保存就覆盖了数据

>python setup.py install
running install
running bdist_egg
running egg_info
creating Opy.egg-info
writing Opy.egg-info\PKG-INFO
writing top-level names to Opy.egg-info\top_level.txt
writing dependency_links to Opy.egg-info\dependency_links.txt
writing manifest file 'Opy.egg-info\SOURCES.txt'
reading manifest file 'Opy.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no previously-included files matching '*.pyc' found anywhere in distribution
warning: no previously-included files matching '*.des' found anywhere in distribution
writing manifest file 'Opy.egg-info\SOURCES.txt'
installing library code to build\bdist.win32\egg
running install_lib
running build_py
creating build
creating build\lib
creating build\lib\opy
copying opy\opy.py -> build\lib\opy
copying opy\opymaster.py -> build\lib\opy
copying opy\__init__.py -> build\lib\opy
creating build\bdist.win32
creating build\bdist.win32\egg
creating build\bdist.win32\egg\opy
copying build\lib\opy\opy.py -> build\bdist.win32\egg\opy
copying build\lib\opy\opymaster.py -> build\bdist.win32\egg\opy
copying build\lib\opy\__init__.py -> build\bdist.win32\egg\opy
byte-compiling build\bdist.win32\egg\opy\opy.py to opy.pyc
byte-compiling build\bdist.win32\egg\opy\opymaster.py to opymaster.pyc
byte-compiling build\bdist.win32\egg\opy\__init__.py to __init__.pyc
creating build\bdist.win32\egg\EGG-INFO
copying Opy.egg-info\PKG-INFO -> build\bdist.win32\egg\EGG-INFO
copying Opy.egg-info\SOURCES.txt -> build\bdist.win32\egg\EGG-INFO
copying Opy.egg-info\dependency_links.txt -> build\bdist.win32\egg\EGG-INFO
copying Opy.egg-info\top_level.txt -> build\bdist.win32\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist\Opy-1.1.28.1-py2.7.egg' and adding 'build\bdist.win32\egg' to it
removing 'build\bdist.win32\egg' (and everything under it)
Processing Opy-1.1.28.1-py2.7.egg
Copying Opy-1.1.28.1-py2.7.egg to c:\python27\lib\site-packages
Adding Opy 1.1.28.1 to easy-install.pth file

Installed c:\python27\lib\site-packages\opy-1.1.28.1-py2.7.egg
Processing dependencies for Opy==1.1.28.1
Finished processing dependencies for Opy==1.1.28.1

cellClassRules = { 'your-css-class': params => { if (params.colDef.field === "myCurrentField" && params.data["previousField"] === "value") { return true; } else { return false; } }, 'your-other-css-class': params => {return false} } 的每次迭代中,您都更改了$chartObject = new stdClass(); foreach($rows as $row){ $row->average = round($row->total_wait_time / $row->clients); $chartObject->date = $row->date; $chartObject->average = $row->average; } $chartArray[] = $chartObject; 中的数据,但从未将其保存在foreach($rows as $row){中。

改为执行此操作

$chartObject

就我个人而言,我什至不用为使用对象而烦恼:

$chartArray

当您使用字符串键对数组进行JSON编码时,它将使其成为正确的Javascript对象结构。因此,实际上没有必要将所有这些对象都保留在内存中,并且代码更小,更简洁,更易于阅读。

我在代码中暗示的最后一件事是,对象是通过PHP(现在)通过引用传递的,如果您不为每次迭代创建对象的新实例,则实际上将所有引用更新为物体。可以这样说明:

foreach($rows as $row){
    $chartObject = new stdClass(); //new instance of stdClass, obj pass by refrence
    $row->average = round($row->total_wait_time / $row->clients);
    $chartObject->date = $row->date;
    $chartObject->average = $row->average;
    $chartArray[] = $chartObject;
}

输出:

foreach($rows as $row){
    $average = round($row->total_wait_time / $row->clients);
    $chartArray[] = ['date'=>$row->date,'average'=>$average];
}

Sanbox

每个数组都是$obj = new stdClass; $objects = []; for($i=0;$i<3;++$i){ $obj->foo = $i; $objects[] = $obj; print_r($objects); } 循环的单个迭代,这是一个数组,每次迭代后都会添加另一行。

您可以看到每个副本(实际上不是副本)都是通过数组中的引用进行更新的。基本上,我们存储了3次相同的对象(实例,将称他为Array ( [0] => stdClass Object ( [foo] => 0 ) ) Array ( [0] => stdClass Object ( [foo] => 1 ) [1] => stdClass Object ( [foo] => 1 ) ) Array ( [0] => stdClass Object ( [foo] => 2 ) [1] => stdClass Object ( [foo] => 2 ) [2] => stdClass Object ( [foo] => 2 ) ) ),而不是3个单独的对象(forBobBob)。

如果您存储的数据是人衬衫的颜色,则Alice穿红色衬衫时,他穿着红色衬衫,但是JohnBob却没有。

因此,您需要为每次迭代创建对象的新实例并将其存储。

希望有帮助!

答案 1 :(得分:3)

您可以在SQL中进行数学运算,从而完全消除循环...

   $sql = 'SELECT STR_TO_DATE(Appt_date, \'%m/%d/%Y\') AS date,
            round(SUM(wait_time)/count(*)) as average
            FROM tb_by_client
            WHERE status = @qualifier
            GROUP BY Appt_date';

    return json_encode($db->fetchAll($sql));

答案 2 :(得分:1)

您误会了循环中应该包含的内容,而不应该知道。

这应该很好:

$sql = 'SELECT 
        count(*) as clients,
        STR_TO_DATE(Appt_date, \'%m/%d/%Y\') AS date,
        SUM(wait_time) as total_wait_time
        FROM tb_by_client
        WHERE status = @qualifier
        GROUP BY Appt_date';

$rows = $db->fetchAll($sql);
$chartArray = [];
foreach($rows as $row){
    $row->average = round($row->total_wait_time / $row->clients);
    $chartObject = new stdClass();
    $chartObject->date = $row->date;
    $chartObject->average = $row->average;
    $chartArray[] = $chartObject;
}
return json_encode($chartArray);