I have this query, that produces next result
$result = DB::table('contagenshora')
->select(DB::raw('post_id as post_id'), DB::raw('product_id'), DB::raw('max(val) as maxHour'), DB::raw('hour'))
->whereBetween('date', array($dt->toDateTimeString(), $dt2->toDateTimeString()))
->where('hour', '=', $actHour->hour)
->groupBy(DB::raw('post_id'), DB::raw('product_id'))
->get();
Result: Array ( [0] => stdClass Object ( [post_id] => 1 [product_id] => 1 [maxHour] => 4 ) [1] => stdClass Object ( [post_id] => 1 [product_id] => 2 [maxHour] => 3 ) [2] => stdClass Object ( [post_id] => 4 [product_id] => 0 [maxHour] => 6 ) ) )
Now, I need the sum of "maxHour", but grouped by "post_id", I need something like this:
(For example, for post_id = 1, I need the sum of 4+3)
Result: Array ( [0] => stdClass Object ( [post_id] => 1 [maxHour] => 7 ) [1] => stdClass Object ( [post_id] => 4 [maxHour] => 6 ) ) )
How can I do this?
Just remove the product_id from group by clause and select then. Also no need to use DB::raw() for direct selectable columns.
$result = DB::table('contagenshora')
->select('post_id', DB::raw('max(val) as maxHour'))
->whereBetween('date', array($dt->toDateTimeString(), $dt2->toDateTimeString()))
->where('hour', $actHour->hour)
->groupBy('post_id')
->get();
Related
I have an array from an inner join statement from 2 Mysql tables... I need them in a better working format for front end display:
mysql query:
SELECT weddings.id, weddings.wImage, images.iLink FROM images INNER JOIN weddings ON weddings.id = images.weddingId
OUTCOME:
Array
(
[0] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => image2.jpg
)
[1] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => image3.jpg
)
[2] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => image4.jpg
)
[3] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => image5.jpg
)
[4] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => image6.jpg
)
[5] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => image7.jpg
)
[6] => Array
(
[id] => 11
[wImage] => image12.jpg
[iLink] => image8.jpg
)
[7] => Array
(
[id] => 11
[wImage] => image12.jpg
[iLink] => image9.jpg
)
[8] => Array
(
[id] => 11
[wImage] => image12.jpg
[iLink] => image10.jpg
)
)
So I need to work this array further to get this expected OUTCOME:
Array
(
[0] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => Array
(
[image] => image2.jpg
[image] => image3.jpg
[image] => image4.jpg
)
)
[1] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => Array
(
[image] => image5.jpg
[image] => image6.jpg
[image] => image7.jpg
)
)
[2] => Array
(
[id] => 10
[wImage] => image12.jpg
[iLink] => Array
(
[image] => image8.jpg
[image] => image9.jpg
[image] => image10.jpg
)
)
)
There are many ways, but assuming your id is unique (as I guess), you can use this id as an associative key in the output, so you can easily determine whether it exists already. In the last step you reindex the result array. See code comments for further explanation.
// $input_array is the outcome from sql
// $output_array will store the desired outcome
$output_array=[];
// loop through every item in input array
foreach ($input_array as $input_item) {
// if key with "id" already exists in output array
if (array_key_exists($input_item["id"], $output_array)) {
// append image to already existing "iLink" array
array_push($output_array[$input_item["id"]]["iLink"],
["image" => $input_item["iLink"]]
);
}
// if key with id not exists yet in output array
else {
// add current input item to output array,
// with "id" value as key
$output_array[$input_item["id"]] = [
"id" => $input_item["id"],
"wImage" => $input_item["wImage"],
// array with first image, can be appended later
"iLink" =>["image" => $input_item["iLink"]]
];
}
}
// now we have the result, but output array keys are not 0,1,2
// but the id values (3, 10 etc.), so we have to reindex
$output_array = array_values($output_array);
// now it is ready
var_dump($output_array);
code
$data = Event::with(['favourite' => function ($q){
$q->select('*');
$q->groupBy(function ($date) {
return Carbon::parse($date->created_at)->format('M'); **// this line have error**
});
}])
->select('id', 'title')
->get()
->where('id', $eventid)
->toArray();
//if not group by the data then the output will become
[0] => Array
(
[id] => 3
[title] => Everything You Need to Know About Business From Start-up to IPO, Straight From the VC's Mouth
[favourite] => Array
(
[0] => Array
(
[created_at] => 2020-09-10T17:54:06.000000Z
)
[1] => Array
(
[created_at] => 2020-09-10T18:21:00.000000Z
)
[2] => Array
(
[created_at] => 2020-09-28T15:05:38.000000Z
)
[3] => Array
(
[created_at] => 2020-08-28T15:05:38.000000Z
)
)
)
Final data should be
[0] => Array
(
[id] => 3
[title] => Everything You Need to Know About Business From Start-up to IPO, Straight From the VC's Mouth
[favourite] => Array
(
[Aug] => Array
(
[total] => 1
)
[Sep] => Array
(
[total] => 3
)
)
)
Anyone facing this error before? When I try to separate the data as a group. If will return error stripos() expects parameter 1 to be string, object given. The above code will return all the data from database.
I have these two arrays:
1:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Type 1
[rate] => 100.00
)
[1] => stdClass Object
(
[id] => 2
[name] => Type 2
[rate] => 75.00
)
[2] => stdClass Object
(
[id] => 3
[name] => Type 3
[rate] => 50.00
)
[3] => stdClass Object
(
[id] => 4
[name] => Type 4
[rate] => 50.00
)
)
2:
Array
(
[0] => stdClass Object
(
[name] => Type 1
[rate] => 125
)
[1] => stdClass Object
(
[name] => Type 2
[rate] => 85
)
[2] => stdClass Object
(
[name] => Type 3
[rate] => 65
)
)
What I need to do is compare the two arrays, and append missing items from 1st array to the 2nd one. This will always be the case first array will have more items than the second one.
I have tried using something like:
$result = array_udiff($array1,$array2,
function ($obj_a, $obj_b) {
return $obj_a->name - $obj_b->name;
}
);
but it just returns an empty array
This?
<?php
$arr1 = array(
(object)array("id"=>1,"name"=>"type 1","rate"=>100.00),
(object)array("id"=>2,"name"=>"type 2","rate"=>75.00),
(object)array("id"=>3,"name"=>"type 3","rate"=>50.00),
(object)array("id"=>4,"name"=>"type 4","rate"=>50.00)
);
$arr2 = array(
(object)array("name"=>"type 1","rate"=>125),
(object)array("name"=>"type 2","rate"=>85),
(object)array("name"=>"type 3","rate"=>65)
);
for($i=0;$i<sizeof($arr1);$i++){
$count=0;
for($j=0;$j<sizeof($arr2);$j++){
if($arr1[$i]->name == $arr2[$j]->name){
$count++;
}
}
if($count==0){
array_push($arr2,(object)array("name"=>$arr1[$i]->name,"rate"=>$arr1[$i]->rate));
}
}
print_r($arr2);
?>
Doesn't need to be complicated, assuming that you allow the arrays to contain objects of the same type and structure. We don't have enough context given the question to understand whether there is a good reason you can't.
//$array1 original array
//$array2 target array
$array2 = array_merge($array1, $array2);
I have trying to create a list of manufactures with a count.
This is my query
$query = $this->pdo->prepare('SELECT manufacture, count(*) AS count
FROM listed_watches
GROUP BY manufacture');
But when i do print_r, its duplicating the results. It is showing "manufacture" and "count" but its also showing [0] and [1], how come?
I just want it to show [manufacture], [count]
Array
(
[0] => Array
(
[manufacture] => Audemars Piguet
[0] => Audemars Piguet
[count] => 2
[1] => 2
)
[1] => Array
(
[manufacture] => Bell and Ross
[0] => Bell and Ross
[count] => 3
[1] => 3
)
[2] => Array
(
[manufacture] => Bulova
[0] => Bulova
[count] => 1
[1] => 1
)
)
try this :
$result = $query->fetchAll(PDO::FETCH_ASSOC);
You can use by default this fetching method in the initialization of your connection with :
$pdo_options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;
I have an array of apps with ids and categories like this:
[apps] => Array
(
[0] => stdClass Object
(
[id] => 0
[categoryid] => 0
)
[1] => stdClass Object
(
[id] => 31265
[categoryid] => 12
)
[2] => stdClass Object
(
[id] => 15965
[categoryid] => 2
)
[3] => stdClass Object
(
[id] => 16554
[categoryid] => 12
)
)
I am trying to get all apps for a category based on this request. So, the resultant output for:
For CategoryId 12:
----------------
[apps] => Array
(
[0] => 31265
[1] => 16554
)
For CategoryId 2:
----------------
[apps] => Array
(
[0] => 15965
)
For CategoryId 0:
----------------
[apps] => Array
(
[0] => 0
)
I believe i need to use nested foreach loops, but is there an efficient method?
Thanks
You could cycle through them, and place them into category-arrays:
foreach ( $apps as $app ) {
$catArray[ $app[CategoryID] ][] = $app;
}
This should result in an array whose key represents a category, and whose nested arrays represent those apps in that category.
I've worked up a demo of this online at: http://codepad.org/WZXIvQ58