Php array get number

PHP range() Function

Create an array containing a range of elements from «0» to «5»:

Definition and Usage

The range() function creates an array containing a range of elements.

This function returns an array of elements from low to high.

Note: If the low parameter is higher than the high parameter, the range array will be from high to low.

Syntax

Parameter Values

Parameter Description
low Required. Specifies the lowest value of the array
high Required. Specifies the highest value of the array
step Optional. Specifies the increment used in the range. Default is 1

Technical Details

Return Value: Returns an array of elements from low to high
PHP Version: 4+
PHP Changelog: The step parameter was added in PHP 5.0.

In PHP versions 4.1.0 through 4.3.2, this function sees numeric strings as strings and not integers. The numeric strings will be used for character sequences, e.g., «5252» is treated as «5».

More Examples

Example

Return an array of elements from «0» to «50» and increment by 10.

Example

Using letters — return an array of elements from «a» to «d»

Источник

Php array get number

// Before php 5.4
$array = array(1,2,3);

// since php 5.4 , short syntax
$array = [1,2,3];

// I recommend using the short syntax if you have php version >= 5.4

Used to creating arrays like this in Perl?

Looks like we need the range() function in PHP:

$array = array_merge (array( ‘All’ ), range ( ‘A’ , ‘Z’ ));
?>

You don’t need to array_merge if it’s just one range:

There is another kind of array (php>= 5.3.0) produced by

$array = new SplFixedArray(5);

Standard arrays, as documented here, are marvellously flexible and, due to the underlying hashtable, extremely fast for certain kinds of lookup operation.

Supposing a large string-keyed array

$arr=[‘string1’=>$data1, ‘string2’=>$data2 etc. ]

when getting the keyed data with

php does *not* have to search through the array comparing each key string to the given key (‘string1’) one by one, which could take a long time with a large array. Instead the hashtable means that php takes the given key string and computes from it the memory location of the keyed data, and then instantly retrieves the data. Marvellous! And so quick. And no need to know anything about hashtables as it’s all hidden away.

However, there is a lot of overhead in that. It uses lots of memory, as hashtables tend to (also nearly doubling on a 64bit server), and should be significantly slower for integer keyed arrays than old-fashioned (non-hashtable) integer-keyed arrays. For that see more on SplFixedArray :

Unlike a standard php (hashtabled) array, if you lookup by integer then the integer itself denotes the memory location of the data, no hashtable computation on the integer key needed. This is much quicker. It’s also quicker to build the array compared to the complex operations needed for hashtables. And it uses a lot less memory as there is no hashtable data structure. This is really an optimisation decision, but in some cases of large integer keyed arrays it may significantly reduce server memory and increase performance (including the avoiding of expensive memory deallocation of hashtable arrays at the exiting of the script).

When creating arrays , if we have an element with the same value as another element from the same array, we would expect PHP instead of creating new zval container to increase the refcount and point the duplicate symbol to the same zval. This is true except for value type integer.
Example:

$arr = [‘bebe’ => ‘Bob’, ‘age’ => 23, ‘too’ => 23 ];
xdebug_debug_zval( ‘arr’ );

(refcount=2, is_ref=0)
array (size=3)
‘bebe’ => (refcount=1, is_ref=0)string ‘Bob’ (length=3)
‘age’ => (refcount=0, is_ref=0)int 23
‘too’ => (refcount=0, is_ref=0)int 23

but :
$arr = [‘bebe’ => ‘Bob’, ‘age’ => 23, ‘too’ => ’23’ ];
xdebug_debug_zval( ‘arr’ );

(refcount=2, is_ref=0)
array (size=3)
‘bebe’ => (refcount=1, is_ref=0)string ‘Bob’ (length=3)
‘age’ => (refcount=0, is_ref=0)int 23
‘too’ => (refcount=1, is_ref=0)string ’23’ (length=2)
or :

$arr = [‘bebe’ => ‘Bob’, ‘age’ => [1,2], ‘too’ => [1,2] ];
xdebug_debug_zval( ‘arr’ );

