Displaying unequal amount of data? - php

I have this small problem although it's small i can't seem to work it out, I've set of data i need to display, lets say 1 to 17. i need to display 3 in a row like 1,2,3 in one row and 4,5,6 in the next because bootstrap row support 12 columns and there are 3 elements of 4 columns each.
Because the amount of data can vary and the total number of data won't divide by 3 like the example it's 17 how can I write something in PHP that will display the data 3 in a row and like in this example there will be 5 rows of 3 and a last row having 2 sets.
Thanks
Edit:
I didn't write any code of this but was thinking a loop and a nested loop but think that's too clunky any better way of doing this?

You can use following code :
for($i = 1;$i<=17;$i++){
if($i%3 !=1 && $i%3 != 0){
print_r($i." , ");
}else if( $i%3 == 0){
print_r($i);
}
else{
print_r("<br/>".$i." , ");
}
}
It'll give you output like this:
1 , 2 , 3
4 , 5 , 6
7 , 8 , 9
10 , 11 , 12
13 , 14 , 15
16 , 17 ,

Rukshan you can use the modulus operator.
You can use the code below as an example. I have mixed html and php, but it is just to show you an example:--
<?php
echo "<table>";
for($i=1;$i<18;$i++)
{
echo "<tr>";
echo "<td>".$i."</td>";
if($i%3 == 0) echo "</tr>";
}
echo "</table>";
?>

Try using array_slice() to slice your array as per your need. You will get your division in arrays. Loop through them to create your table.
Reference Example
$stores = array(1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12);
$division = ceil( count($stores) / 3 ); //to divide array into 3 halves
$firstHalf = array_slice($stores, 0, $division);
$secondHalf = array_slice($stores, $division, $division);
$thirdHalf = array_slice($stores, $division * 2);
Output for $stores = array(1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)
Array
(
[0] => 10
[1] => 11
[2] => 12
)
Output for $stores = array(1, 2, 3, 4, 5, 6, 7, 8, 10)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)
Array
(
[0] => 9
[1] => 10
)
Output for $stores = array(1, 2, 3, 4, 5, 6, 7, 8);
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => 4
[1] => 5
[2] => 6
)
Array
(
[0] => 7
[1] => 8
)
To divide the array in two halves you can use
$division = ceil( count($stores) / 2 );
$firstHalf = array_slice($stores, 0, $division);
$secondHalf = array_slice($stores, $division);

Related

Sort an array of numbers into two arrays of equal length in PHP

