Filling an array with null when empty - php

I couldn't find a similar problem, so I hope you guys can help me.
I'm trying to build a dashboard where there is an overview of the top 10 website positions in Google search over a period of time. Right now we get the data from Goolge and put it in a database, after that we extract the data from the database and manipulate it so it fits in a Highcharts chart.
The problem is that not every site is in the top 10 every time so there wil be holes in the data.
An array should look something like this in the end [9,7,8,0,0,10] for a date range like this ["2016-05-15", "2016-05-16", "2016-05-17", "2016-05-18", "2016-05-19", "2016-05-20"] (a zero is for everytime a site is not in the top 10). But the result we get right now is like this [9,7,8,10] (so it pushes all the values to the front of the array). I tried to calculate the amount of days between everytime the site is in the top 10, but this gives me an array like [9,9,9,9,8,9]
This is the code I have so far
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$matchFound = false;
for($i = 0; $i < count($urlData); $i++) {
if($urlData[$i]["keyword"] == $row["keyword"]){
addDates($row["date"]);
if(!isset($prevDate)){
$urlData[$i]["urlpos"][$row["url"]][] = $row["position"];
$prevDate = $row["date"];
}else {
if(calcDateDiff($prevDate, $row["date"]) > 1){
for($i = 0; $i < calcDateDiff($prevDate, $row["date"]); $i++){
$urlData[$i]["urlpos"][$row["url"]][] = 0;
}
$urlData[$i]["urlpos"][$row["url"]][] = $row["position"];
$prevDate = $row["date"];
}else {
$urlData[$i]["urlpos"][$row["url"]][] = $row["position"];
$prevDate = $row["date"];
}
}
$matchFound = true;
break;
}
}
if (!$matchFound) {
$urlData[] = array(
"keyword" => $row["keyword"],
"urlpos" => array(
$row["url"] => array($row["position"])
)
);
}
}
function calcDateDiff($firstAppearence, $seconAppearance){
$first = strtotime($firstAppearence);
$second = strtotime($seconAppearance);
$days = floor(($second - $first) / (60*60*24));
return $days;
}
Any help would be appreciated.

Use keys in your array like:
'fishes.com' => position [0=> 10, 1 => 0, 2=> ...]
Anyway, this code works fine:
$a = [9,7,8,0,0,10];
var_dump ($a);
So maybe you are not handling and assigning correctly.
So why don't you try to change the schema more like a DB, kind of, an array of dates, that contains the 10 top sites.
Other array, mapping the site id with the name. Or just site name directly if youu feel that could be uniquely retrieved. It would seem easier approach for me.

Related

Is there a faster way than array_diff in PHP

