简体   繁体   English

合并$ query-> result()和数组codeigniter的结果

[英]Merging result from $query->result() and an array codeigniter

I have an sql query which returns values. 我有一个返回值的SQL查询。 I would want to add new values with keys so that I can use it to my next controller which is these values doesn't come from the query result. 我想用键添加新值,以便可以将其用于下一个控制器,因为这些值不是来自查询结果。

My Model: 我的模特:

$paymentDetails = $this->db->query($sql);
$payments = $paymentDetails->result();

//these are the values I wanted to add to the result for the $payments
$amountDue = 'Sample';
$change = 'Sample';

$result = array_merge($payments,  array(
                "AmountDue" => $amountDue,
                "Change" => $change
            ));
if($result){
    return $result;
}else{
    return false;
}

I wonder why array_merge() doesn't work. 我不知道为什么array_merge()不起作用。 In my view, values fetched from the SQL Query did returned, but the added (AmountDue and Change) is not found. 在我看来,确实返回了从SQL查询中获取的值,但是找不到添加的(AmountDue和Change)。

Please help me. 请帮我。 Thank you so much! 非常感谢! Ya'll be a big help for my project. 对我的项目有很大的帮助。 :) :)

because result() function returns the query result as an array of objects, or an empty array on failure. 因为result()函数以对象数组或失败时为空数组的形式返回查询结果。 It is just an alias of result_object(). 它只是result_object()的别名。

You have to use result_array() which returns the query result as a pure array. 您必须使用result_array()将查询结果作为纯数组返回。

I hope this will help you. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM