I am currently stumped while searching for an answer, but I have an array which contains information relating to a tournament, as seen below.
I would like to search through this Array of arrays for specifically the opponent key, and determine if they are all -2. This is because the logic is that -2 is a bye, so if all people have -2 as their opponent, this means that all brackets have been finished.
My initial thought was just do an array_search(!(-2), $anArray), but that obviously didn't work and I hit myself on the head for thinking it would be that easy haha. Thanks.
EX:
Array (
[0] => Array ( [dId] => 11 [uId] => 3 [id] => 1 [round] => 0 [opponent] => 3 [bracket] => 0 )
[1] => Array ( [dId] => 11 [uId] => 5 [id] => 2 [round] => 0 [opponent] => -2 [bracket] => 1 )
[2] => Array ( [dId] => 11 [uId] => 10 [id] => 3 [round] => 0 [opponent] => 1 [bracket] => 0 ) )
This function will return true if all opponents are -2 and false if there is a single opponent != -2:
function all_opponent_search($arr){
for($i = 0; $i < count($arr); $i++)
if($arr[$i]['opponent'] != -2)
return false;
return true;
}
how about:
foreach ($masterArray as $subArray) {
if (isset($subArray['opponent']) && ($subArray['opponent'] == -2)) {
// do whatever you need
}
}
Related
I have this response
[0] => Array (
[name] => Test
[question_id] => 4
[question_choice_id] => 14
[choice_level] => 0
)
[1] => Array (
[name] => Test
[question_id] => 5
[question_choice_id] => 19
[choice_level] => 0
)
[2] => Array (
[name] => Test
[question_id] => 6
[question_choice_id] => 24
[choice_level] => 0
)
[3] => Array (
[name] => Test
[question_id] => 7
[question_choice_id] => 26
[choice_level] => 0
)
[4] => Array (
[name] => Test
[question_id] => 8
[question_choice_id] => 29
[choice_level] => 1
)
[5] => Array (
[name] => Test
[question_id] => 9
[question_choice_id] => 36
[choice_level] => 0
)
[6] => Array (
[name] => Test
[question_id] => 1
[question_choice_id] => 2
[choice_level] => 0
)
[7] => Array (
[name] => Test
[question_id] => 2
[question_choice_id] => 7
[choice_level] => 0
)
[8] => Array (
[name] => Test
[question_id] => 3
[question_choice_id] => 9
[choice_level] => 0
)
I want to get the percentage of the user with the formula of
Score = the_right_answer / total_count_array * 100
The correct answer has a value of 1 in the choice_level columns
so for my example is, the formula should be
Score = 1/ 9 * 100
How can I get the total from this array?
Once I get the answer I just like to return them to my view.
public function progress(){
$category_id = Session::get('category_id');
$user_set_id = Session::get('user_set_id');
$score = Answer::get_user_score($user_set_id,$category_id);
return view('pages.user.user_progress', [
'name' => '',
'score' => '',
]);
}
Can anyone help me on how to do this properly? any help would be really appreciated.
Based on Score = total count_of_array / the_right_answer * 100:
for total count_of_array could be calculated easily using count($answes)
for calculating the_right_answer, you can use array_map() or manual loop:
$total = count($answers);
$correct = 0;
foreach($answers as $answer){
if($answer['choice_level'] == '1'){
$correct++;
}
}
the snippet above will give you $correct as total correct answer
Now that you have the needed data, you can then do the calculation yourself. However, I would remind you that when the user doesn't have any correct answer, you will face a Division by zero warning. Keep that in mind😉
Since apparently choice_level can only take the values 0 or 1 you can use array_sum to get the number of correct answers. You will need to reduce the response array to just that field first, you can achive that with array_column. So all together:
$score = array_sum(array_column($answers, 'choice_level')) / count($answers) * 100;
for each($arrayname['score'] as $item){
}
I've got an array and I need count the keys with a specific value, which is proving to be a nightmare.
Array([0] => Array
(
[0] => 1
[ruleid] => 1
[1] => Test Outbound Life 1
[rule_name] => Test Outbound Life 1
[2] => Life Insurance
[product_type] => Life Insurance
[3] => 1
[status] => 1
[4] => 1000
[priority] => 1000
[5] => 100
[quantity] => 100
[6] => 1-2-3-4-5-6-7-
[dayofweek] => 1-2-3-4-5-6-7-
[7] => 2
[income] => 2
[8] => external/arc.php
[integrationfile] => external/arc.php
[9] => 1
[partnerid] => 1
)
[1] => Array
(
[0] => 2
[ruleid] => 2
[1] => Test Outbound Life 2
[rule_name] => Test Outbound Life 2
[2] => Life Insurance
[product_type] => Life Insurance
[3] => 1
[status] => 1
[4] => 800
[priority] => 800
[5] => 100
[quantity] => 100
[6] => 1-2-3-4-5-6-7-
[dayofweek] => 1-2-3-4-5-6-7-
[7] => 2
[income] => 2
[8] => test.php
[integrationfile] => test.php
[9] => 1
[partnerid] => 1
) )
The array will be generated dynamically so the same array will appear in the array.
I want to count how many times the same ruleid appears is it will look like this:
Array{
[1] => 1
[2] => 1
}
Update: I need to count how many times ruleid = 2 or how many times ruleid = 1
So you want to count the number of time each ruleid appears inside an array.
Let's call this array $count. This is how I'd do it.
$count = array();
foreach($arrays as $array) { // $arrays is your big ass array containing arrays
// increment the value with the key corresponding to ruleid (improved by JustOnUnderMillions)
$count[$array['ruleid']] = isset($count[$array['ruleid']]) ? ($count[$array['ruleid']] + 1) : 1;
}
print_r(count); // should give you what you're looking for
I have worked it out by looping and counting how many times a ruleid appears in an array.
foreach ($count as $key => $value) {
$c = 0;
//check that outbound has passed all rules
foreach ($out as $k => $v) {
if ($v['ruleid']==$key) {
$c +=1;
}
}
if ($c==$value) {
//add valid outbound to array
foreach ($out as $k => $v) {
if ($v['ruleid']==$key) {
$valid[$key] = $v;
}
}
}
}
The loop checks the ruleid has passed all rules I had already counted in the $count variable.
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
I know there are some other topics about sorting with multiple criteria, but they don't fix my problem.
Let's say I have this array:
Array
(
[0] => Array
(
[uid] => 1
[score] => 9
[endgame] => 2
)
[1] => Array
(
[uid] => 2
[score] => 4
[endgame] => 1
)
[2] => Array
(
[uid] => 3
[score] => 4
[endgame] => 100
)
[3] => Array
(
[uid] => 4
[score] => 4
[endgame] => 70
)
)
I want to sort it, putting the one with the HIGHEST score on top. On same score, I want the one with the LOWEST endgame number on top.
The sorting mechanisme should rank user1 on top, then user2, then 4 and then user3.
I use this sorting mechanisme:
function order_by_score_endgame($a, $b)
{
if ($a['score'] == $b['score'])
{
// score is the same, sort by endgame
if ($a['endgame'] == $b['endgame']) return 0;
return $a['endgame'] == 'y' ? -1 : 1;
}
// sort the higher score first:
return $a['score'] < $b['score'] ? 1 : -1;
}
usort($dummy, "order_by_score_endgame");
This gives me the following array:
Array
(
[0] => Array
(
[uid] => 1
[score] => 9
[endgame] => 2
)
[1] => Array
(
[uid] => 3
[score] => 4
[endgame] => 100
)
[2] => Array
(
[uid] => 2
[score] => 4
[endgame] => 1
)
[3] => Array
(
[uid] => 4
[score] => 4
[endgame] => 70
)
)
As you can see, the array isn't sorted properly... Anyone knows what I'm doing wrong? Thanks a lot!
Your function should be like this:
function order_by_score_endgame($a, $b) {
if ($a['score'] == $b['score']) {
// score is the same, sort by endgame
if ($a['endgame'] > $b['endgame']) {
return 1;
}
}
// sort the higher score first:
return $a['score'] < $b['score'] ? 1 : -1;
}
Try it out. It will give you result like this:
Array
(
[0] => Array
(
[uid] => 1
[score] => 9
[endgame] => 2
)
[1] => Array
(
[uid] => 2
[score] => 4
[endgame] => 1
)
[2] => Array
(
[uid] => 4
[score] => 4
[endgame] => 70
)
[3] => Array
(
[uid] => 3
[score] => 4
[endgame] => 100
)
)
I have a PHP object like below and all I want to know whats the the easiest way to get a count of objects where the property 'typeId' = 3
Array
(
[0] => ABC Object
(
[id] => 13
[typeId] => 3
[sortOrder] => 0
)
[1] => ABC Object
(
[id] => 12
[typeId] => 2
[sortOrder] => 0
)
[2] => ABC Object
(
[id] => 14
[typeId] => 4
[sortOrder] => 0
)
[3] => ABC Object
(
[id] => 15
[typeId] => 3
[sortOrder] => 0
)
)
A simple foreach counter should do:
$count = 0;
foreach ($array as $object) {
if ($object->typeId == 3) $count++;
}
No need to over complicate things
From my point of view, a much nicer solution is to use the array_filter function:
$newarray = array_filter( $old_array, function($object) { return $object->typeId == 3; } );
(note: inline functions only work since PHP 5.3)
Just create a temporary variable called objectCount or something similar, loop through your array and when you find an object where the typeId equals 3 add 1 to objectCount.
In the multidimensional array below, I would like to merge arrays that have the same merge_id. I'm not sure "merge" is the right word: in the example below, array['0'] should become array['0'] with in it array['0']['0'] and array['0']['1'], the latter being equal to array['1']. I hope this makes sense ...
The array comes out of the db sorted on merge_id so arrays with matching merge_id are always "next to" each other, and there will only ever be 2 with the same merge_id
As I loop through the array I know I need to keep a variable that is always equal to the previous merge_id and if there is a match between previous and current, then merge.
Array
(
[0] => Array
(
[client_id] => 5
[company_name] => company111_name
[id] => 3
[fee] => 111
[year] => 2009
[quarter] => 3
[date_inserted] => 1264948583
[description] => 2009 - Q3
[fee_type] =>
[merge_id] => a87ff679a2f3e71d9181a67b7542122c
[total_paid] => 0
[total_remainder] => 0
)
[1] => Array
(
[client_id] => 5
[company_name] => company111_name
[id] => 6
[fee] => 55.5
[year] => 2010
[quarter] => 2
[date_inserted] => 1264949470
[description] => 2010 - Q2
[fee_type] =>
[merge_id] => a87ff679a2f3e71d9181a67b7542122c
[total_paid] => 0
[total_remainder] => 0
)
[2] => Array
(
[client_id] => 5
[company_name] => company111_name
[id] => 4
[fee] => 111
[year] => 2009
[quarter] => 4
[date_inserted] => 1264948583
[description] => 2009 - Q4
[fee_type] =>
[merge_id] =>
[total_paid] => 0
[total_remainder] => 0
)
[3] => Array
(
[client_id] => 5
[company_name] => company111_name
[id] => 7
[fee] => 55.5
[year] => 2010
[quarter] => 3
[date_inserted] => 1264949470
[description] => 2010 - Q3
[fee_type] =>
[merge_id] =>
[total_paid] => 0
[total_remainder] => 0
)
)
Code
$merger = $data['search']['0']['merge_id'];
$i = 0;
foreach($data['search'] as $fee)
{
if($fee['merge_id'] == $merger)
{
//bump up & merge arrays
???
}
$merger = $fee['merge_id'];
$i++;
}
You can use the ID as key to put all items with the same ID in the same array:
$merged = array();
foreach ($data['search'] as $fee) {
if ($fee['merge_id'] == '') {
continue;
}
if (!isset($merged[$fee['merge_id']])) {
$merged[$fee['merge_id']] = array();
}
$merged[$fee['merge_id']][] = $fee;
}
$merged = array_values($merged);
Notice that this will skip the items with an empty merge ID. You could also use a default merge ID in that case by replacing continue; with $fee['merge_id'] = 0;.
foreach($array as $p)
$result[$p['merge_id']][] = $p;