I'm trying to rank a group of scores. So I've taken the input from a users form and sorted them from largest to smallest in a multidimensional array, so then that I can search the array for the user and retrieve the index to get its "rank" however I appear to be doing something totally wrong! any help on where I'm going wrong would be greatly appreciated, or if there is a better way entirely!
Thank you
PHP Code:
if ( isset( $_POST['submit'] ) ) {
$size = $num_rows;
$p = 0;
$myarray = array();
while ( $p < $size ) {
$myarray[] = array( "user" => $user[$p], "data" => $scanrate[$p] );
$p++;
}
$sort = array();
foreach ( $myarray as $k => $v ) {
$sort['data'][$k] = $v['data'];
}
array_multisort( $sort['data'], SORT_DESC, $myarray );
for ( $i = 0; $i < $num_rows; $i++ ) {
$key = array_search( $i, array_column( $myarray, 'user' ) );
if ( !$key ) {
$key = $num_rows;
}
//this is stored into my database
echo "<br>User " . $user[$i] . " scan rate " . $scanrate[$i] . " rank " . $key;
}
}
You do useless code to do this sort, use the usort() function like :
while($p < $size)
{
$myarray[] = array("user" => $user[$p], "data" => $scanrate[$p]);
$p++;
}
usort($myarray, function($a, $b) {
return $a['data'] - $b['data'];
});
Now your $myarray is sorted and you can search and do what you want.
If you're putting too much effort on smt. with a scripting language, you're definitely doing it wrong...
$sort = array_combine($user, $scanrate);
asort($sort);
// $sort is sorted now, used it as you like:
// List them
foreach ($sort as $user => $scanrate) {
echo "<br>User " . $user . " scan rate " . $scanrate . " rank " . ++$i;
}
// Find the rank of specific user:
echo array_search('matt', array_keys($sort)) + 1;
Related
I am confused to count these words,
I've some data like this :
web = 1
sistem=1
web=1
sistem=1
web=1
sistem=1
sistem=0
sistem=0
web=0
sistem=0
web=0
sistem=0
web=0
web=0
I want to make result like this :
web = 3
sistem = 3
I'm using array_count_values(), but this result is not good
Array ( [web=1] => 3 [sistem=1] => 3 [sistem=0] => 4 [web=0] => 4 )
My code like this :
foreach ($g as $key => $kata) {
if (strpos($cleanAbstrak, $kata)) {
echo $kata . $ada . "<br>";
$p[] = $kata . "=" . $ada;
// print_r($p);
echo "<br><br>";
} else {
echo $kata, $tidak . "<br>";
$q[] = $kata . "=" . $tidak;
// $m = explode(" ", $q);
// print_r($q);
// echo $q . '<br>';
echo "<br><br>";
}
}
$s = array_merge($p, $q);
echo "<br><br>";
print_r($s);
echo "<br>";
$f = array_count_values($s);
// print_r($f);
echo "<br><br>";
thank you very much if you are willing to help me
RESULT THIS CODE
Another simple way is use a counter like that:
$web=0;
$sistem=0;
foreach ($g as $key => $kata) {
if (strpos($cleanAbstrak, $kata)) {
$sistem=$sistem + $ada;
} else {
$web=$web+$tidak
}
}
echo 'web='.$web.'<br> sistem='.$sistem;
First, you need to separate word and value.
Second, you need to check the value : if it's zero you let it go (can't hold it back anymore). Else you count the value ; if it's written, i suppose it can be greater than 1 ; if it's not, it should be "word", or nothing (which would greatly facilitate counting).
Something like
<?php
$tab = [
'web=1',
'sistem=1',
'web=1',
'sistem=1',
'web=1',
'sistem=1',
'sistem=0',
'sistem=0',
'web=0',
'sistem=0',
'web=0',
'sistem=0',
'web=0',
'web=0',
];
$tab_clean = [];
foreach($tab as $item) {
preg_match('/([a-z]+)=([\d]+)/', $item, $matches);
//print_r($matches);
$word = $matches[1];
$number = $matches[2];
for($i = 0; $i < $number; $i++) {
$tab_clean[] = $word;
}
}
$tab_count = array_count_values($tab_clean);
print_r($tab_count);
?>
I have a function to calculate distance, the data comes from a database.
Here is the code for calculating:
function jarak() {
global $conn;
$query1 = mysqli_query($conn, "SELECT signature, sig_priority FROM centro");
$query2 = mysqli_query($conn, "SELECT signature, sig_priority, status FROM acid_event");
while ($row = mysqli_fetch_array($query1, MYSQLI_ASSOC)) { $master[]=$row; }
while ($row = mysqli_fetch_array($query2, MYSQLI_ASSOC)) { $data[]=$row; }
$jarak = array();
foreach ($data as $key => $val) {
foreach ($master as $key2 => $value) {
$jarak = sprintf("%0.2f",sqrt(pow($val['signature'] - $value['signature'], 2) + pow($val['sig_priority'] - $value['sig_priority'], 2)));
echo "distance from (" . $value['signature'] . "," . $value['sig_priority'] . ") ke (" . $val['signature'] . "," . $val['sig_priority'] . ") is : " . $jarak . "<br>";
$euc[]=$jarak;
}
}
}
And here is the result from that:
Array(
[0] => 30.04
[1] => 0.00
[2] => 30.04
[3] => 0.00
[4] => 47.00
[5] => 17.03
[6] => 5.02
[7] => 25.08
[8] => 2.06
[9] => 32.06
[10] => 37.00
[11] => 7.07 )
I want to compare each 2 index array with greater than or less than.
Example : [0] with [1], [2] with [3], [4] with [5] and so on. It just compare with 2 index.
I tried this but no result
for ($i=0; $i<count($cb); $i++) {
for ($k=0;$k<2;$k++) {
if($cb[$i][$k]<$cb[$i][$k]) {
echo "low";
} elseif ($cb[$i][$k]>$cb[$i][$k]) {
echo "high";
}
}
}
Output I want should look like this
if [0] < [1] then "high" and it loop for other index array like [2] with [3], [4] with [5] and so on.
I think you were quite close to what you wanted to accomplish but it seems like you were making things harder than they needed to be.
Your code is below.
for ($i=0; $i<count($cb); $i++) {
for ($k=0;$k<2;$k++) {
if($cb[$i][$k]<$cb[$i][$k]) {
echo "low";
} elseif ($cb[$i][$k]>$cb[$i][$k]) {
echo "high";
}
}
}
As you can see your if statements are comparing the exact same value to each other, that's not going to do much. But I can see what you were trying to do in the second for loop.
Instead, what we really want to do is move through your array in steps of 2.
for ($i=0; $i<count($cb); $i+=2) {
//stuff
}
This way we can compare the first element and the element after that to each other. Like this:
if($cb[$i] > $cb[$i+1]) {
echo $i . 'is higher than '. $i+1;
} elseif($cb[$i] < $cb[$i+1]) {
echo $i . 'is lower than '. $i+1;
} else {
echo $i . 'is the same as '. $i+1;
}
So all together it would be something like this:
for ($i=0; $i<count($cb); $i+=2) {
if($cb[$i] > $cb[$i+1]) {
echo $i . 'is higher than '. $i+1;
} elseif($cb[$i] < $cb[$i+1]) {
echo $i . 'is lower than '. $i+1;
} else {
echo $i . 'is the same as '. $i+1;
}
}
Now you can change the echo's to whatever you want to do and you should probably add some validation too (like checking if the keys actually exist before accessing them), but this is a good place to get started.
I did a solution, but I decided to keep the response in an array because it makes more sense to me to be able to read this information somewhere else later.
Here's my solution:
$test = array(
30.04,
0.00,
30.04,
0.00,
47.00,
17.03,
5.02,
25.08,
2.06,
32.06,
37.00,
7.07,
);
$output = array();
foreach ($test as $key => $value) {
// To make sure this will only be executed every second item
if ($key % 2 !== 0) {
continue;
}
$next = '';
if (!isset($test[$key+1])) {
break;
} else {
$next = $test[$key+1];
}
$output[$key] = $value . ' is ' . ($value < $next
? "lower"
: ($value > $next
? 'higher'
: 'equal')) . ' than ' . $next;
}
echo '<pre>';
print_r($output);
Here's the code tested: https://3v4l.org/Pg5La
I'm using PHP to generate a list of references to a text by doing a preg_match_all search on a database table. Here is the PHP code:
$query = "SELECT primary_tag,display_short_title,content FROM topics;";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$num_results = mysql_num_rows($result);
for ($i = 0; $i < $num_results; $i++) {
$row = mysql_fetch_array($result);
if (preg_match_all("/(\<i\>U\<\/i\>|U) [0-9]{1,2}\.[0-9]{1,7}/", $row["content"], $matches)) {
foreach ($matches[0] as $match) {
$match = ltrim(strip_tags($match), "U ");
echo '<p class="textmark_result">' . $match;
echo ' ' . $row["display_short_title"] . '';
echo "</p>\n";
}
}
}
And the results (viewing source) look like this:
<p class="textmark_result">15.1737 Medicine</p>
<p class="textmark_result">5.678 Science</p>
<p class="textmark_result">14.665 Science</p>
In the resulting web page, I want to order the results by the decimal in the middle, the $match in the code, so that (in this example) 5.678 comes first, then 14.665, then 15.1737. Is there a way to do that?
Thank you!
Three steps to do:
Get all your matches and add them to an array - floatval($match) is the key.
Sort the resulting array by key (floats sort by numeric value, strings sort by characters - therefore floatval(...)).
Iterate on the sorted array
Code:
// MySQL stuff goes here ...
// create empty array
$results = array();
for ($i = 0; $i < $num_results; $i++) {
$row = mysql_fetch_array($result);
if (preg_match_all("/(\<i\>U\<\/i\>|U) [0-9]{1,2}\.[0-9]{1,7}/", $row["content"], $matches)) {
foreach ($matches[0] as $match) {
$match = ltrim(strip_tags($match), "U ");
// array pseudo key is the float value of $match
// add '_key' member for usort()
$row['_key'] = floatval($match);
$results[] = $row;
}
}
}
// sort the array by the float key
usort($results, function($a, $b) {
if($a['_key'] == $b['_key']) return 0;
elseif($a['_key'] > $b['_key']) return 1;
else return -1;
});
// ... then display stuff in order
foreach($results as $row) {
echo '<p class="textmark_result">' . (string)$row['_key'];
echo ' <a href="../Essays/topic.php?shorttitle='
. $row["primary_tag"] . '">' . $row["display_short_title"] . '</a>';
echo "</p>\n";
}
I have a multi-dimension array in php like this
$shop = array(
array("name","point","number"),
array('Ranjit', 1.25 , 15),
array('Pitabas', 0.75 , 25),
array('Khela', 1.15 , 7)
);
Now I have to show the output like this
name-> ranjit
Point-> 1.25
number->15
name->Pitabas
Point->0.75
number->25
name->Khela
Point->1.15
number->7
I am trying for loop, but I could get the result in nested forloop. Please help me to get the answer.
My solution:
$headings = array_shift($shop);
foreach ($shop as $item) {
foreach ($item as $key => $value) {
echo $headings[$key], '=>', $value;
}
}
Here's a simple loop: Observe that we skip the first element of the outer array, which is deemed to contain the headers:
for ($i = 1; $i != count($shop); ++$i)
{
print $shop[0][0] . ": ". $shop[$i][0] . "\n";
print $shop[0][1] . ": ". $shop[$i][1] . "\n";
print $shop[0][2] . ": ". $shop[$i][2] . "\n";
}
You know the first row will be the titles, so store them separately:
$titles = $shop[0];
That will give you
$titles = array('name', 'point', 'number');
Then loop through your array:
foreach ($shop as $index => $row) {
if ($index == 0)
continue;
foreach($row as $column => $item) {
echo $titles[$column] . " -> " . $item . "<br />";
}
}
This should give the desired output:
for($x = 1, $lim = sizeof($shop); $x < $lim; $x++)
{
echo $shop[0][0]."->".$shop[$x][0]."<br>";
echo $shop[0][1]."->".$shop[$x][1]."<br>";
echo $shop[0][2]."->".$shop[$x][2]."<br>";
}
I've got a rather complicated set of loops that pulls data out of mysql and compares it to values in an array and increments a counter. When I echo a flag when the counter is incremented, I get a bijilion flags (there're like 2600 records returned from the mysql query). But each time it prints, the counters are always 1 and when I print the counter's value at the end, it shows up as zero. It seems like something is re-setting the counter…
code
# ARRAY
$demographics=array(
"region"=>array(
"Northeast"=>array('total'=>0,'consented'=>0,'completed'=>0),
//more...
"West"=>array('total'=>0,'consented'=>0,'completed'=>0)
),"societal envirn"=>array(
"Urban"=>array('total'=>0,'consented'=>0,'completed'=>0)
),"age"=>array(
'18-19'=>array('total'=>0,'consented'=>0,'completed'=>0),
'20-24'=>array('total'=>0,'consented'=>0,'completed'=>0),
//more...
'55-59'=>array('total'=>0,'consented'=>0,'completed'=>0)
),
//more...
);
# LOOPS
while ($dbrecord = mysql_fetch_assoc($surveydata)) {
foreach ( $dbrecord as $dbfield=>$dbcellval ) {
foreach ( $demographics as $demographic=>$options ) {
foreach ( $options as $option=>&$counter ) {
if($demographic==="age"){
list($min,$max) = explode('-', $option);
if ($dbcellval >= $min && $dbcellval <= $max){
$counter['total']++;
echo '$' . $option . "['total'] = " . $counter['total'] . "<br />";
if ($dbrecord['consent']==="1"){
$counter['consented']++;
echo '$' . $option . "['consented'] = " . $counter['consented'] . "<br />";
if ($dbrecord['completion status']==="complete") {
$counter['completed']++;
echo '$' . $option . "['completed'] = " . $counter['completed'] . "<br />";
break 3;
} // if
} // if
break 2;
}
} // if age
else if ($option===$dbcellval){
$counter['total']++;
echo '$' . $option . "['total'] = " . $counter['total'] . "<br />";
if ($dbrecord['consent']==="1"){
$counter['consented']++;
echo '$' . $option . "['consented'] = " . $counter['consented'] . "<br />";
if ($dbrecord['completion status']==="complete") {
$counter['completed']++;
echo '$' . $option . "['completed'] = " . $counter['completed'] . "<br />";
break 3;
} // if
} // if
break 2;
} // else if $option==$dbcellval
} // foreach $options
} // foreach $demographics
} // foreach $dbrecord
} // while
sample output
$40-44['total'] = 1
$White['total'] = 1
$35-39['total'] = 1
$Northeast['total'] = 1 // the 'total' counter is 1
$Northeast['consented'] = 1
$Northeast['completed'] = 1
$South['total'] = 1
$Northeast['total'] = 1 // notice the 'total' counter is 1 again :(
$Northeast['consented'] = 1
$Northeast['completed'] = 1
You're defining counter from a foreach instruction, as $value in foreach($foo as $key=>$value), when using the foreach you only have a local copy of $counter.
You need to use either foreach($foo as $key=>&$value) or to refer to the full array path of your counter from $demographics.
You need to reference your array at each level, otherwise you are working on a copy of the data:
foreach ( $dbrecord as $dbfield=>$dbcellval ) {
foreach ( $demographics as $demographic => &$options ) {
foreach ( $options as $option => &$counter ) {
if($demographic==="age"){
list($min,$max) = explode('-', $option);
if ($dbcellval >= $min && $dbcellval <= $max){
$counter['total']++;
What if you simply stick with the $demographics variable.
i.e.
...
foreach ( $options as $option ) {
...
$demographics[$option]['total']++;
...