1function sortByAge($a, $b) {
2 return $a['age'] > $b['age'];
3}
4$people=[
5 ["age"=>54,"first_name"=>"Bob","last_name"=>"Dillion"],
6 ["age"=>22,"first_name"=>"Sarah","last_name"=>"Harvard"],
7 ["age"=>31,"first_name"=>"Chuck","last_name"=>"Bartowski"]
8];
9
10usort($people, 'sortByAge'); //$people is now sorted by age (ascending)
1substr ( $string , $start , $length );
2
3/**Returns the extracted part of string;
4 *or FALSE on failure, or an empty string
5 */
1const str = 'substr';
2
3console.log(str.substr(1, 2)); // (1, 2): ub
4console.log(str.substr(1)); // (1): ubstr
5
6/* Percorrendo de trás para frente */
7console.log(str.substr(-3, 2)); // (-3, 2): st
8console.log(str.substr(-3)); // (-3): str
9
1<?php
2$str = "Africa Beautiful!";
3echo substr($str, 0, 6); // Outputs: Africa
4echo substr($str, 0, -10); // Outputs: Beautiful
5echo substr($str, 0); // Outputs: Africa Beautiful!
6?>
1array_slice() function is used to get selected part of an array.
2Syntax:
3array_slice(array, start, length, preserve)
4*preserve = false (default)
5If we put preserve=true then the key of value are same as original array.
6
7Example (without preserve):
8<?php
9$a=array("red","green","blue","yellow","brown");
10print_r(array_slice($a,1,2));
11?>
12
13Output:
14Array ( [0] => green [1] => blue )
15
16Example (with preserve):
17<?php
18$a=array("red","green","blue","yellow","brown");
19print_r(array_slice($a,1,2,true));
20?>
21
22Output:
23Array ( [1] => green [2] => blue )
24