(refcount=2, is_ref=0)
array (size=3)
‘bebe’ => (refcount=1, is_ref=0)string ‘Bob’ (length=3)
‘age’ => (refcount=2, is_ref=0)
array (size=2)
0 => (refcount=0, is_ref=0)int 1
1 => (refcount=0, is_ref=0)int 2
‘too’ => (refcount=2, is_ref=0)
array (size=2)
0 => (refcount=0, is_ref=0)int 1
1 => (refcount=0, is_ref=0)int 2

This function makes (assoc.) array creation much easier:

function arr (. $array )< return $array ; >
?>

It allows for short syntax like:

$arr = arr ( x : 1 , y : 2 , z : 3 );
?>

Instead of:

$arr = [ «x» => 1 , «y» => 2 , «z» => 3 ];
// or
$arr2 = array( «x» => 1 , «y» => 2 , «z» => 3 );
?>

Sadly PHP 8.2 doesn’t support this named arguments in the «array» function/language construct.

Источник

range

If a step value is given, it will be used as the increment (or decrement) between elements in the sequence. step must not equal 0 and must not exceed the specified range. If not specified, step will default to 1.

Return Values

Returns an array of elements from start to end , inclusive.

Examples

Example #1 range() examples

// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
foreach ( range ( 0 , 12 ) as $number ) echo $number ;
>

// The step parameter
// array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
foreach ( range ( 0 , 100 , 10 ) as $number ) echo $number ;
>

// Usage of character sequences
// array(‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’);
foreach ( range ( ‘a’ , ‘i’ ) as $letter ) echo $letter ;
>

// array(‘c’, ‘b’, ‘a’);
foreach ( range ( ‘c’ , ‘a’ ) as $letter ) echo $letter ;
>
?>

Notes

Note:

Character sequence values are limited to a length of one. If a length greater than one is entered, only the first character is used.

See Also

User Contributed Notes 29 notes

To create a range array like

combine two range arrays using array_combine:

So with the introduction of single-character ranges to the range() function, the internal function tries to be «smart», and (I am inferring from behavior here) apparently checks the type of the incoming values. If one is numeric, including numeric string, then the other is treated as numeric; if it is a non-numeric string, it is treated as zero.

If you pass in a numeric string in such a way that is is forced to be recognized as type string and not type numeric, range() will function quite differently.

echo implode ( «» , range ( 9 , «Q» ));
// prints 9876543210

echo implode ( «» , range ( «9 » , «Q» )); //space after the 9
// prints 9:;?@ABCDEFGHIJKLMNOPQ

echo implode ( «» , range ( «q» , «9 » ));
// prints qponmlkjihgfedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDCBA@ ?> =?>

I wouldn’t call this a bug, because IMO it is even more useful than the stock usage of the function.

The function «range» is very useful to get an array of characters as range(‘C’,’R’) does.

At work, I had to extend the function range($a,$b) to work in this special case: with two uppercase strings $a and $b, it should return all the possible strings between $a and $b.
This could be used for example to get the excel column indexes.
e.g. array( ‘A’ , ‘B’ , ‘C’ . ‘Z’ , ‘AA’ , ‘AB’ , ‘AC’ , ‘AD’ ) ?>

So I wrote the function getrange($min,$max) that exactly does this.

function getcolumnrange ( $min , $max ) $pointer = strtoupper ( $min );
$output =array();
while( positionalcomparison ( $pointer , strtoupper ( $max )) <= 0 )array_push ( $output , $pointer );
$pointer ++;
>
return $output ;
>

function positionalcomparison ( $a , $b ) $a1 = stringtointvalue ( $a ); $b1 = stringtointvalue ( $b );
if( $a1 > $b1 )return 1 ;
else if( $a1 < $b1 )return - 1 ;
else return 0 ;
>

/*
* e.g. A=1 — B=2 — Z=26 — AA=27 — CZ=104 — DA=105 — ZZ=702 — AAA=703
*/
function stringtointvalue ( $str ) $amount = 0 ;
$strarra = array_reverse ( str_split ( $str ));

