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>';
Related
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,
)
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 6 years ago.
Improve this question
So I have an array like this:
[
543 => 1,
22 => 3,
65 => 4,
10 => 5,
50 => 6,
]
Now I get a key and a value as input. For example 22 as key and 5 as value.
Now I want to use those two inputs as start and end point in my array and want to shift all keys one forward between those two positions.
[
543 => 1,
22 => 3, ─┐ ┌─ 65 => 3,
65 => 4, ├ Shift all those keys one forward to: ┤ 10 => 4,
10 => 5, ─┘ └─ 22 => 5,
50 => 6,
]
So the expected output would be:
[
543 => 1,
65 => 3,
10 => 4,
22=> 5,
50 => 6,
]
Figure out the start and end offset from your inputs in your array:
$startIndex = array_search(22, array_keys($arr));
$endIndex = array_search(5 , array_values($arr));
//↑ Your input
So for your example array this would look like this:
[
543 => 1, //Offset: 0
22 => 3, //Offset: 1 ← 22 found; offset: 1
65 => 4, //Offset: 2
10 => 5, //Offset: 3 ← 5 found; offset: 3
50 => 6, //Offset: 4
]
Split your array into three parts:
$before = array_slice($arr, 0, $startIndex, true);
$data = array_slice($arr, $startIndex, ($endIndex - $startIndex) + 1, true);
$after = array_slice($arr, $endIndex, null, true);
Visualized this would look like this:
[
543 => 1, → $before; Where you do NOT want to shift your keys
22 => 3, ┐
65 => 4, ├ $data; Where you want to shift your leys
10 => 5, ┘
50 => 6, → $after; Where you do NOT want to shift your keys
]
Rotate the data part keys, just by merging the last key at the start with the other keys at the end:
$keys = array_keys($data);
$keys = array_merge(array_slice($keys, -1), array_slice($keys, 0, -1));
$data = array_combine($keys, $data);
Put it all back together:
$arr = $before + $data + $after;
This question already has answers here:
How to have a stable sort in PHP with arsort()?
(7 answers)
Closed 9 years ago.
There is strange issue in php asort, arsort.
I am taking example of arsort
Case1
$a = array(
1 => 2,
2 => 1,
3 => 2,
4 => 1
);
arsort($a);
var_dump($a);
Output:
array(4) {
[3] =>
int(2)
[1] =>
int(2)
[4] =>
int(1)
[2] =>
int(1)
}
Here at index (3,1) and (4,2) are sorted in descending order because at index 3 and 1 values are same. Same for index 4 and 2.
Case2
$a = array(
1 => 2,
2 => 1,
3 => 2
);
arsort($a);
var_dump($a);
Output:
array(3) {
[1] =>
int(2)
[3] =>
int(2)
[2] =>
int(1)
}
Here at index (3,1) are sorted in ascending order and still at index 3 and 1 values are same.
Is there any solution for this issue? As I want that ordering should be certain. Like sort in either descending or ascending order if value at some indices are same.
According to the PHP documentation:
If any of these sort functions evaluates two members as equal then the order is undefined (the sorting is not stable).
You can't know exactly which behaviour is correct, with testing just with 2 elements. Here's an array with multiple elements (odd and even).
even number:
<?php
$a = array(
1 => 2,
2 => 1,
3 => 2,
4 => 1,
5 => 2,
6 => 1,
7 => 2,
8 => 1,
9 => 2,
10 => 1,
11 => 2,
12 => 1
);
arsort($a);
var_dump($a);
Result:
array (size=12)
1 => int 2
7 => int 2
5 => int 2
11 => int 2
9 => int 2
3 => int 2
10 => int 1
12 => int 1
6 => int 1
2 => int 1
4 => int 1
8 => int 1
odd number
<?php
$a = array(
1 => 2,
2 => 1,
3 => 2,
4 => 1,
5 => 2,
6 => 1,
7 => 2,
8 => 1,
9 => 2,
10 => 1,
11 => 2,
12 => 1,
13 => 2
);
arsort($a);
var_dump($a);
Result
array (size=13)
9 => int 2
11 => int 2
13 => int 2
1 => int 2
7 => int 2
3 => int 2
5 => int 2
12 => int 1
2 => int 1
4 => int 1
8 => int 1
6 => int 1
10 => int 1
The question is now, where did it add the 13th element (=2)? it's added just after the 11th element and before the first element...this means that there's no rule here. (At least according to what we see).
We can't say that it follows any rule with testing with just 2 variables like what you did, because you saw (1,3) and you presumed that it's sorted by key. Which is apparently not the case with multiple variables.
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);
The code in question comes from MathGuard, a PHP anti-spam CAPTCHA script that requires the user to answer a simple math problem. It displays the digits and operator symbols as 3x5 matrices of random characters. I understand how the code works in the sense that I can follow the code and understand what it's doing; I just don't understand how one would come to this solution.
This function takes an integer that describes one line of the 3x5 matrix and converts it into a line of random characters:
function decToBin($dec) {
$pattern = "123456789ABCDEFGHIJKLMNOPQRTSTUWXYZ";
$output = " ";
$i = 0;
do {
if ($dec % 2) {
$rand = rand() % 34;
$output { 2 - $i } = $pattern { $rand };
} else {
$output { 2 - $i } = " ";
}
$dec = (int) ($dec / 2);
$i++;
} while ($dec > 0);
$output = str_replace(" ", " ", $output);
return $output;
}
Here are the digit descriptors:
$number = array (
array ( 7, 5, 5, 5, 7 ), // 0
array ( 2, 6, 2, 2, 7 ), // 1
array ( 7, 1, 7, 4, 7 ), // 2
array ( 7, 1, 7, 1, 7 ), // 3
array ( 4, 5, 7, 1, 1 ), // 4
array ( 7, 4, 7, 1, 7 ), // 5
array ( 7, 4, 7, 5, 7 ), // 6
array ( 7, 1, 1, 1, 1 ), // 7
array ( 7, 5, 7, 5, 7 ), // 8
array ( 7, 5, 7, 1, 7 ) // 9
);
My question is: how does one come to this conclusion and method of generation and know that, for example, 7 will generate a full line of random characters, and 5 only the outermost characters?
Is this just a form of code obfuscation? What makes this method better than, say, storing the digits as a string (111101101101111 as 0, for example) and replacing each 1 with a random character?
Looks like a simple bitmap to me.
7 = 1 1 1
5 = 1 0 1
5 = 1 0 1
5 = 1 0 1
7 = 1 1 1
2 = 0 1 0
6 = 1 1 0
2 = 0 1 0
2 = 0 1 0
7 = 1 1 1