Home  >  Article  >  Backend Development  >  How to count the number of elements in a two-dimensional array in PHP_PHP Tutorial

How to count the number of elements in a two-dimensional array in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:22931browse

Solution
1. First read the data from the content field of the database and merge them into a string.

Copy code The code is as follows:

while($myrow = $connector - > fetch_array($result))
{
//$r[] = explode(",", $myrow["content"]);
$str .= $myrow["content" ].',';
}

$arr_str = substr($str, 0, -1);
?>

Because after the last number There are commas, so the string needs to be intercepted.
2. Split the string into an array by commas.
Copy code The code is as follows:

$r = explode(",", $arr_str);

3. Use array_count_values() to count the number of elements of a one-dimensional array
Since array_count_values() seems to be unable to directly count the elements of a two-dimensional array, so proceed After completing the above two steps, a one-dimensional array is obtained.
The array_count_values() function is used to count the number of occurrences of all values ​​in the array. Returns an array, the key name of its element is the value of the original array, and the key value is the number of times the value appears in the original array.
Copy code The code is as follows:

$rs = array_count_values($r);

4. Sort
Copy code The code is as follows:

asort($rs);
echo '
';<br>print_r($rs);<br>echo '
';

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825107.htmlTechArticleSolution 1. First read the data from the congtent field of the database and merge them into a string. Copy the code The code is as follows: ?php while($myrow = $connector - fetch_array($r...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn