/    Sign up×
Community /Pin to ProfileBookmark

php; array_unique(); returns only 10 items

I have a database table which lists several items on the same day.
I want to set up a select menu, but only to list each unique date.
When I retrieve the date variables into the array ($date) and employ the code: $unique_date = array_unique($date), it only returns 10 items.

Here is my test code, and currently I have 37 entries so I should have 37.

[code=php]<?php
// 03_create_report.php

// The array_unique() function removes duplicate values from an array.
// If two or more array values are the same, the first appearance will
// be kept and the other will be removed.

// set up select menu with unique date

// retrieve and display records where pay= 0.00

include(“config_main_content.php”);
$sql = “SELECT *FROM `absolute_report` where `pay` = 0.00 ORDER BY `id` asc”;
$result = mysql_query($sql, $conn) or die(mysql_error());

$id = array();
$control_number = array();
$date = array();
$description = array();
$urgency = array();
$pay = array();

$i = 0;

while ($newArray = mysql_fetch_array($result)) {
$id[$i] = $newArray[‘id’];
$control_number[$i] = $newArray[‘control_number’];
$date[$i] = $newArray[‘date’];
$description[$i] = $newArray[‘description’];
$urgency[$i] = $newArray[‘urgency’];
$pay[$i] = $newArray[‘pay’];

$i++;
}
?>

<?php
// set up select unique date menu
$unique_date = array();
$unique_date = array_unique($date);
?>

<p>count= <?php echo count($unique_date); ?></p>

<table>
<?php
$i=0;while($i<count($unique_date)){ ?>
<tr>
<td><?php echo $i; ?></td><td><?php echo $unique_date[$i]; ?></td>
</tr>
<?php $i++;} ?>

</table>[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 29.2016 — Well, the code apparently thinks there are only 10 [B][I][U]unique[/U][/I][/B] values among the elements in $data, and that's what array_unique() does: it just gives you one element for each [I]distinct[/I] value in the source array.
Copy linkTweet thisAlerts:
@anothenauthorMar 30.2016 — Thanks Nogdog

Well, I know it works. I must have an error in my code somewhere.

I took all the values in $date from the database and used the implode() explode() functions to test the array_unique() function and it works:

[code=php]<?php
// 03_create_report.php

// The array_unique() function removes duplicate values from an array.
// If two or more array values are the same, the first appearance will
// be kept and the other will be removed.

// set up select menu with unique date


// test string for unique date
$date_string="2016-03-03,2016-03-03,2016-03-03,2016-03-03,2016-03-03,2016-03-04,2016-03-04,2016-03-04,2016-03-04,2016-03-04,2016-03-04,2016-03-07,2016-03-07,2016-03-07,2016-03-07,2016-03-08,2016-03-09,2016-03-09,2016-03-09,2016-03-10,2016-03-10,2016-03-10,2016-03-10,2016-03-10,2016-03-11,2016-03-11,2016-03-11,2016-03-15,2016-03-15,2016-03-15,2016-03-16,2016-03-17,2016-03-17,2016-03-17,2016-03-17,2016-03-17,2016-03-21";
$date_array = explode("," , $date_string);
$date_array_unique = array_unique($date_array);
echo implode("," , $date_array_unique);
?>[/code]


this returned:

2016-03-03,2016-03-04,2016-03-07,2016-03-08,2016-03-09,2016-03-10,2016-03-11,2016-03-15,2016-03-16,2016-03-17,2016-03-21

which is what I wanted. I just need to find out why it is not working in my original code.
Copy linkTweet thisAlerts:
@anothenauthorMar 30.2016 — duhhh, I feel stupid

yeah, I just realized that the array_unique() function works fine.

I just did not count the empty values in the array.
×

Success!

Help @anothen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...