I have 100 files named: file1, file2,file3... file100. I want to enter only 10 files in Array via preg_grep function, how can I limit the number?
$dir=glob('/mydir/*');
preg_grep('#file#i', $dir);
I get result array with length 100,how can I divide it before it create?
For PHP, to get the 1st 10 elements from the $dir array, use array_slice.
$dir=glob('/mydir/*');
$dir10 = array_slice($dir, 0, 10);
preg_grep('#file#i', $dir10);
Related
I have a 2-dimensional PHP array containing strings ($fulltable) which I'm trying to fit into the datables grid (https://www.datatables.net/).
Sometimes some of the strings are really long. I'd like to truncate each string to lets say to 75 charachters, which will make the fields more manageable on display.
Is there an easy PHP function to do this or should I just create a double loop like this?
foreach ($fulltable as $row) {
foreach ($row as $field) {
// TRUNCATE FIELD HERE
}
}
You could use array_walk_recursive() to do this and take the value by reference, e.g.
array_walk_recursive($arr, function(&$v){
$v = substr($v, 0, 75);
});
Use mb_substr:
mb_substr($field, 0, 30);
Where 0 is the beginning and 30 is the end, 30 could be anything you want, the length of your output.
array_map() or array_walk() will apply a function to the contents of an array (single dimension), and be probably faster that looping with foreach.
There is also array_walk_recursive() for multi dimensional arrays.
I have an array of 1000 emails.
And I want to split the array into n arrays having 45 emails in each array.
I want to separately access the splitted arrays.
How can we implement using php. I have also tried array_splice() function in php, but it returns splitted arrays, but I unable to access the splitted array.
You could use array_chunk() in this case to get batches of those emails:
$emails = array('email1', 'email2', ..... 'email1000');
// 45 each batch
$batch_emails = array_chunk($emails, 45);
foreach($batch_emails as $batch) {
print_r($batch);
}
// or explicitly get batch/group 5
print_r($batch_emails[4]);
try this -
$f = array_chunk($yourArray, 45);
var_dump($f);
Chunks an array into arrays with size elements. The last chunk may contain less than size elements.
How to I get an array of the last n items of another array in PHP?
$n is equal to the number of items you want off the end.
$arr = array_slice($old_arr, -$n);
You can use array_slice:
$arr = array_slice($old_arr, -$n, $n, true);
If the array indices are meaningful to you, remember that array_slice will reset and reorder the numeric array indices. You need the preserve_keys flag (4th parameter) set to true to avoid this.
how make special random array. i have array(1=>35,2=>25,3=>40). how make possibility that array show special elements. Ex.
If i want get 100 elements from array. it will be
35/100 +-10 - must bee 1 element,
25/100 +-10 = must be 2 element,
40/100 +-10 - must be 3 element.
Elements must be random, but most elements +-10. i know its possible make by this code:
$a = array_fill(1, 35, 1);
$b = array_fill(36, 60, 2);
$c = array_fill(61, 100, 3);
array looks like array(35 elements with value 1, 25 elements with value 2, 40 elements with value 3)
and using merge and array_rand i will get my code. But i don't want this code. it will be create 100 items. need optimization this code. it's possible. help. :-)
Are you looking for a weighted random array?
Check this example:
http://20bits.com/articles/random-weighted-elements-in-php/
I want to shorten an array so it only contains 30 elements. If for example I have an array of 100 elements is it possible to take it and chop of (as to speak) 70 of those elements?
Use array_slice to extract the range of elements you need.
$short_array = array_slice($my_big_array, 0, 30)
$short_array will have first 30 elements of $my_big_array
http://php.net/manual/en/function.array-slice.php
Usage:
$shortarray = array_slice($longarray, 0, 30);