I have an array of 20 different numbers which contains ranking of students.
I want to split this array into two sub arrays of equal length i.e 10
I also want the sum of all numbers within each array to be close.
For example, in sub-array A there could be a total sum of 56 and in sub-array B there could be a total sum of 57.
I am using PHP.
I sort the main array here and would like to assign index[0] to sub-array A and index[1] to sub-array B, and keep repeating this until both arrays are filled.
My approach works but i think its not great and not dynamic.
I interate through the main original array for [i] and then add that to the first sub-array, then I set i = i+2 so that I get every second value and store them in the first array.
I then remove the value at index[i] from the main array.
What is left over is now sub-array B.
$kids = array (8,5,6,9,3,8,2,4,6,10,8,5,6,1,7,10,5,3,7,6);
sort($kids);
$arrlength = count($kids);
for($x = 0; $x < $arrlength; $x++) {
echo $kids[$x];
echo "<br>";
}
$teamA = array();
$teamB = array();
$i = 0;
while ($i < $arrlength)
{
#echo $kids[$i] ."<br />";
array_push($teamA, $kids[$i]);
unset($kids[$i]);
$i += 2;
}
$teamB = $kids;
print_r($teamA);
print_r($teamB);
My Output is :
Array ( [0] => 1 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 6 [6] => 7 [7] =>
8 [8] => 8 [9] => 10 )
The sum here of all values is = 58
Array ( [1] => 2 [3] => 3 [5] => 5 [7] => 5 [9] => 6
[11] => 6 [13] => 7 [15] => 8 [17] => 9 [19] => 10 )
The sum here of all values is = 61
Any help is greatly appreciated. I have no real experience with PHP or its built in functions so sorry if this is a basic question. Thanks!
There is a built-in helper function of php, array_slice. You can read about it in the link I provided.
Here's how you can use in to achieve what you want:
$kids = array (5,7,6,8,3,8,2,4,6,10,8,5,6,10,7,6,5,3,7,6);
sort($kids);
$arrlength = count($kids);
$arrayA= array_slice($kids, 0, $arrlength / 2);
$arrayB= array_slice($kids, $arrlength / 2);
Output
// $arrayA
array:10 [▼
0 => 2
1 => 3
2 => 3
3 => 4
4 => 5
5 => 5
6 => 5
7 => 6
8 => 6
9 => 6
]
// $arrayB
array:10 [▼
0 => 6
1 => 6
2 => 7
3 => 7
4 => 7
5 => 8
6 => 8
7 => 8
8 => 10
9 => 10
]
Another approach for achieving what you asked
$kids = array(8,5,6,9,3,8,2,4,6,10,8,5,6,1,7,10,5,3,7,6);
sort($kids);
$teamA = array();
$teamB = array();
foreach($kids as $i => $kid){
if($i % 2){
array_push($teamA, $kid);
} else{
array_push($teamB, $kid);
}
}
It will generate the same output and sum as you want.

How do i find the occurence of words of particular number

