Dup Ver Goto 📝

PHP Array Cheat Sheet

To
40 lines, 135 words, 996 chars Page 'ArrayCheatSheet' does not exist.
$arr_a = array(1,2,3);
$arr_b = [4,5,6];
$arr_c = ["hello"=>"world"];
$arr_d = array("hello"=>"world");
$a = array_pop($arr_a);
$b = array_shift($arr_b);
array_unshift($arr_a,5,6,7);
array_push($arr_b,9,8,7);

Note that with array_filter the array comes first; with array_map, the function comes first. Note that array_map can take more than one array as arguments.

$arr_c = array_filter($arr_a,function($x) { return true; });
$arr_d = array_map(function($x) { return "hello $x"; }, $arr_a, $arr_b);
if( count($arr_a) > 0 ) { ... }
if( array_key_exists("hello",$arr_a) ) { ... }
if( isset($arr_a["hello"]) ) { ... }
if( in_array("world",$arr_a) ) { ... }
$xs = explode(",","hello,world");
$y = implode(":",$xs);

Reindexing

If we filter and don't reindex, then json_encode will return a {"dict":"object"} and not an [a,r,r,a,y]. To reindex:

$arr_e = array_values($arr_d);

Also there is

$keys = array_keys($arr_e);