/    Sign up×
Community /Pin to ProfileBookmark

Ok, I’m tring to figure out a way to search an array for a value twice. Is there a way to do something along the lines of

[code=php]if(in_array($array_names, “john doe”)) {//I need it to see if john doe is in the array twice or not, if it is in there twice return true, else return false
echo “true”;
}else{
echo “false”;
}[/code]

Is that do able or am I searching for something hard to do?

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@theuedimasterNov 09.2005 — [code=php]
foreach($array as $num) {
if ("John" == $num) {
$num = "xx789xx";
foreach($array as $num2) {
if ("John" == $num2) {
$two_of_a_kind = true;
break 2;
}
}
}
}
foreach($array as $num3) {
if ("xx789xx"==$num3) {
$num3 = "John";
}
}
[/code]


or

[code=php]
$counter = 0;
$two_of_a_kind = false;
foreach ($array as $num) {
if ("John" == $num) $counter++;
if (2 == $counter) $two_of_a_kind = true;
}
[/code]
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYNov 09.2005 — [code=php]
<?php
$array_names = array("john doe", "max frisch", "george bush", "john doe", "marie curie", "john doe");

# counter of instances of 'john doe'
$count = 0;

foreach($array_names as $values){

if($values == "john doe"){
$count++;
}
}
echo $count;
?>
[/code]
Copy linkTweet thisAlerts:
@bokehNov 09.2005 — Returns true on duplication or false otherwise:[code=php]function duplicated($array_names, $look_for)
{
$count = 0;
foreach($array as $value){
if($value == $look_for){
$count++;
}
}
if($count > 1){
return true;
}
return false;
}[/code]
Copy linkTweet thisAlerts:
@GenixdeaeauthorNov 09.2005 — Awsome thanks guys, appriciate it ?
Copy linkTweet thisAlerts:
@theuedimasterNov 09.2005 — Overkill. I like it. ?
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYNov 09.2005 — bwah, that was like 3 answers in 3 minutes, hehe ?
Copy linkTweet thisAlerts:
@NogDogNov 09.2005 — Part of the never-ending quest to discover built-in functions I've never used before:
[code=php]
$counts = array_count_values($array_names);
if(!empty($counts["john doe"]) and $counts["john doe"] > 1)
{
# blah blah blah
}
[/code]
Copy linkTweet thisAlerts:
@ShrineDesignsNov 10.2005 — case-insensitive[code=php]function array_value_occurs($array, $search_value)
{
$i = 0;

while(list(, $v) = each($array))
{
$i+= preg_match("/" .preg_quote($search_value). "/i", $v);
}
return $i;
}[/code]
recursive[code=php]function array_value_occurs($array, $search_value)
{
$i = 0;

while(list(, $v) = each($array))
{
if(is_array($v))
{
$i+= array_value_occurs($v, $search_value);
}
else
{
$i += preg_match("/" .preg_quote($search_value). "/i", $v);
}
}
return $i;
}[/code]
Copy linkTweet thisAlerts:
@NogDogNov 10.2005 — Nice, terse one-liner: ?
[code=php]
if(count(array_keys($array_names, "john doe")) > 1)
{
# found more than 1
}
[/code]
Copy linkTweet thisAlerts:
@ShrineDesignsNov 10.2005 — nice one

i think Genixdeae was looking for the values not keys lol
Copy linkTweet thisAlerts:
@NogDogNov 10.2005 — nice one

i think Genixdeae was looking for the values not keys lol[/QUOTE]

From http://www.php.net/array_keys :

array array_keys ( array input [, mixed search_value [, bool strict]] )

array_keys() returns the keys, numeric and string, from the input array.

[i]If the optional search_value is specified, then only the keys for that value are returned.[/i] [My italics]
Copy linkTweet thisAlerts:
@GenixdeaeauthorNov 10.2005 — You're very much right Nogdog, thanks ?
Copy linkTweet thisAlerts:
@ShrineDesignsNov 10.2005 — i must have being thinking of array_key_exists(), my mistake, lol
×

Success!

Help @Genixdeae 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.24,
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,
)...