Hello guys I have a small question that suppose I have a string as
"Hello My name is XYZ"
Now I know I can find the length of the words as "Hello" has 5 characters and "My" has 2 characters. By using following code
$text = file_get_contents('text.txt'); // $text = 'Hello my name is XYZ';
$words = str_word_count($text, 1);
$wordsLength = array_map(
function($word) { return mb_strlen($word, 'UTF-8'); },
$words
);
var_dump(array_combine($words, $wordsLength));
But what if i want to find that the number of words with length 1 is 0. The number of words with lengths 2 is 2. The number of words with length 3 is 1 and so on till number of length 10
Note- I am considering the word length till there is a space Suppose there is a date in the data like 20.04.2016 so it should show me that the number is words with length 10 is 1.
and one more thing how do I find the average length for the words in the string.
Thank you in advance
If you use array_count_values() on the $wordsLength array it will give a count of the string lengths there are. If you use this and a template array (created using array_fill()) with the elements 1-10 and a value of 0. You will get a list of all of the word counts...
$counts = array_replace(array_fill(1, 9, 0),
array_count_values($wordsLength));
will give...
Array
(
[1] => 0
[2] => 2
[3] => 1
[4] => 1
[5] => 1
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
Hi try this it works on the date and special chars,emojis
$text = 'Hello 20.04.2016 🚩 my face😘face is XYZ';
$words =preg_split('/\s+/', $text);
$wordsLength = array_map(
function($word) { return mb_strlen($word, 'UTF-8'); } ,$words);
print_r($words);
//Get Average word Length
$avg=round(array_sum($wordsLength)/count($words),1);
//print Avg
print($avg);
?>
(Demo)
$text = ' Hello 20.04.2016 🚩 my incredibleness face😘face is XYZ ';
Generate array of continuous visible characters
$words = preg_split('/\s+/', $text, 0, PREG_SPLIT_NO_EMPTY);
array (
0 => 'Hello',
1 => '20.04.2016',
2 => '🚩',
3 => 'my',
4 => 'incredibleness',
5 => 'face😘face',
6 => 'is',
7 => 'XYZ',
)
Replace visible strings with their multibyte length notice the simpler syntax
$wordsLength = array_map('mb_strlen', $words);
array (
0 => 5,
1 => 10,
2 => 1,
3 => 2,
4 => 14,
5 => 9,
6 => 2,
7 => 3,
)
Group and count lengths
$lengthCounts = array_count_values($wordsLength);
array (
5 => 1,
10 => 1,
1 => 1,
2 => 2,
14 => 1,
9 => 1,
3 => 1,
)
Establish an array of defaults, because $lengthCounts may have gaps
$defaultCounts = array_fill_keys(range(1,10), 0);
array (
1 => 0,
2 => 0,
3 => 0,
4 => 0,
5 => 0,
6 => 0,
7 => 0,
8 => 0,
9 => 0,
10 => 0,
)
Filter the counts to remove elements/counts that are out-of-range
$filteredCounts = array_intersect_key($lengthCounts, $defaultCounts);
array (
5 => 1,
10 => 1,
1 => 1,
2 => 2,
9 => 1,
3 => 1,
)
Overwrite the defaults with found counts to prevent gaps in the output
$gaplessCounts = array_replace($defaultCounts, $filteredCounts);
array (
1 => 1,
2 => 2,
3 => 1,
4 => 0,
5 => 1,
6 => 0,
7 => 0,
8 => 0,
9 => 1,
10 => 1,
)

How to remove every third element in a php array until only one element remains and print that element?

The array is like this
$a = array(1,2,3,4,5,6,7,8);
After that in each iteration the 3rd element should be removed until it reaches to a single element
the iteration will be something like this
index:0 1 2 3 4 5 6 7
value:1 2 3 4 5 6 7 8
this is the normal one
index:0 1 2 3 4 5 6 7
value:1 2 4 5 7 8
here 3 and 6 removed as they came out as the 3rd elements
then after 6 is removed it should count 7 and 8 as 1st and 2nd and go to value 1 which makes 1 as the 3rd element.This continues until there is only one element remaining.
output
12345678
1245678
124578
24578
2478
478
47
7
7 is the remaining element
here is the code, hope it helps.
<?php
$array = [1,2, 3,4,5,6,7,8];
function removeAtNth($array, $nth)
{
$step = $nth - 1; //gaps between operations
$benchmark = 0;
while(isset($array[1]))
{
$benchmark += $step;
$benchmark = $benchmark > count($array) -1 ? $benchmark % count($array) : $benchmark;
echo $benchmark."\n";
unset($array[$benchmark]);
$array = array_values($array);
echo implode('', $array)."\n";
}
}
removeAtNth($array, 3);
result:
kris-roofe#krisroofe-Rev-station:~$ php test.php
1245678
124578
24578
2478
478
47
7
Your looking for array_chunk()
$a = array(1,2,3,4,5,6,7,8);
$thirds = array_chunk($a, 3);
$thirds now is like:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[2] => Array
(
[0] => 7
[1] => 8
)
)
Then just loop through the $thirds array and array_pop() to grab the last value.
However, I'm not sure why you're looking to get 7 at the end and not 8. Can you explain?

