Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用具有某些现有值的数组形成PHP数据_Php_Mysql_Arrays_Codeigniter_Codeigniter 3 - Fatal编程技术网

使用具有某些现有值的数组形成PHP数据

使用具有某些现有值的数组形成PHP数据,php,mysql,arrays,codeigniter,codeigniter-3,Php,Mysql,Arrays,Codeigniter,Codeigniter 3,假设我有这样的值 Array ( [0] => stdClass Object ( [amount] => 4000.00 [monthyear] => 2018-02 ) [1] => stdClass Object ( [amount] => 5000.00 [monthyear] => 2018

假设我有这样的值

Array
(
    [0] => stdClass Object
        (
            [amount] => 4000.00
            [monthyear] => 2018-02
        )

    [1] => stdClass Object
        (
            [amount] => 5000.00
            [monthyear] => 2018-01
        )
)
我想创造一些像这样的价值

[5000,4000,0,0,0,0,0,0,0,0,0,0]
我使用了foreach循环和for循环,但我不知道如何做到这一点,它实际上是按月生成一个余额报告的总和

是否可以通过CodeIgniter生成该值

我对上述输出的查询是:

$this->cm->select_sum = "amount";
$this->cm->select = "DATE_FORMAT(date,'%Y-%m') as monthyear";
$this->cm->table_name = "personal_balance";
$this->cm->where = array("DATE_FORMAT(date,'%Y')" => date('Y'));
$this->cm->group_by = "DATE_FORMAT(date,'%m')";
$balance = $this->cm->get();

像这样的事我想可以胜任

$monthBalance = [];
foreach($balance as $entry){
    $month = intval(substr($entry['monthyear']),6,7));
    $monthBalance[$month] = $entry;
}
//so there is [1=>obj(), 2=>obj(), 5=>obj] in it


$value = [];
for($m = 1; $m <= 12; $m++) {

    //if the balance month currently exists in the DB results
    if( isset($monthBalance[$m]) ){

       $value[] = $monthBalance[$m]->amount;

    } else {

       $value[] = 0;

    }
}
$monthBalance=[];
foreach(余额作为$entry){
$month=intval(substr($entry['monthyear',]),6,7);
$monthBalance[$month]=$entry;
}
//所以其中有[1=>obj(),2=>obj(),5=>obj]
$value=[];
对于($m=1;$m金额;
}否则{
$value[]=0;
}
}

首先,您需要能够成功创建新阵列
[5000,4000]
通过对现有数组进行迭代。然后,您可以使用“0”将新数组扩展到所需长度。报告将基于月份。首先放置值并不重要。这些值可以在任何位置。向我们展示您尝试过的循环。这行不通,因为如果第1、2和5个月有3个值,则输出f或者这个代码是[1,2,5,0,0,0,0,0,0,0,0,0],但是我需要得到这样的输出[1,2,0,0,0,5,0,0,0,0,0,0,0],希望你理解。哦,好的,我试着修复它^^