I have a set of numbers from MySQL within the range 1000 0000 (8 digits) to 9 999 999 999 (10 digits). It's supposed to be consecutive, but there are missing numbers. I need to know which numbers are missing.
The range is huge. At first I was going to use PHP to do this:
//MySqli Select Query
$results = $mysqli->query("SELECT `OCLC Number` FROM `MARC Records by Number`");
$n_array = array();
while($row = $results->fetch_assoc()) {
$n_array[] = $row["OCLC Number"];
}
d($n_array);
foreach($n_array as $k => $val) {
print $val . " ";
}
/* 8 digits */
$counter = 10000000;
$master_array = array();
/* 10 digits */
while ($counter <= 9999999999 ) {
$master_array[] = $counter;
$counter++;
d($master_array);
}
d($master_array);
$missing_numbers_ar = array_diff ($master_array, $n_array);
d($missing_numbers_ar);
d() is a custom function akin to var_dump().
However, I just realized it would take tons of time for this to be done. At the 15 minute mark, $master_array is being populated with only 4000 numbers.
How can I do this in a quicker way? MySQL-only or MySQL-and-PHP solutions both welcome. If the optimal solution depends on how many numbers are missing, please let me know how so. Tq.
Your d() probably is the cause of slowness, please remove it, and make small changes in your code
while($row = $results->fetch_assoc()) {
$n_array[$row["OCLC Number"]] = 1;
}
and
$missing_numbers_ar = [];
while ($counter++ <= 9999999999 ) {
if (empty($n_array[$counter])) {
$missing_numbers_ar[] = $counter;
}
}
If the following is still slow I would be surprised. I also just noticed it is similar to #Hieu Vo's answer.
// Make sure the data is returned in order by adding
// an `ORDER BY ...` clause.
$results = $mysqli->query("SELECT `OCLC Number`
FROM `MARC Records by Number`
ORDER BY `OCLC Number`");
$n_array = array();
while($row = $results->fetch_assoc()) {
// Add the "OCLC Number" as a key to the array.
$n_array[$row["OCLC Number"]] = $row["OCLC Number"];
}
// assume the first array key is in fact correct
$i = key($n_array);
// get the last key, also assume it is not missing.
end($n_array);
$max = key($n_array);
// reset the array (should not be needed)
reset($n_array);
do {
if (! $n_array[$i]) {
echo 'Missing key:['.$i.']<br />';
// flush the data to the page as you go.
flush();
}
} while(++$i <= $max);

Decrementing round number and check for results

This is a basic PHP problem and I probably could crack it in no time tomorrow morning, but today it's been a looong day, so...
I have a function that returns neighborhood name using Google Maps API. I noticed that neighborhood name is changing based on a round of the decimals in lon lat. When it cannot return neighborhood name it returns city name instead.
What I am trying to do is start with 12 decimal numbers and compare returned result to the city name and if it's the same, continue decrementing until the returned result is different.
Here's what I've got:
$rnd = 12;
function get_hood($lat, $lon, $rnd) {
$get_API = "http://maps.googleapis.com/maps/api/geocode/json?latlng=";
$get_API .= round($lat,$rnd).",";
$get_API .= round($lon,$rnd);
$jsonfile = file_get_contents($get_API.'&sensor=false');
$jsonarray = json_decode($jsonfile);
if (isset($jsonarray->results[1]->address_components[1]->long_name)) {
return($jsonarray->results[1]->address_components[1]->long_name);
}
}
for($i=1; $i<=$rnd; $i--) {
if ($b->busCity == get_hood($b->lat, $b->lon, $rnd)) {
echo get_hood($b->lat, $b->lon, $rnd);
break;
} else {
echo get_hood($b->lat, $b->lon, $rnd);
}
}
I feel like I'm going in circles and need a fresh look at this.
Thanks.
I see several problems with your for loop. First of all, you need to start your loop with the $rnd and stop at 0 or 1. Second, you don't seem to use the loop variable, but always use the same $rnd variable every time. Lastly, you use $b variable, but it's not defined anywhere (although I think it may be defined outside of the short snippet you included).
If I understand correctly what you want to get, then this code should get you there (note I had to define $b to test the code)
$b = (object)[
'lat' => 51.123456789012,
'lon' => 0.123456789012,
'busCity' => 'Withyham'
];
for($i = $rnd; $i >= 0; $i--) {
$newCity = get_hood($b->lat, $b->lon, $i);
echo "$i => $newCity\n";
if($b->busCity != $newCity) {
break;
}
}

Create a custom array in PHP

I've played around with a personal project regarding a website listing PC games by year of releasing etc. (the topic it is not so important i guess) using the following structure for the links:
http://localhost/
http://localhost/?g=15
http://localhost/?g=30
http://localhost/?g=45
Like it is seen, I've display 15 games per page. What am I working right now is displaying the above links using php in a specific manner for each page (thumbnails, links, etc.):
$arr = array("","?g=15","?g=30","?g=45","?g=60","?g=75","?g=90");
foreach ($arr as $page) {
$link = 'http://localhost/' . (string)$page;
// do stuff to each page link
}
I am very satisfied with how it goes so far but I am wondering if there is a way to create the array automatically not requiring me to manually write the string, just specify the last multiple of 15 for example. I searched the web but I haven't find something concludent or maybe I don't express myself clear enough that's why any help is more than welcomed.
echo 'http://localhost';
for ($i = 15; $i < $max; $i += 15) {
echo "http://localhost/?g=$i";
}
In practice $max is calculated by how many entries there are, which is something you usually figure out from querying a database. Hope this points you in the right direction though.
You can easily generate your array:
<?php
$arr = array('');
$max = 5;
for($i = 0; $i < $max; ++$i) {
$arr[] = '?g='.($i*15);
}
?>
Thry this and tell me if it helps
$links = array();
$links[] = 'http://localhost/';
$multiplier = 5; //the mutiplier, number of links to provide
for ($i = 1; $i < $multiplier; $i++)
{
$links[$i] = 'http://localhost/?g='.(15*$i);
// do here what you want with $links[$i]
}
print_r($links);

Dynamic X-axis graph that will change according to the count of rows

Let's say I have a mysql table with an id, some measurements and a DATE column.
Example: id, measurements, date_entered
This table stores some measurements of a patient so as to keep a record for him.
I want to make a graph which according to the count of rows that exist in the database will change dynamically the X-axis.
For example, if there are only 7 rows in the table I need to represent 7 days to the graph with the measurement for every day. If there are more than 14 days, I want it to change to respresent 2 weeks on X-axis and the average measurements(average for 1 week and average for the other too) on Y-axis and so on from weeks to months.
Can anyone help me on this? I cannot think of something that will do in my case..
I use JPGraph to make the line graph but i don't have a problem there. My problem is on how to handle the results.
I hope you will understand what I need! Thanks.
Something like this?
// Get the results from the database
$query = "SELECT `data_col` FROM `table` WHERE `condition_col` = 'some value'";
$result = mysql_query($query);
// Get all results into array and count them
$results = array();
for ($i = 0; $row = mysql_fetch_assoc($result); $i++) {
$results[] = $row;
}
// Re-format the data depending on number of results
$data = array();
if ($i < 14) { // Less than 14 days, show per day
foreach ($results as $row) {
$data[] = $row['data_col'];
}
} else if ($i < 56) { // Less than 8 weeks, show per-week
$thisweek = array();
for ($j = 0; isset($results[$j]); $j++) { // Loop the results
$thisweek[] = $results[$j]['data_col']; // Add result to this week total
if ($j % 7 == 0 && $j > 0) { // Every 7 days...
$data[] = array_sum($thisweek) / 7; // ...calculate the week average...
$thisweek = array(); // ...and reset the total
}
}
// If there is an incomplete week, add it to the data
$data[] = array_sum($thisweek) / count($thisweek);
} else { // 8 weeks or more, show per-month
$thismonth = array();
for ($j = 0; isset($results[$j]); $j++) { // Loop the results
$thismonth[] = $results[$j]['data_col']; // Add result to this month total
if ($j % 28 == 0 && $j > 0) { // Every 28 days...
$data[] = array_sum($thismonth) / 28; // ...calculate the month average...
$thismonth = array(); // ...and reset the total
}
}
// If there is an incomplete month, add it to the data
$data[] = array_sum($thismonth) / count($thismonth);
}
// $data now contains an array from which you should be able to draw your
// graph, where array keys are (sort of) x values and array values are y
// values.
Obviously, this solution assumes a 28-day month - it does not use the calendar, simply the number of days. You could do something horrible involving working out the stats based on some values returned by date() or similar, but this would likely drastically increase the calculation overhead and slow the process down.
Hopefully this will give you a place to start.

K-means clustering: What's wrong? (PHP)

I was looking for a way to calculate dynamic market values in a soccer manager game. I asked this question here and got a very good answer from Alceu Costa.
I tried to code this algorithm (90 elements, 5 clustes) but it doesn't work correctly:
In the first iteration, a high percentage of the elements changes its cluster.
From the second iteration, all elements change their cluster.
Since the algorithm normally works until convergence (no element changes its cluster), it doesn't finish in my case.
So I set the end to the 15th iteration manually. You can see that it runs infinitely.
You can see the output of my algorithm here. What's wrong with it? Can you tell me why it doesn't work correctly?
I hope you can help me. Thank you very much in advance!
Here's the code:
<?php
include 'zzserver.php';
function distance($player1, $player2) {
global $strengthMax, $maxStrengthMax, $motivationMax, $ageMax;
// $playerX = array(strength, maxStrength, motivation, age, id);
$distance = 0;
$distance += abs($player1['strength']-$player2['strength'])/$strengthMax;
$distance += abs($player1['maxStrength']-$player2['maxStrength'])/$maxStrengthMax;
$distance += abs($player1['motivation']-$player2['motivation'])/$motivationMax;
$distance += abs($player1['age']-$player2['age'])/$ageMax;
return $distance;
}
function calculateCentroids() {
global $cluster;
$clusterCentroids = array();
foreach ($cluster as $key=>$value) {
$strenthValues = array();
$maxStrenthValues = array();
$motivationValues = array();
$ageValues = array();
foreach ($value as $clusterEntries) {
$strenthValues[] = $clusterEntries['strength'];
$maxStrenthValues[] = $clusterEntries['maxStrength'];
$motivationValues[] = $clusterEntries['motivation'];
$ageValues[] = $clusterEntries['age'];
}
if (count($strenthValues) == 0) { $strenthValues[] = 0; }
if (count($maxStrenthValues) == 0) { $maxStrenthValues[] = 0; }
if (count($motivationValues) == 0) { $motivationValues[] = 0; }
if (count($ageValues) == 0) { $ageValues[] = 0; }
$clusterCentroids[$key] = array('strength'=>array_sum($strenthValues)/count($strenthValues), 'maxStrength'=>array_sum($maxStrenthValues)/count($maxStrenthValues), 'motivation'=>array_sum($motivationValues)/count($motivationValues), 'age'=>array_sum($ageValues)/count($ageValues));
}
return $clusterCentroids;
}
function assignPlayersToNearestCluster() {
global $cluster, $clusterCentroids;
$playersWhoChangedClusters = 0;
// BUILD NEW CLUSTER ARRAY WHICH ALL PLAYERS GO IN THEN START
$alte_cluster = array_keys($cluster);
$neuesClusterArray = array();
foreach ($alte_cluster as $alte_cluster_entry) {
$neuesClusterArray[$alte_cluster_entry] = array();
}
// BUILD NEW CLUSTER ARRAY WHICH ALL PLAYERS GO IN THEN END
foreach ($cluster as $oldCluster=>$clusterValues) {
// FOR EVERY SINGLE PLAYER START
foreach ($clusterValues as $player) {
// MEASURE DISTANCE TO ALL CENTROIDS START
$abstaende = array();
foreach ($clusterCentroids as $CentroidId=>$centroidValues) {
$distancePlayerCluster = distance($player, $centroidValues);
$abstaende[$CentroidId] = $distancePlayerCluster;
}
arsort($abstaende);
if ($neuesCluster = each($abstaende)) {
$neuesClusterArray[$neuesCluster['key']][] = $player; // add to new array
// player $player['id'] goes to cluster $neuesCluster['key'] since it is the nearest one
if ($neuesCluster['key'] != $oldCluster) {
$playersWhoChangedClusters++;
}
}
// MEASURE DISTANCE TO ALL CENTROIDS END
}
// FOR EVERY SINGLE PLAYER END
}
$cluster = $neuesClusterArray;
return $playersWhoChangedClusters;
}
// CREATE k CLUSTERS START
$k = 5; // Anzahl Cluster
$cluster = array();
for ($i = 0; $i < $k; $i++) {
$cluster[$i] = array();
}
// CREATE k CLUSTERS END
// PUT PLAYERS IN RANDOM CLUSTERS START
$sql1 = "SELECT ids, staerke, talent, trainingseifer, wiealt FROM ".$prefix."spieler LIMIT 0, 90";
$sql2 = mysql_abfrage($sql1);
$anzahlSpieler = mysql_num_rows($sql2);
$anzahlSpielerProCluster = $anzahlSpieler/$k;
$strengthMax = 0;
$maxStrengthMax = 0;
$motivationMax = 0;
$ageMax = 0;
$counter = 0; // for $anzahlSpielerProCluster so that all clusters get the same number of players
while ($sql3 = mysql_fetch_assoc($sql2)) {
$assignedCluster = floor($counter/$anzahlSpielerProCluster);
$cluster[$assignedCluster][] = array('strength'=>$sql3['staerke'], 'maxStrength'=>$sql3['talent'], 'motivation'=>$sql3['trainingseifer'], 'age'=>$sql3['wiealt'], 'id'=>$sql3['ids']);
if ($sql3['staerke'] > $strengthMax) { $strengthMax = $sql3['staerke']; }
if ($sql3['talent'] > $maxStrengthMax) { $maxStrengthMax = $sql3['talent']; }
if ($sql3['trainingseifer'] > $motivationMax) { $motivationMax = $sql3['trainingseifer']; }
if ($sql3['wiealt'] > $ageMax) { $ageMax = $sql3['wiealt']; }
$counter++;
}
// PUT PLAYERS IN RANDOM CLUSTERS END
$m = 1;
while ($m < 16) {
$clusterCentroids = calculateCentroids(); // calculate new centroids of the clusters
$playersWhoChangedClusters = assignPlayersToNearestCluster(); // assign each player to the nearest cluster
if ($playersWhoChangedClusters == 0) { $m = 1001; }
echo '<li>Iteration '.$m.': '.$playersWhoChangedClusters.' players have changed place</li>';
$m++;
}
print_r($cluster);
?>
It's so embarassing :D I think the whole problem is caused by only one letter:
In assignPlayersToNearestCluster() you can find arsort($abstaende);. After that, the function each() takes the first value. But it's arsort so the first value must be the highest. So it picks the cluster which has the highest distance value.
So it should be asort, of course. :) To prove that, I've tested it with asort - and I get convergence after 7 iterations. :)
Do you think that was the mistake? If it was, then my problem is solved. In that case: Sorry for annoying you with that stupid question. ;)
EDIT: disregard, I still get the same result as you, everyone winds up in cluster 4. I shall reconsider my code and try again.
I think I've realised what the problem is, k-means clustering is designed to break up differences in a set, however, because of the way you calculate averages etc. we are getting a situation where there are no large gaps in the ranges.
Might I suggest a change and only concentrate on a single value(strength appears to make most sense to me) to determine the clusters, or abandon this sorting method altogether, and adopt something different(not what you want to hear I know)?
I found a rather nice site with an example k-mean sort using integers, I'm going to try and edit that, I will get back with the results some time tomorrow.
http://code.blip.pt/2009/04/06/k-means-clustering-in-php/ <-- link I mentioned and forgot about.

Categories