How to make a graphic using numbers with PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to break a number in a table field and turn it into an array.
input_table
+------+---------+
| id | number |
+------+---------+
| 1 | 7 |
| 2 | 8 |
+------+---------+
for the id 1 (which has 7 number) the output what I need in php is:
1 2 3 4 5 6 7
2 3 4 5 6
3 4 5
4
How to do this?
for ($q=1; $q <= $obj->number ; $q++) {
echo "$q";
//This only turn 1, 2, 3, 4, 5, 6, 7
}
try something like this.
$set = array( 7, 8 );
echo '<pre>';
foreach( $set as $number ){
//assuming number is your INT
$array = range( 1, $number );
while( count( $array ) ){
echo "\n";
var_export( $array );
//remove first element
array_shift( $array );
//remove last element
array_pop($array);
}
}
Outputs:
For 7
array (
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
5 => 6,
6 => 7,
)
array (
0 => 2,
1 => 3,
2 => 4,
3 => 5,
4 => 6,
)
array (
0 => 3,
1 => 4,
2 => 5,
)
array (
0 => 4,
)
For 8
array (
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
5 => 6,
6 => 7,
7 => 8,
)
array (
0 => 2,
1 => 3,
2 => 4,
3 => 5,
4 => 6,
5 => 7,
)
array (
0 => 3,
1 => 4,
2 => 5,
3 => 6,
)
array (
0 => 4,
1 => 5,
)
I'll leave it to you to build a multi-dimensional array out of that.
If you just want the output, than use echo implode(' ', $array ); in place of var_export(). Such as this:
$set = array( 7, 8 );
echo '<div style="text-align:center">';
foreach( $set as $number ){
//assuming number is your INT
$array = range( 1, $number );
while( count( $array ) ){
echo implode(' ', $array );
//remove first element
array_shift($array);
//remove last element
array_pop($array);
echo '<br>';
}
echo '<br>';
}
echo '</div>';
Outputs:
1 2 3 4 5 6 7
2 3 4 5 6
3 4 5
4
1 2 3 4 5 6 7 8
2 3 4 5 6 7
3 4 5 6
4 5
<?php
$n = 7; //or whatever you want
echo '<div style="text-align:center">';
for($i=0;$i<=round($n/2,0);$i++){
for($j=$i; $j<$n-$i;$j++){
echo ($j+1).' ';
}
echo "<br />\n";
}
echo '</div>';

php importance of a number in a set of numbers

I have a set of numbers e.g.
$input = array(1, 4, 7, 4, 9, 4, 8, 6, 2, 8, 7, 7, 4, 5, 3);
I am trying to work out the importance of each number based on the following rule:
As the sequence gets longer the numbers get less significant, and each time a number is mentioned then it will improve the relevance (how much depends on its position in the
sequence).
I am expecting something like:
Array(
'4' => 90%
'1' => 75%
'7' => 60%
....
)
So 4 is the most inportant, followed by 1 and then 7 etc. Note that the output is completely fabricated but gives in indication that 4 should be the most important. I believe I want some kind of linear solution.
Is this more of what you were thinking? Answer based on stillstanding
$numbers = array(1, 4, 7, 4, 9, 4, 8, 6, 2, 8, 7, 7, 4, 5, 3);
$weight = array();
$count = count($numbers);
for ($i=0; $i<$count; $i++) {
if (!isset($weight[$numbers[$i]])) $weight[$numbers[$i]] = 1;
$weight[$numbers[$i]] += $count + pow($count - $i, 2);
}
$max = array_sum($weight);
foreach ($weight as &$w) {
$w = ($w / $max) * 100;
}
arsort($weight);
result:
Array
(
[4] => 34.5997286296
[7] => 17.3677069199
[1] => 16.3500678426
[8] => 10.0407055631
[9] => 9.29443690638
[6] => 5.42740841248
[2] => 4.40976933514
[5] => 1.35685210312
[3] => 1.15332428765
)
$numbers=array(1, 4, 7, 4, 9, 4, 8, 6, 2, 8, 7, 7, 4, 5, 3);
$weight=array();
$count=count($numbers);
for ($i=0; $i<$count; $i++) {
if (!isset($weight[$numbers[$i]]))
$weight[$numbers[$i]]=1;
$weight[$numbers[$i]]*=$count-$i;
}
var_dump($weight);
Result:
Array
(
[1] => 15
[4] => 5040
[7] => 260
[9] => 11
[8] => 54
[6] => 8
[2] => 7
[5] => 2
[3] => 1
)
This algorithm is fairly simplistic, but I think it accomplishes what you're looking for.
Given that you have the sequence you described above and it is stored in an array called $sequence
$a = array();
for($i=0;$i<count($sequence);$i++)
{
//calculate the relevance = 1/position in array
$relevance = 1/($i+1);
//add $relevance to the value of $a[$sequence[$i]]
if(array_key_exists((string)$sequence[$i],$a))
$a[(string)$sequence[$i]] += $relevance;
else
$a[(string)$sequence[$i]] = $relevance;
}
return $a;

Categories