Combine 3 arrays into One - mysql

I have 3 Queries
$this->db->select('count(id) as Lead_Current_Month')->from('admin_lead_distribution');
$result1=$this->db->get()->result();
$this->db->select('count(id) as Lead_Current_Month')->from('level1_lead_distribution');
$result2=$this->db->get()->result();
$this->db->select('count(id) as Lead_Current_Month')->from('level2_lead_distribution');
$result3=$this->db->get()->result();
When I print it gives
Array
(
[0] => stdClass Object
(
[Lead_Current_Month] => 12
)
)
Array
(
[0] => stdClass Object
(
[Lead_Current_Month] => 38
)
)
Array
(
[0] => stdClass Object
(
[Lead_Current_Month] => 10
)
)
How to combine 3 arrays into one?
I want the result to be:
Array
(
[0] => stdClass Object
(
[Lead_Current_Month] => 60
)
)
[Lead_Current_Month] => 60(60 is sum all the 3 array values)

You can just sum up the values of the results, and I don't know why you want to store the information in any array/object, just store it in a variable.
$sum_result = $result1[0]->Lead_Current_Month + $result2[0]->Lead_Current_Month + $result3[0]->Lead_Current_Month;
echo $sum_result; //will echo 60

Related

Cakephp Table query: SUM returning 0

I'm having problems when using find to sum the field num_days from table events.
$events = TableRegistry::get('Events');
$query = $events->find('all')
->select(['used' => 'sum(num_days)'])
->first();
Why $query->used is always 0?
print_r($events->find('all')->select(['used' => 'sum(num_days)'])->toArray()) gives,
Array ( [0] => App\Model\Entity\Event Object ( [used] => 6 [[new]] => [[accessible]] => Array ( [*] => 1 ) [[dirty]] => Array ( ) [[original]] => Array ( ) [[virtual]] => Array ( ) [[errors]] => Array ( ) [[invalid]] => Array ( ) [[repository]] => Events ) )
6 is exactly the correct answer for the query and print_r shows it but $query->used is returning always 0.
try $query->order(['used' => 'DESC']); before $query->used
Also, we can add used as protected $_virtual = ['used']; inside Event.php Entity File

mysql cant save my big decimal number

I have a number like this
34.59704151614417
and I put datatype on decimal(16,14) but it has saved as 34.59704151614400
it means mysql save just 12 digit decimal
this is my array
Array
(
[geometry] => Array
(
[type] => Polygon
[coordinates] => Array
(
[0] => Array
(
[0] => Array
(
[0] => 32.34375
[1] => 48.690960390925
)
[1] => Array
(
[0] => 16.875
[1] => 34.597041516144
)
[2] => Array
(
[0] => 43.2421875
[1] => 31.653381399664
)
[3] => Array
(
[0] => 64.3359375
[1] => 40.97989806962
)
[4] => Array
(
[0] => 32.34375
[1] => 48.690960390925
)
)
)
)
)
I understand
after I do json_decode of my array it reduce it's digit to 12 number
Set precision directive:
ini_set('precision', 16);
It is currently 14 and that's why the decimals get reduced. Check this link about more information

how to combine mysql query in CodeIgniter

I need to combine those two array into a single array.
1st query result
Array ( [0] => stdClass Object ( [p_type] => Bank [tsales] => 131000 [tdues] => 55000 [tpaid] => 75000 ) [1] => stdClass Object ( [p_type] => Cash [tsales] => 104000 [tdues] => 50000 [tpaid] => 54000 ) [2] => stdClass Object ( [p_type] => bKash [tsales] => 25000 [tdues] => 10000 [tpaid] => 5000 ) [3] => stdClass Object ( [p_type] => Pause [tsales] => 4000 [tdues] => 1000 [tpaid] => 2000 ) )
2nd query result
Array ( [0] => stdClass Object ( [dpayment] => 1000 ) [1] => stdClass Object ( [dpayment] => 5000 ) [2] => stdClass Object ( [dpayment] => 1000 ) [3] => stdClass Object ( [dpayment] => 5000 ) )
try to use array_merge. That should be very easy.
Your query result shows that you using $query->result() to get the value,
Instead of using result(), use $query->row_array();
then you get single dimensional array for both queries
then use array_merge() that would be easy

Laravel 4 : How to make a sub-query with Query Builder?

I have two tables. There is a table place that contains data as shown below:
id name
-----------
1 A1
2 A2
3 A3
4 B1
Other table place_conditions contains data as sown below:
id placeID desription
----------------------------
1 1 Windy
2 1 Dry
3 3 Wet
4 3 Rainy
4 4 Dry
I like to made a query in laravel 4 that make result like this below:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => A1
[place_cond] => Array
(
[0] => stdClass Object
(
[description] => Windy
)
[1] => stdClass Object
(
[description] => Dry
)
)
)
[1] => stdClass Object
(
[id] => 2
[name] => A2
[place_cond] => Array
(
)
)
[3] => stdClass Object
(
[id] => 3
[name] => A3
[place_cond] => Array
(
[0] => stdClass Object
(
[description] => Wet
)
[1] => stdClass Object
(
[description] => Rainy
)
)
)
)
Here part of my query (it still error) :
$query = DB::table('place');
if(Input::has('search')){
$query->where(function($a){
$a->where('name','LIKE','%'.Input::get('search').'%');
});
}
/* $query->select('place.*',DB::raw('SELECT Description as place_cond FROM place_conditions WHERE place_conditions.placeID = place.id')); // sub query . Arrggghhhhhh... whyyyy.. stillllll.. errorrrrrrr.. */
$data = $query->get();
Is possible to produce results like the above in laravel 4? Can you help me? Thank you

Sort array (stdClass Object)

Getting this result after array_merge of 2 arrays:
Array ( [0] => stdClass Object ( [tid] => 3954 [name] => Alleman )
[1] => stdClass Object ( [tid] => 3958 [name] => Jack )
[2] => stdClass Object ( [tid] => 3963 [name] => Aquin Catholic ));
I want to sort the array by [name]. I have been reading and searching for hours but cannot figure out how to sort passed the stdClass Object to [name].
Have tried sort, ksort, uasort, etc. wont sort passed the stdClass Object to [name].
Example
function compareItems($a, $b)
{
if ( $a->name < $b->name ) return -1;
if ( $a->name > $b->name ) return 1;
return 0; // equality
}
uasort($players, "compareItems");