I have an array in which I need to count duplicate values.
Here's the original array...
array(
[0] => 23.07.2014
[1] => 23.07.2014
[2] => 23.07.2014
[3] => 22.07.2014
[4] => 22.07.2014
[5] => 21.07.2014
[6] => 21.07.2014
[7] => 21.07.2014
[8] => 20.07.2014
[9] => 20.07.2014
[10] => 19.07.2014
[11] => 19.07.2014
[12] => 18.07.2014
[13] => 18.07.2014
[14] => 18.07.2014
[15] => 17.07.2014
[16] => 17.07.2014
[17] => 17.07.2014
[18] => 16.07.2014
[19] => 15.07.2014
[20] => 14.07.2014
[21] => 13.07.2014
[22] => 13.07.2014
[23] => 12.07.2014
[24] => 12.07.2014
[25] => 11.07.2014
[26] => 11.07.2014
[27] => 10.07.2014
[28] => 09.07.2014
[29] => 09.07.2014
[30] => 08.07.2014
)
and I want to get a count of every value in the array, for example:
23.07.2014 Count=3
24.07.2014 Count=2
25 07.2014 Count=1
29.07.2014 Count=1
28.07.2014 Count=1
Use this :
print_r(array_count_values($array));
Try this...
$vals = array_count_values($array);
$key=array_keys($vals);
$value=array_values($vals);
$count=count($vals);
for($i=0;$i<$count;$i++)
{
echo $key[$i]."=>".$value[$i]."</br>";
}
Something likethis should work: (not tested)
unset($tempArr);
foreach ($arr as $elem => $content) {
if(isset($tempArr[$content]) $tempArr[$content]++;
else $tempArr[$content] = 0;
}
echo '<pre>'.print($tempArr).'</pre>';
You can try this by diffrent count of existing array count and unique array count...
`$duplicates = count($currentArray)-count(array_unique($currentArray));
echo $duplicates;
Related
First of all, I am using this piece of code in PHP to retrieve data from a database.
DATABASE DATA CODE:
// Database Details
$servername = "localhost";
$username = "admin_testdb";
$password = "123412345";
$dbname = "test_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM `HOURLY_DATA` WHERE 1 ORDER BY `HOURLY_DATA`.`TIME` ASC";
// Run $sql content as sql script in db.
$result=$conn->query($sql);
This code is just fine and has absolutely no errors or problems related to my goal here.
Also the specific data table I am looking after in this Query is the table named TIME which is of type "Datetime" format in SQL which looks like this:
DATETIME FORMAT:
YYYY-MM-DDTHH:MM:SS or to make it more clear an example would be 1970-09-24T05:00:00
Now moving onto what I am trying to achieve, my code and then the problem.
The Goal:
I have a list of data in the datetime format explained above, where theres HOURS missing, the increment of the HOURS is always +1 meaning that an example of missing data inside this data table would be:
2021-11-01T00:00:00
2021-11-01T01:00:00
2021-11-01T02:00:00
2021-11-01T05:00:00
2021-11-01T06:00:00
2021-11-01T07:00:00
2021-11-01T08:00:00
2021-11-01T13:00:00
2021-11-01T14:00:00
2021-11-01T15:00:00
2021-11-01T16:00:00
2021-11-01T17:00:00
2021-11-01T18:00:00
2021-11-01T19:00:00
2021-11-01T20:00:00
2021-11-01T21:00:00
So by taking a look at this DATETIME list above, we can understand that there is data missing and more specifically hours missing like 22:00:00 or 23:00:00 etc...
Now I want to write a code that will find this missing HOUR steps.
So far I have tried this code:
// Run $sql content as sql script in db.
$result=$conn->query($sql);
$flag = 0;
$i = 0;
$x = 0;
$cont = array();
$temp = array();
foreach ($result as $value) {
$singular_value = $value["TIME"];
$singular_value = str_replace(" ", "", $singular_value);
$day_value = $singular_value[8].$singular_value[9];
$day_value = (int) $day_value;
$singular_value = $singular_value[11].$singular_value[12];
$singular_value = (int) $singular_value;
// First time running.
if ( $flag == 0 ){
$day_chair = $day_value;
$flag = 1;
}
if ( $day_chair == $day_value ){
$temp[]= $value["TIME"];
$cont[$i]= $temp;
$day_chair = $day_value;
} else {
if ( $day_value == 30 || $day_value == 31 ){
$day_chair = 1;
continue;
} else {
$day_chair += 1;
$temp = array();
}
$i++;
}
}
print("<pre>".print_r($cont,true)."</pre>");
*PROBLEM:
To begin with this code returns the hour 00 which is considered to be the 12 o'clock midnight only for the first iteration or day.
Also the code above is supposed to return and array that will look like this:
Array
(
[0] => Array
(
[0] => 2021-11-01T00:00:00
[1] => 2021-11-01T01:00:00
[2] => 2021-11-01T02:00:00
[3] => 2021-11-01T03:00:00
[4] => 2021-11-01T04:00:00
[5] => 2021-11-01T05:00:00
[6] => 2021-11-01T06:00:00
[7] => 2021-11-01T07:00:00
[8] => 2021-11-01T08:00:00
[9] => 2021-11-01T09:00:00
[10] => 2021-11-01T10:00:00
[11] => 2021-11-01T11:00:00
[12] => 2021-11-01T12:00:00
[13] => 2021-11-01T13:00:00
[14] => 2021-11-01T14:00:00
[15] => 2021-11-01T15:00:00
[16] => 2021-11-01T16:00:00
[17] => 2021-11-01T17:00:00
[18] => 2021-11-01T18:00:00
[19] => 2021-11-01T19:00:00
[20] => 2021-11-01T20:00:00
[21] => 2021-11-01T21:00:00
[22] => 2021-11-01T22:00:00
[23] => 2021-11-01T23:00:00
)
[1] => Array
(
[0] => 2021-11-02T00:00:00
[1] => 2021-11-02T01:00:00
[2] => 2021-11-02T02:00:00
[3] => 2021-11-02T03:00:00
[4] => 2021-11-02T04:00:00
[5] => 2021-11-02T05:00:00
[6] => 2021-11-02T06:00:00
[7] => 2021-11-02T07:00:00
[8] => 2021-11-02T08:00:00
[9] => 2021-11-02T09:00:00
[10] => 2021-11-02T10:00:00
[11] => 2021-11-02T11:00:00
[12] => 2021-11-02T12:00:00
[13] => 2021-11-02T13:00:00
[14] => 2021-11-02T14:00:00
[15] => 2021-11-02T15:00:00
[16] => 2021-11-02T16:00:00
[17] => 2021-11-02T17:00:00
[18] => 2021-11-02T18:00:00
[19] => 2021-11-02T19:00:00
[20] => 2021-11-02T20:00:00
[21] => 2021-11-02T21:00:00
[22] => 2021-11-02T22:00:00
[23] => 2021-11-02T23:00:00
)
[2] => Array
(
[0] => 2021-11-03T00:00:00
[1] => 2021-11-03T01:00:00
[2] => 2021-11-03T02:00:00
[3] => 2021-11-03T03:00:00
[4] => 2021-11-03T04:00:00
[5] => 2021-11-03T05:00:00
[6] => 2021-11-03T06:00:00
[7] => 2021-11-03T07:00:00
[8] => 2021-11-03T08:00:00
[9] => 2021-11-03T09:00:00
[10] => 2021-11-03T10:00:00
[11] => 2021-11-03T11:00:00
[12] => 2021-11-03T12:00:00
[13] => 2021-11-03T13:00:00
[14] => 2021-11-03T14:00:00
[15] => 2021-11-03T15:00:00
[16] => 2021-11-03T16:00:00
[17] => 2021-11-03T17:00:00
[18] => 2021-11-03T18:00:00
[19] => 2021-11-03T19:00:00
[20] => 2021-11-03T20:00:00
[21] => 2021-11-03T21:00:00
[22] => 2021-11-03T22:00:00
[23] => 2021-11-03T23:00:00
)
)
And will leave a GAP or simply do write nothing in the array if the date is missing from the database. But this code return a lot of problematic arrays and just to give an example there are some here:
First day was parsed fine but the second one and all the days after the first day are missing the 12 o'clock aka 00:00:00
E.X:
[0] => Array
(
[0] => 2021-11-01T00:00:00
[1] => 2021-11-01T01:00:00
[2] => 2021-11-01T02:00:00
[3] => 2021-11-01T03:00:00
[4] => 2021-11-01T04:00:00
[5] => 2021-11-01T05:00:00
[6] => 2021-11-01T06:00:00
[7] => 2021-11-01T07:00:00
[8] => 2021-11-01T08:00:00
[9] => 2021-11-01T09:00:00
[10] => 2021-11-01T10:00:00
[11] => 2021-11-01T11:00:00
[12] => 2021-11-01T12:00:00
[13] => 2021-11-01T13:00:00
[14] => 2021-11-01T14:00:00
[15] => 2021-11-01T15:00:00
[16] => 2021-11-01T16:00:00
[17] => 2021-11-01T17:00:00
[18] => 2021-11-01T18:00:00
[19] => 2021-11-01T19:00:00
[20] => 2021-11-01T20:00:00
[21] => 2021-11-01T21:00:00
[22] => 2021-11-01T22:00:00
[23] => 2021-11-01T23:00:00
)
[1] => Array
(
[0] => 2021-11-02T01:00:00
[1] => 2021-11-02T02:00:00
[2] => 2021-11-02T03:00:00
[3] => 2021-11-02T04:00:00
[4] => 2021-11-02T05:00:00
[5] => 2021-11-02T06:00:00
[6] => 2021-11-02T07:00:00
[7] => 2021-11-02T08:00:00
[8] => 2021-11-02T09:00:00
[9] => 2021-11-02T10:00:00
[10] => 2021-11-02T11:00:00
[11] => 2021-11-02T12:00:00
[12] => 2021-11-02T13:00:00
[13] => 2021-11-02T14:00:00
[14] => 2021-11-02T15:00:00
[15] => 2021-11-02T16:00:00
[16] => 2021-11-02T17:00:00
[17] => 2021-11-02T18:00:00
[18] => 2021-11-02T19:00:00
[19] => 2021-11-02T20:00:00
[20] => 2021-11-02T21:00:00
[21] => 2021-11-02T22:00:00
[22] => 2021-11-02T23:00:00
)
Also, there are broken days for example when the day reaches day 30 or 31 of the month this happens:
[28] => Array
(
[0] => 2021-11-29T01:00:00
[1] => 2021-11-29T02:00:00
[2] => 2021-11-29T03:00:00
[3] => 2021-11-29T04:00:00
[4] => 2021-11-29T05:00:00
[5] => 2021-11-29T06:00:00
[6] => 2021-11-29T07:00:00
[7] => 2021-11-29T08:00:00
[8] => 2021-11-29T09:00:00
[9] => 2021-11-29T10:00:00
[10] => 2021-11-29T11:00:00
[11] => 2021-11-29T12:00:00
[12] => 2021-11-29T13:00:00
[13] => 2021-11-29T14:00:00
[14] => 2021-11-29T15:00:00
[15] => 2021-11-29T16:00:00
[16] => 2021-11-29T17:00:00
[17] => 2021-11-29T18:00:00
[18] => 2021-11-29T19:00:00
[19] => 2021-11-29T20:00:00
[20] => 2021-11-29T21:00:00
[21] => 2021-11-29T22:00:00
[22] => 2021-11-29T23:00:00
[23] => 2021-12-01T00:00:00
[24] => 2021-12-01T01:00:00
[25] => 2021-12-01T02:00:00
[26] => 2021-12-01T03:00:00
[27] => 2021-12-01T04:00:00
[28] => 2021-12-01T05:00:00
[29] => 2021-12-01T06:00:00
[30] => 2021-12-01T07:00:00
[31] => 2021-12-01T08:00:00
[32] => 2021-12-01T09:00:00
[33] => 2021-12-01T10:00:00
[34] => 2021-12-01T11:00:00
[35] => 2021-12-01T12:00:00
[36] => 2021-12-01T13:00:00
[37] => 2021-12-01T14:00:00
[38] => 2021-12-01T15:00:00
[39] => 2021-12-01T16:00:00
[40] => 2021-12-01T17:00:00
[41] => 2021-12-01T18:00:00
[42] => 2021-12-01T19:00:00
[43] => 2021-12-01T20:00:00
[44] => 2021-12-01T21:00:00
[45] => 2021-12-01T22:00:00
[46] => 2021-12-01T23:00:00
)
[29] => Array
(
[0] => 2021-12-02T01:00:00
[1] => 2021-12-02T02:00:00
[2] => 2021-12-02T03:00:00
[3] => 2021-12-02T04:00:00
[4] => 2021-12-02T05:00:00
[5] => 2021-12-02T06:00:00
[6] => 2021-12-02T07:00:00
[7] => 2021-12-02T08:00:00
[8] => 2021-12-02T09:00:00
[9] => 2021-12-02T10:00:00
[10] => 2021-12-02T11:00:00
[11] => 2021-12-02T12:00:00
[12] => 2021-12-02T13:00:00
[13] => 2021-12-02T14:00:00
[14] => 2021-12-02T15:00:00
[15] => 2021-12-02T16:00:00
[16] => 2021-12-02T17:00:00
[17] => 2021-12-02T18:00:00
[18] => 2021-12-02T19:00:00
[19] => 2021-12-02T20:00:00
[20] => 2021-12-02T21:00:00
[21] => 2021-12-02T22:00:00
[22] => 2021-12-02T23:00:00
)
QUESTION:
What can I do to solve this programmatic/algorithmic problem or what am I doing wrong in my code?
I have a query the returns columns from the INFORMATION_SCHEMA.
I then create an array, use the array in a foreach loop to create another array.
The issue I have is the final array starts at a position 0, producing column SeqID0 but the first column is SeqID1:
Array ( [0] => SeqID0 [1] => SeqID1 [2] => SeqID2 [3] => SeqID3 [4] => SeqID4 [5] => SeqID5 [6] => SeqID6 [7] => SeqID7 [8] => SeqID8 [9] => SeqID9 [10] => SeqID10 [11] => SeqID11 [12] => SeqID12 [13] => SeqID13 [14] => SeqID14 [15] => SeqID15 [16] => SeqID16 [17] => SeqID17 [18] => SeqID18 [19] => SeqID19 [20] => SeqID20 [21] => SeqID21 [22] => SeqID22 [23] => SeqID23 [24] => SeqID24 [25] => SeqID25 [26] => SeqID26 [27] => SeqID27 [28] => SeqID28 [29] => SeqID29 [30] => SeqID30 [31] => SeqID31 [32] => SeqID32 [33] => SeqID33 [34] => SeqID34 [35] => SeqID35 [36] => SeqID36 [37] => SeqID37 [38] => SeqID38 [39] => SeqID39 [40] => SeqID40 [41] => SeqID41 [42] => SeqID42 [43] => SeqID43 [44] => SeqID44 [45] => SeqID45 [46] => SeqID46 )
How can I make the array start at SeqID1.
This is my code;
$strSQLCol = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME= 'NXLHR_Active' AND COLUMN_NAME LIKE 'SeqID%'";
$rsCol = mysqli_query($link,$strSQLCol);
$SeqList = array();
do {
$SeqList[] = $row;
} while($row = $rsCol->fetch_assoc());
$SeqIDArray = array();
foreach($SeqList as $key => $value) {
$SeqIDArray[] = 'SeqID' . $key;
}
Many thanks in advance for your time.
Here your improved code. Notice I have added a conditional in foreach:
foreach($SeqList as $key => $value) {
if (1 > $key) { continue; }
$SeqIDArray[] = 'SeqID' . $key;
}
I am trying to get my image slides to display in order by number with the following:
$image=array();
$img_folder = $params->get('path');
mt_srand((double)microtime()*1000);
if(is_dir($img_folder)){
krsort($file);
$imgs = dir($img_folder);
while ($file = $imgs->read()) {
if ((eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file)))
$image[] = "$file";
} closedir($imgs->handle);
print_r($image);
return $image;
}
However, the array works but the order is off:
Array ( [0] => Slide1.png [1] => Slide10.png [2] => Slide11.png [3] => Slide12.png [4] => Slide13.png [5] => Slide14.png [6] => Slide15.png [7] => Slide16.png [8] => Slide17.png [9] => Slide18.png [10] => Slide19.png [11] => Slide2.png [12] => Slide20.png [13] => Slide21.png [14] => Slide22.png [15] => Slide23.png [16] => Slide24.png [17] => Slide25.png [18] => Slide26.png [19] => Slide27.png [20] => Slide28.png [21] => Slide29.png [22] => Slide3.png [23] => Slide30.png [24] => Slide31.png [25] => Slide4.png [26] => Slide5.png [27] => Slide6.png [28] => Slide7.png [29] => Slide8.png [30] => Slide9.png )
If I put natsort($image); after $image[] = "$file"; the order is correct but the array is off:
Array ( [0] => Slide1.png [11] => Slide2.png [22] => Slide3.png [25] => Slide4.png [26] => Slide5.png [27] => Slide6.png [28] => Slide7.png [29] => Slide8.png [30] => Slide9.png [1] => Slide10.png [2] => Slide11.png [3] => Slide12.png [4] => Slide13.png [5] => Slide14.png [6] => Slide15.png [7] => Slide16.png [8] => Slide17.png [9] => Slide18.png [10] => Slide19.png [12] => Slide20.png [13] => Slide21.png [14] => Slide22.png [15] => Slide23.png [16] => Slide24.png [17] => Slide25.png [18] => Slide26.png [19] => Slide27.png [20] => Slide28.png [21] => Slide29.png [23] => Slide30.png [24] => Slide31.png )
I need them to order Slide1 - Slide30, not like listed above. I could change the names from numeric (Slide1) to alphabetic (SlideA) but would like to keep this naming convention. Is there a way to do this without changing the image names?
This image slide is a "how to" slide show.
NatSort is what you are looking for.
just use natsort($image);
You can reference it at:
http://php.net/manual/es/function.natsort.php
I didn't know about natsort(), this should do it too though..
$imgs = scandir(dir($img_folder));
$sorted = array();
foreach($imgs as $img){
$index = intval(trim($img, "Slide.png"));
$sorted[$index] = $img;
}
ksort($sorted);
$sorted = array_values($sorted);
You can use natsort() function
natsort
e.g.
$array1 = array("img12.png", "img10.png", "img2.png", "img1.png");
natsort($array);
print_r($array);
This way you will get out put is :
Array ( [3] => img1.png [2] => img2.png [1] => img10.png [0] => img12.png )
You can also use natcasesort($image). It sort an array using a case insensitive "natural order" algorithm.
This did the trick!!!
Just after
$image[] = "$file";
I added
sort($image, SORT_NATURAL | SORT_FLAG_CASE);
This correctly orders the images in the array and the order:
Array ( [0] => Slide1.png [1] => Slide2.png [2] => Slide3.png [3] => Slide4.png [4] => Slide5.png [5] => Slide6.png [6] => Slide7.png [7] => Slide8.png [8] => Slide9.png [9] => Slide10.png [10] => Slide11.png [11] => Slide12.png [12] => Slide13.png [13] => Slide14.png [14] => Slide15.png [15] => Slide16.png [16] => Slide17.png [17] => Slide18.png [18] => Slide19.png [19] => Slide20.png [20] => Slide21.png [21] => Slide22.png [22] => Slide23.png [23] => Slide24.png [24] => Slide25.png [25] => Slide26.png [26] => Slide27.png [27] => Slide28.png [28] => Slide29.png [29] => Slide30.png [30] => Slide31.png )
I have a array called $array which is multidimensional array. It consits of keys like brandname, productsubcategory (Keys are not static they can be anything and even number of keys are not fixed). I want all the common values from the array
Current array:
$array = array
(
[brandname] => Array
(
[0] => Array
(
[0] => sony_brandname
[1] => touch screen_type
[3] => 2.8_size
[4] => gsm + gsm_sim_support
[5] => 2_primary_camera
[6] => 64 mb_internal_memory
[7] => 32_expandable_memory
[8] => 1200_standard_battery_type
[9] => 3_size
[10] => 1000_standard_battery_type
[11] => touch and type_type
)
)
[productsubcategory] => Array
(
[1] => Array
(
[0] => karbonn_brandname
[1] => touch and type_type
[2] => micromax_brandname
[3] => 2.8_size
[4] => gsm_sim_support
[5] => 3.15_primary_camera
[6] => 52 mb_internal_memory
[7] => 8_expandable_memory
[8] => 1000_standard_battery_type
[9] => nokia_brandname
[10] => symbian s40_os_clean
[11] => 5_primary_camera
[12] => 128 mb_ram
[13] => 256 mb_internal_memory
[14] => 32_expandable_memory
[15] => 1110_standard_battery_type
[16] => gsm + gsm_sim_support
[17] => 2_primary_camera
[18] => 32 mb_ram
[19] => 10 mb_internal_memory
[20] => no_expandable_memory
[21] => 1020_standard_battery_type
[22] => 680 mhz_processor
[23] => 64 mb_ram
[24] => 128 mb_internal_memory
[25] => 860_standard_battery_type
[26] => blackberry_brandname
[27] => 2.45_size
[28] => 1 ghz_processor
)
)
);
Desired array :
$array = array
(
[0] => sony_brandname
[1] => touch and type_type
[2] => 2.8_size
[8] => 1000_standard_battery_type
[16] => gsm + gsm_sim_support
[17] => 2_primary_camera
)
Use array_intersect():
$result = array_intersect($array["brandname"][0], $array["productsubcategory"][1]);
print_r($result);
This is my array:
[abominado] => Array
(
[0] => réprobo
[1] => réprobo
[2] => abominado
[3] => banido
[4] => condenado
[5] => detestado
[6] => odiado
[7] => precito
[8] => renegado
[9] => repudiado
)
[abominar] => Array
(
[0] => repelir
[1] => repelir
[2] => abominar
[3] => afastar
[4] => afugentar
[5] => arredar
[6] => desalojar
[7] => desviar
[8] => detestar
[9] => empuxar
[10] => escorraçar
[11] => espinafrar
[12] => execrar
[13] => exercer
[14] => expulsar
[15] => grimpar
[16] => impugnar
[17] => odiar
[18] => rebater
[19] => rechaçar
[20] => recusar
[21] => rejeitar
[22] => relegar
[23] => repudiar
)
how can I insert it into sql ?
foreach ($abominado as $key=>$str)
{
$string .= "$key:$str\n";
}
mysql_query("INSERT INTO strings VALUES ('".mysql_real_escape_string($string)."')");
You can use serialize to cast it to string and unserialize when you get it
First, these arrays happen to be sequential.
Second, in a SQL table, any column can be used as a search criteria, so just insert the data directly and set up an index on any searchable column.