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);
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 and I don't predict how many elements could be there in the array. And I want to keep only first five elements of the array and truncate the rest of the elements. Is there any build in PHP function to perform this action?
http://php.net/manual/en/function.array-slice.php
$truncated_array = array_slice ( $full_array , 0, 5 )
I have the following array:
$array = array(1,0,0,0,1,1,1,1,0,1,1,1,1,0,1,1,0,1,0,0,1,0,1);
I want split it up into individual arrays so that each array contains seven or less values.
So for example the first array will become:
$one = array(1,0,0,0,1,1,1)
$two = array(1,0,1,1,1,1,0)
$three = array(1,1,0,1,0,0,1);
$four = array(0,1);
Also how would you count the number of times 1 occurs in array one?
array_chunk() is what you are looking for.
$splitted = array_chunk($array, 7);
For counting the occurences I would be lazy. If your arrays only contain 1s or 0s, then a simple array_sum() would do:
print array_sum($splitted[0]); // for the first chunk
I want split it up into individual arrays so that each array contains seven or less values.
Use array_chunk(), which is made expressly for this purpose.
Also how would you count the number of times 1 occurs in array one?
Use array_count_values().
$one = array(1,0,0,0,1,1,1);
$one_counts = array_count_values($one);
print_r($one_counts);
// prints
Array
(
[0] => 3
[1] => 4
)
Assuming you want to preserve the contents of the array, I'd use array_slice() to extract the needed number of elements from the array, incrementing the '$offset' by the required count each time until the array was exhausted.
And as to your second question, try:
$num_ones=count(preg_grep(/^1$/,$array));
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/