Display which row and which column has the biggest number of 1's


I am new to PHP and I have this homework due Sunday, I got it on Friday btw, this Friday :slight_smile: I have complete the first part of it , but I can not complete the second part of it. This is the exercise:

Largest Row and Column

Write a 4x4 array and fill it with random 1’s or 0’s

0 0 1 1

0 1 0 0

1 0 1 1

1 0 0 1

Display which row and which column has the biggest number of 1’s

this is my code:

<?php $num = array("0", "1"); $rows = rand(4, 4); //using rand() function to generate 4 different numbers, min & max=4 $cols = rand(4, 4); echo ""; for($i = 1; $i <= $rows; $i++) { echo ""; for($j = 1; $j <= $cols; $j++) { $td_text = $num[rand(0,(count($num)-1))]; //number spacing echo ''; } echo ""; } I have an idea : to store random numbers of the array in new array than to count 1's and then to display which row/col have the biggest number of 1's and then echo the result. I googled and I could not find a function or method to store random numbers into an array. Could you please help me I am confused .

You didnt format your code… so its truncated or something…

Please post again.

There are many ways to get your total ‘counts’ here… are there are restrictions? or ways you were told to approach this?

Do you need to use any specific functions or approaches?

You could implode and do a string count in a loop?

That task sounds pretty random, like there’s a bigger problem, what’s the actual use case? Anyway, if you combine these numbers into one string per row as mentioned, you could use the substr_count function.

OP was already given complete working code on another forum.

Thank you for your replies . I am sorry that I have written bad the code.I solve it :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service