for( $i = 0 ; $i < strlen ( $str ); $i ++)$amount +=( ord ( $strarra [ $i ])- 64 )* pow ( 26 , $i );
>
return $amount ;
>
?>

The function will generate an array of integers even if your numerical parameters are enclosed in quotes.
var_dump ( range ( ‘1’ , ‘2’ ) ); // outputs array(2) < [0]=>int(1) [1]=> int(2) >
?>

An easy way to get an array of strings is to map strval() to the range:
var_dump ( array_map ( ‘strval’ , range ( ‘1’ , ‘2’ )) ); // outputs array(2) < [0]=>string(1) «1» [1]=> string(1) «2» >
?>

You might expect range($n, $n-1) to be an empty array (as in e.g. Python) but actually PHP will assume a step of -1 if start is larger than end.

function natural_prime_numbers (array $range , bool $print_info = false ) : array $start_time = time ();
$primes_numbers = array();
$print = » ;
$count_range = count ( $range );
foreach( $range as $number ) $values_division_number = array();
if( $number === 0 || $number === 1 || ! is_int ( $number )) < // eliminate 0, 1 and other no integer
continue;
>
if( $number != 2 && $number % 2 === 0 ) < // eliminate 2 and pairs numbers
continue;
>
for( $i = 1 ; $i <= $number ; $i ++)$resultado_divisao = $number / $i ;
$values_division_number [ $i ] = $resultado_divisao ;

if( $count_range $print .= PHP_EOL ;
$info = ‘The number ‘ . $number . ‘ divided by the number ‘ . $i . ‘ is equal to: ‘ .( $number / $i );
$print .= $info ;
if( $i === $number ) $print .= PHP_EOL ;
>
>

$values_division_number = array_values ( $values_division_number ); // reindex array

// here we want only array with 2 indexes with the values 1 and own number (rule to a natural prime number)
if( count ( $values_division_number ) === 2 && $values_division_number [ 0 ] === $number && $values_division_number [ 1 ] === 1 ) $primes_numbers [ $number ] = $number ;
>

>
>
return array(
‘length_prime_numbers’ => count ( $primes_numbers ),
‘prime_numbers’ => array_values ( $primes_numbers ),
‘print’ => $print ,
‘total_time_processing’ => ( time () — $start_time ). ‘ seconds.’ ,
);
>
var_dump ( natural_prime_numbers ( range ( 0 , 11 ))); // here the range() function 😉

// Result:
// array (size=3)
// ‘length_prime_numbers’ => int 5
// ‘prime_numbers’ =>
// array (size=5)
// 0 => int 2
// 1 => int 3
// 2 => int 5
// 3 => int 7
// 4 => int 11
// ‘print’ => string ‘
// O número 2 dividido pelo número 1 é igual a: 2
// O número 2 dividido pelo número 2 é igual a: 1

// O número 3 dividido pelo número 1 é igual a: 3
// O número 3 dividido pelo número 2 é igual a: 1.5
// O número 3 dividido pelo número 3 é igual a: 1

// O número 5 dividido pelo número 1 é igual a: 5
// O número 5 dividido pelo número 2 é igual a: 2.5
// O número 5 dividido pelo número 3 é igual a: 1.6666666666667
// O número 5 dividido pelo número 4 é igual a: 1.25
// O número 5 dividido pelo ‘.

// **************************** //
//
// * Remember that the function is recursive, that is: a range of 5000 takes more than 1 minute on a processor Intel® Core™ i5-8250U (3.40 GHz).
//
// **************************** //
?>

Источник

PHP count() Function

The count() function returns the number of elements in an array.

Syntax

Parameter Values

  • 0 — Default. Does not count all elements of multidimensional arrays
  • 1 — Counts the array recursively (counts all the elements of multidimensional arrays)

Technical Details

Return Value: Returns the number of elements in the array
PHP Version: 4+
PHP Changelog: The mode parameter was added in PHP 4.2

More Examples

Example

Count the array recursively:

echo «Normal count: » . count($cars).»
«;
echo «Recursive count: » . count($cars,1);
?>

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Читайте также:  Require php абсолютный путь
Оцените статью