What is a clean way to convert an array that looks like this:
[584] => Array ( [link_id] => 1 [site_id] => 5 [COUNT(*)] => 2 )
[585] => Array ( [link_id] => 243 [site_id] => 5 [COUNT(*)] => 2 )
[586] => Array ( [link_id] => 522 [site_id] => 89223 [COUNT(*)] => 3 )
To an array where the key is the site_id from above, so for the above example the resulting array would be:
[5] => Array( 1, 2, 243, 2) //Even ones and 0 are link_id, odd ones are count(*)
[89223] => Array(522, 3)
So, basically, group them by site_id. I still need to keep the relationship between link_id and count(*), in the case above I am doing it by the positions (so 0 and 1 are together, 2 and 3, etc) but I am open to a new structure as well.
Thanks!
You mean something like this? (Demo):
$out = array();
foreach($input as $v)
{
$site_id = $v['site_id'];
unset($v['site_id']);
$out[$site_id][] = $v;
}
Or in case you prefer value pairs after each other (Demo):
...
unset($v['site_id']);
$out[$site_id][] = array_shift($v);
$out[$site_id][] = array_shift($v);
} ...
Related
this is quite beyond me. Appreciate some help.
I have an array in php like so:
[0] => Array
(
[cust_id] => 1006
[no_of_subs] => 2
[dlv_id] => 1000
)
[1] => Array
(
[cust_id] => 1011
[no_of_subs] => 3
[dlv_id] => 1000
)
[2] => Array
(
[cust_id] => 1012
[no_of_subs] => 5
[dlv_id] => 1001
)
[3] => Array
(
[cust_id] => 1013
[no_of_subs] => 6
[dlv_id] => 1001
)
I don't need the cust_id field. I just need to group the dlv_id and the sum of no_of_subs for each matching dlv_id. The result should look like this:
[0] => Array
(
[dlv_id] => 1000
[no_of_subs] => 5
)
[1] => Array
(
[cust_id] => 1011
[no_of_subs] => 11
)
Thank you for any help.
I don't understand the downvotes for this question. Am i doing it all wrong? Downvoting without a reason is not helping.
The simplest, most efficient way to group and sum is to perform a single loop and assign temporary associative keys.
When a row is identified as a new dlv_id row, save the two desired elements, otherwise add the no_of_subs value to the pre-existing value.
Optionally, remove the temporary keys with array_values().
Code (Demo)
$array = [
["cust_id" => 1006, "no_of_subs" => 2, "dlv_id" => 1000],
["cust_id" => 1011, "no_of_subs" => 3, "dlv_id" => 1000],
["cust_id" => 1012, "no_of_subs" => 5, "dlv_id" => 1001],
["cust_id" => 1013, "no_of_subs" => 6, "dlv_id" => 1001]
];
foreach ($array as $row) {
if (!isset($result[$row["dlv_id"]])) {
$result[$row["dlv_id"]] = ["dlv_id" => $row["dlv_id"], "no_of_subs" => $row["no_of_subs"]];
} else {
$result[$row["dlv_id"]]["no_of_subs"] += $row["no_of_subs"];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'dlv_id' => 1000,
'no_of_subs' => 5,
),
1 =>
array (
'dlv_id' => 1001,
'no_of_subs' => 11,
),
)
Using array_column function, we can extract out dlv_id and no_of_subs separately in two different arrays, using cust_id as the key.
Now, simply loop over the array of dlv_id, and if matching key found, add the no_of_subs to it, else set the value (for the first time).
We use isset function to check if the key exists already or not.
Try the following:
// your input array is $input_array
// get all dlv_id maintaining the cust_id as index
$dlv_id = array_column($input_array, 'dlv_id', 'cust_id');
// get all no_of_subs maintaining the cust_id as index
$no_of_subs = array_column($input_array, 'no_of_subs', 'cust_id');
$output = array();
foreach ($dlv_id as $key => $value) {
if (isset($output[$value]['dlv_id'])) {
$output[$value]['dlv_id'] += $no_of_subs[$key];
} else {
$output[$value]['dlv_id'] += $no_of_subs[$key];
}
}
I have an array coming from my database and simply it consists of questions and answers. I am trying to merge 2 arrays and create multidimensional array if values are more than one.
Array
(
[0] => Array
(
[question_id] => 1
[option_id] => 1
)
[1] => Array
(
[question_id] => 2
[option_id] => 3
)
[2] => Array
(
[question_id] => 3
[option_id] => 5
)
[3] => Array
(
[question_id] => 3
[option_id] => 6
)
)
I've tried to separate answers and questions to 2 different arrays but couldn't figure how to merge them again.
$user_questions = array_column($answers, 'question_id');
$user_answers = array_column($answers, 'option_id');
What I need is (question 3 has 2 answers) :
Array
(
[1] => 1
[2] => 3
[3] => Array (5, 6)
)
You can group your data like this as you fetch the results from your query instead of processing it after the fact. To get the array you have now, you're currently doing something like this:
while ($row = $stmt->someFetchMethod()) {
$result[] = $row;
}
Instead, use the question id as the key in your result array, and append the option id to an array at that key.
while ($row = $stmt->someFetchMethod()) {
$result[$row['question_id']][] = $row['option_id'];
}
Below code will create a new array by looping the existing array.
// Considering your existing array to be like this
$array = array(
'0' => array('question_id' => 1,'option_id' => 1),
'1' => array('question_id' => 2, 'option_id' => 3 ),
'2' => array('question_id' => 3,'option_id' => 5),
'3' => array('question_id' => 3,'option_id' => 6)
);
//define new array
$new_array = array();
// loop the array
foreach($array as $key=>$value){
// if the option/answer is already set to to question key
if(isset($new_array[$value['question_id']])){
// if question key is an array, push new option to that array
if(is_array($new_array[$value['question_id']])){
array_push($new_array[$value['question_id']], $value['option_id']);
}else{
// convert question key to array with the old value and new option value
$new_array[$value['question_id']] = array($new_array[$value['question_id']],$value['option_id']);
}
}
else{
// assing option as value to question key
$new_array[$value['question_id']] = $value['option_id'];
}
}
print_r($new_array);
Out put:
Array
(
[1] => 1
[2] => 3
[3] => Array
(
[0] => 5
[1] => 6
)
)
my problem is i can't arrange the array in the structure i want.
array1 and array2 are generated dynamically. as you can see in the array 2 it has a subjectid which is the same in array1, that means, that element is under the subject cpe 305. all elements in the array 2 which has the id of 5 is under the the subject of cpe 305. same logic with cpe 304.
array1:
Array
(
[0] => Array
(
[subjectid] => 5
[subjectcode] => Cpe 305
)
[1] => Array
(
[subjectid] => 4
[subjectcode] => Cpe 304
)
)
array2:
Array
(
[0] => Array
(
[subjectid] => 5
[soid] => 1
[socode] => A
[sodesc] => Ability to apply knowledge of mathematics and science to solve engineering problems
)
[1] => Array
(
[subjectid] => 5
[soid] => 3
[socode] => C
[sodesc] => Ability to design a system, component, or process to meet the desired needs within realistic constraints such as economic, environmental, social, political, ethical, health and safety, manufacturability, and sustainability, in accordance to standards
)
[2] => Array
(
[subjectid] => 5
[soid] => 4
[socode] => D
[sodesc] => Ability to function on multidisciplinary teams
)
[3] => Array
(
[subjectid] => 5
[soid] => 5
[socode] => E
[sodesc] => Ability to identify, formulate, and solve engineering problems
)
[4] => Array
(
[subjectid] => 5
[soid] => 9
[socode] => I
[sodesc] => Recognition of the need for, and an ability to engage in life-long learning
)
[5] => Array
(
[subjectid] => 4
[soid] => 10
[socode] => J
[sodesc] => Knowledge of contemporary issues
)
)
OUTPUT (my desired structure)
Array(
[subjectid] => 5
[subjectcode] => Cpe 305
[sodetails] => array(
[0]=>Array ([soid]=>1
[socode]=>A)
[1]=>Array([soid]=>3
[socode]=>C .....until the last
)
[subjectid] => 4
[subjectcode] => Cpe 305
[sodetails] => array(
[0]=>Array ([soid]=>10
[socode]=>J)
.......until the last
)
what i've tried
this is the code that im testing. i only include few data here in my test code.
$so = array();
$exist = array();
foreach ($this->subject as $key => $value) {
foreach ($this->sodetails as $key2 => $value2) {
if($value['subjectid'] === $value2['subjectid']){
$exist = array(
"subjectid" => $value['subjectid'],
"details" =>array(
"soid" => $value2['soid']
)
);
array_push($so, $exist);
}
}
}
First of all your output array is incorrect. An array cannot have more than one same indexes like "subjectid", "subjectcode". We need to have a different index. It will be wise to use subjectid as index for outer array.
First prepare your outer array as it is array1.
foreach($this->subject as $key => $val){
$myArray[$val['subjectid']]['subjectid'] => $val['subjectid'];
$myArray[$val['subjectid']]['subjectcode'] => $val['subjectcode'];
}
Now iterate your sodetails array.
foreach($this->sodetails as $key => $val){
$temp['soid'] = $val['soid'];
$temp['socode'] = $val['socode'];
array_push($myArray[$val['subjectid']]['sodetails'], $temp);
}
There can be other ways too. But it is my style and I believe it will solve your problem.
You just need to pass subjectid as key in your return array,
$exist = array();
foreach ($this->subject as $key => $value) {
foreach ($this->sodetails as $key2 => $value2) {
if($value['subjectid'] === $value2['subjectid']){
$exist[$value['subjectid']]['subjectid'] = $value['subjectid'];
$exist[$value['subjectid']]['subjectcode'] = $value['subjectcode'];
$exist[$value['subjectid']]['sodetails'] = array(array('soid'=>$value2['soid']),array('socode'=>$value2['socode']));
}
}
}
Instead of running the loop/iterations for m x n times, what you could do is first have the subjectid as the key for the $arrray1 and let the natural PHP array key handling do the job.
$new_array1 = array();
foreach($arrray1 as $item){
$new_array1[$item['subjectcode']] = $item;
}
foreach($array2 as $desc){
if(array_key_exists($desc['subjectid'],$new_array1){
$new_array1[$desc['subjectid']]['desc'][] = $desc;
}
}
This way you only have to make m + n iterations.
I have an array that's output is this:
Array ( [winners] =>
Array ( [0] => Gold Member
[1] => CROTCH SNIFFER
[2] => TEAM #1 )
[prizeTotal] => 20 )
Array ( [winners] =>
Array ( [0] => TEAM #1
[1] => CROTCH SNIFFER )
[prizeTotal] => 60 )
Array ( [winners] =>
Array ( [0] => Gold Member
[1] => TEAM #1 )
[prizeTotal] => 30 )
Array ( [winners] =>
Array ( [0] => TEAM #1
[1] => TEAM #2
[2] => SCREW-NUT-BOLT )
[prizeTotal] => 90 )
Please forgive the names...it's not my DB.
I can not change the way the array is show here.
With that being said how can I group and sum?
1. For each winners array there is a prizeTotal below the team names. That prize total should be the value of each teamName above it.
Example
Array ( [winners] =>
Array ( [0] => Gold Member
[1] => CROTCH SNIFFER
[2] => TEAM #1 )
[prizeTotal] => 20 )
Gold Member should have 20
CROTCH SNIFFER should have 20
TEAM #1 should have 20 AND
Array ( [winners] =>
Array ( [0] => TEAM #1
[1] => CROTCH SNIFFER )
[prizeTotal] => 60 )
TEAM #1 should have 60
CROTCH SNIFFER sould have 60....etc....
Then I want to group by team name and sum so that I can display...
CROTCH SNIFFER = 80
Gold Member = 50
TEAM #1 = 200
Team #2 = 90.
Thanks in advance...
Assuming you can collect all your arrays into one i.e. :
$arrays = array($array1,$array2,....,$arrayn);
then
$grouping = array();
foreach($arrays AS $array)
{
foreach($array['winners'] AS $k=>$v)
{
//check if the array key is a number, will contain a team, and if that
//them is not alreay listed
if(is_numeric($k) && !array_key_exists($v,$grouping))
$grouping[$v] = 0;
//sum the prize to the team's sum
$grouping[$v] += intval($array['winners']['prizeTotal']);
}
}
//the following is just for debugging
print_r($grouping);
this should produce something like:
$grouping[TEAM #1] = 200
$grouping[TEAM #2] = 90
$grouping[CROTCH SNIFFER] = 200
...
You need to make a new array to hold your results, then for each of your existing arrays, check if you're already processed one for that team
If you have, then simply add the prizeTotal to the prizeTotal stored in your new array's entry for that team.
If not, simply add the team's entry to your new array.
I have made a function that imitates mysql SUM() and GROUP BY. I hope it will fit your needs:
$in_a = array(
array("a" => 0,"b"=>0,"s"=> 1),
array("a" => 0,"b"=>0,"s"=> 2),
array("a" => 1,"b"=>1,"s"=> 1),
array("a" => 0,"b"=>1,"s"=> 1),
array("a" => 0,"b"=>1,"s"=> 1),
array("a" => 1,"b"=>0,"s"=> 1),
array("a" => 0,"b"=>1,"s"=> 1),
array("a" => 1,"b"=>1,"s"=> 1),
array("a" => 1,"b"=>0,"s"=> 1),
);//input array exaple
$group_by_a = array("a","b");//input array will be grouped by these
$sum_a = array("s"); //'s' values of input will be summed
$out_a = array(); //this is the output array
foreach($in_a as $in_i => $in)
{
$add = false;
foreach($out_a as $out_i => $out)
{
$add = true;
foreach($group_by_a as $group_by)
if($in[$group_by] != $out[$group_by])
{
$add = false;
break;
}
if($add)
{
foreach($sum_a as $sum)
$out_a[$out_i][$sum] += $in[$sum];
break;
}
}
if(!$add)
{
foreach($group_by_a as $group_by)
$out_a[$in_i][$group_by] = $in[$group_by];
foreach($sum_a as $sum)
$out_a[$in_i][$sum] = $in[$sum];
}
}
the result:
Array
(
[0] => Array
(
[a] => 0
[b] => 0
[s] => 3
)
[2] => Array
(
[a] => 1
[b] => 1
[s] => 2
)
[3] => Array
(
[a] => 0
[b] => 1
[s] => 3
)
[5] => Array
(
[a] => 1
[b] => 0
[s] => 2
)
)
I have some array containing other arrays:
Array
(
[0] => Slip Object
(
[userId:protected] => 1
[parentSlipId:protected] => 0
[id:protected] => 25
[madeDatetime:protected] => 2011-04-19 17:13:09
[stake:protected] => 34.00
[status:protected] => 6
)
[1] => Slip Object
(
[userId:protected] => 1
[parentSlipId:protected] => 0
[id:protected] => 25
[madeDatetime:protected] => 2011-04-19 17:13:09
[stake:protected] => 34.00
[status:protected] => 6
)
[2] => Slip Object
(
[userId:protected] => 1
[parentSlipId:protected] => 0
[id:protected] => 24
[madeDatetime:protected] => 2011-04-18 11:31:26
[stake:protected] => 13.00
[status:protected] => 6
)
)
What's the best way of counting unique arrays?
Off the top of my head you could try:
$hashes = array();
$uniques = 0;
foreach($array as $slip) {
$hash = sha1(serialize($slip));
if(!in_array($hash, $hashes)) {
++$uniques;
$hashes[] = $hash;
}
}
var_dump($uniques); // prints total number of unique objects.
Edit:
#biakaveron's idea looks better though and could be adapted to:
$uniques = count(array_unique($array, SORT_REGULAR));
var_dump($uniques); // prints total number of unique objects.
This previous question has various solutions for removing duplicate arrays from within an array. If you implement any of them and then use sizeof() on the returned array you will have your solution.
eg:
<?php
$yourarray = array();
$tmp = array ();
foreach ($yourarray as $row)
if (!in_array($row,$tmp)) array_push($tmp,$row);
echo sizeof($tmp);
?>