PHP Array Count Occurences


 

PHP Array Count Occurences

PHP Array Count Occurrences: In this tutorial we will come to know about how search a string or any value within an array. This tutorial covers up array_search() function with its general format, parameters, return types etc. In addition it has several examples to make it easy to understand.

PHP Array Count Occurrences: In this tutorial we will come to know about how search a string or any value within an array. This tutorial covers up array_search() function with its general format, parameters, return types etc. In addition it has several examples to make it easy to understand.

Array Search

In PHP, array_search() function performs a search  for a value in an array, as with in_array(). If the value is found then the index position of that key (which could be string or number) will be returned.

General Format mixed array_search ( mixed $search , array $array [, bool $strict ] )
Parameters $search: The value to be searched $array: The array in which the value to be searched $strict: If the value is set to true then the types of $search and $array will be checked
Return Value Return the key for $search if it is found in $array, otherwise false

 

Example 1:

<?php

$array=array( "new", "Delhi", "common", "wealth", "game");

echo"<br/><b>Initially the values of \$array is:</b><br/>";

var_dump($array);

$key='common';

echo"<br/><b>Now we will search the word \"$key\" in \$array</b><br/>";

$search=array_search($key,$array);

echo"<br/><b>$key is found at $search</b><br/>";

?>

Output:

Initially the values of $array is:
array(5) {
  [0]=>
  string(3) "new"
  [1]=>
  string(5) "Delhi"
  [2]=>
  string(6) "common"
  [3]=>
  string(6) "wealth"
  [4]=>
  string(4) "game"
}

Now we will search the word "common" in $array

common is found at 2

Example 2:

<?php

$array=array(10=>"new",11=>"delhi",12=>"common",13=>"wealth",14=>"game");

echo"<br/><b>Initially the values of \$array is:</b><br/>";

var_dump($array);

$key='common';

echo"<br/><b>Now we will search the word \"$key\" in \$array</b><br/>";

$search=array_search($key,$array);

echo"<br/><b>$key is found at $search</b><br/>";

?>

Output:

Initially the values of $array is:
array(5) {
  [10]=>
  string(3) "new"
  [11]=>
  string(5) "delhi"
  [12]=>
  string(6) "common"
  [13]=>
  string(6) "wealth"
  [14]=>
  string(4) "game"
}

Now we will search the word "common" in $array

common is found at 12

Example 3:

<?php

$array=array("new"=>11,"delhi"=>12,"common" => 13,"wealth"=>14,"game"=>15);

echo"<br/><b>Initially the values of \$array is:</b><br/>";

var_dump($array);

0

$key=13;

echo"<br/><b>Now we will search $key in \$array</b><br/>";

$search=array_search($key,$array);

1

echo"<br/><b>$key is found at $search</b><br/>";

?>

Output:

2
Initially the values of $array is:
array(5) {
  ["new"]=>
  int(11)
  ["delhi"]=>
  int(12)
  ["common"]=>
  int(13)
  ["wealth"]=>
  int(14)
  ["game"]=>
  int(15)
}

Now we will search 13 in $array

13 is found at common


Example 4:

<?php

$array=array("new"=>11,"delhi"=>12,"common","wealth"=>14,"game"=>15);

3

echo"<br/><b>Initially the values of \$array is:</b><br/>";

var_dump($array);

$key="common";

4

echo"<br/><b>Now we will search \"$key\" in \$array</b><br/>";

$search=array_search($key,$array);

echo"<br/><b>$key is found at $search</b><br/>";

5

?>

Output:

Initially the values of $array is:
array(5) {
  ["new"]=>
  int(11)
  ["delhi"]=>
  int(12)
  [0]=>
  string(6) "common"
  ["wealth"]=>
  int(14)
  ["game"]=>
  int(15)
}

Now we will search "common" in $array

common is found at 0

Example 5:

6

<?php

$array=array("new"=>11,"delhi"=>12,13,"wealth"=>14,"game"=>15);

echo"<br/><b>Initially the values of \$array is:</b><br/>";

7

var_dump($array);

$key=13;

echo"<br/><b>Now we will search $key in \$array</b><br/>";

8

$search=array_search($key,$array);

echo"<br/><b>$key is found at $search</b><br/>";

?>

9

Output:

Initially the values of $array is:
array(5) {
  ["new"]=>
  int(11)
  ["delhi"]=>
  int(12)
  [0]=>
  int(13)
  ["wealth"]=>
  int(14)
  ["game"]=>
  int(15)
}

Now we will search 13 in $array

13 is found at 0

Example 6 (If we search a value which occurs more than one time):

<?php

0

$array=array("new","delhi","new","wealth","game");

echo"<br/><b>Initially the values of \$array is:</b><br/>";

var_dump($array);

1

$key="new";

echo"<br/><b>Now we will search \"$key\" in \$array</b><br/>";

$search=array_search($key,$array);

2

echo"<br/><b>$key is found at $search</b><br/>";

?>

Output:

3
Initially the values of $array is:
array(5) {
  [0]=>
  string(3) "new"
  [1]=>
  string(5) "delhi"
  [2]=>
  string(3) "new"
  [3]=>
  string(6) "wealth"
  [4]=>
  string(4) "game"
}

Now we will search "new" in $array

new is found at 0

Ads