Write a PHP code to:

1- Create a 2-dimensional array, with a random number of rows and columns.

2- Fill the array with random numbers.

3- Send the array to a function that returns the number of the highest frequency in the array.

Who can help me to solve it?

That has a distinctive smell of a homework assignment about it. People here won't just throw the answer out to such things, not least as that wouldn't really help you in any meaningful way. So, how about you show us the code you have so far and where you are getting stuck with it? Then people will be able to point you in the right direction...

Quite HG.

A good place to start: google "array functions php".

This should help you get started. Be aware that this is a trick question - there may be ties at the highest frequency, and the function will only return one value, so an equally deserving value will be lost. You might want to try discerning that condition and returning a collection. Also, you need to constrain the random values as I have shown here. Without that, the size of the arrays could exceed the memory available to PHP. Good luck with your project, Ray

<?php // demo/temp_abdallah.php
/**
 * https://www.daniweb.com/programming/web-development/threads/519077/multidimensional-array
 *
 * Write a PHP code to:
 *
 * 1- Create a 2-dimensional array, with a random number of rows and columns.
 *
 * 2- Fill the array with random numbers.
 *
 * 3- Send the array to a function that returns the number of the highest frequency in the array.
 *
 * Who can help me to solve it?
 */
error_reporting(E_ALL);

// http://php.net/manual/en/function.array-count-values.php
function two_d_counts(array $arr) {
    $counts = [];
    foreach ($arr as $row) {
        foreach ($row as $col) {
            if (empty($counts[$col])) $counts[$col] = 0;
            $counts[$col]++;
        }
    }
    // http://php.net/manual/en/array.sorting.php
    asort($counts);

    // http://php.net/manual/en/function.end.php
    end($counts);

    // http://php.net/manual/en/function.key.php
    $key = key($counts);
    $val = $counts[$key];
    return array($key => $val);
}

// http://php.net/manual/en/function.rand.php
$rows = rand(5, 10);
$cols = rand(5, 10);

// http://php.net/manual/en/book.array.php
$r = 0;
while ($r <= $rows) {
    $c = 0;
    while ($c <= $cols) {
        $arr[$r][$c] = rand(3,8);
        $c++;
    }
    $r++;
}

$high_frequency = two_d_counts($arr);

$keys = array_keys($high_frequency);
$popular = array_pop($keys);
$howmany = array_pop($high_frequency);
echo PHP_EOL . "Number $popular occurred $howmany times";

@Ray
thank you very much really thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.