Arrays Flashcards

1
Q

How do you create a new array?

A

$array = array(); or $array[0] = “info” or range() for numeric arrays.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the types of arrays?

A

Indexed arrays - Arrays with a numeric index
Associative arrays - Arrays with named keys
Multidimensional arrays - Arrays containing one or more arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you access data from an associative array?

A

$array[‘string’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you access data from a numeric array?

A

$array[N] where N = a positive integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you add to the end of an array?

A

$array[] = value;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can you add keys to an array out of order?

A

Yes, keys can be added in any order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you initialize a multidimensional array?

A

array(array(), array(), etc.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you make a single assignment in a multi-dimensional array?

A

$array[][] = value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you access a single cell in a multidimensional array?

A

$array[key][key];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you search an array by value?

A

in_array() and array_search()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you search an array by key?

A

array_key_exists() or isset($array[“key”])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between array_key_exists() and isset()?

A

array_key_exists will tell you if a key exists in an array, whereas isset will only return true if the key/variable exists and is not null.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you find the length of an array?

A

count($array)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between sort() and asort()?

A

sort() is used for numeric arrays and will reset keys in an associative array to numbers, whereas asort() can be used to sort and associative array and maintain the current keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you sort a multidimensional array?

A

array_multisort(array1,sorting order,sorting type,array2,array3…)

EXAMPLE:

$a=array(“Dog”,”Cat”,”Horse”,”Bear”,”Zebra”);
array_multisort($a);
print_r($a);

OUTPUT:

Array ( [0] => Bear [1] => Cat [2] => Dog [3] => Horse [4] => Zebra )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How would you sort this array and its children into descending order?

$matrix = array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9));

A

array_multisort($matrix, SORT_DESC);

array_multisort($matrix[0], SORT_DESC, $matrix[1], SORT_DESC, $matrix[2], SORT_DESC);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you remove an array element from the end?

A

array_pop(array)

EXAMPLE:

$a=array(“red”,”green”,”blue”);
array_pop($a);
print_r($a);

Array ( [0] => red [1] => green )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How would you measure the difference between these 2 arrays?

$array1 = array(1, 2, 3, 4, 5, 6, 7);
$array2 = array(2, 4, 6, 8);
A

array_diff($array1, $array2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How would you copy and create a new array from the numbers 3-7 in the following array?

$array1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9);

A

$array2 = array_slice($array1, 2, 5);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How would you merge these two array?

$array1 = array('a' => 'Apples', 'b' => 'Oranges', 'c' => 'Bananas', 'd' => 'Grapes');
$array2 = array('d' => 'Kiwi', 'e' => 'Plums', 'f' => 'Pears', 'g' => 'Cherries');
A

$array3 = array_merge($array1, $array2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What will be the result?

$array1 = array('a' => 'Apples', 'b' => 'Oranges', 'c' => 'Bananas', 'd' => 'Grapes');
$array2 = array('d' => 'Kiwi', 'e' => 'Plums', 'f' => 'Pears', 'g' => 'Cherries');
$array3 = array_merge($array1, $array2);
A

array(7) { [“a”]=> “Apples” [“b”]=> “Oranges” [“c”]=> “Bananas” [“d”]=> “Kiwi” [“e”]=> “Plums” [“f”]=> “Pears” [“g”]=> “Cherries” }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How do you change all keys in an array to lowercase or uppercase?

A

array_change_key_case(array,case);

CASE_LOWER - Default value. Changes the keys to lowercase
CASE_UPPER - Changes the keys to uppercase

EXAMPLE:

$age=array(“Peter”=>”35”,”Ben”=>”37”,”Joe”=>”43”);
print_r(array_change_key_case($age,CASE_UPPER));

OUTPUT:

Array ( [PETER] => 35 [BEN] => 37 [JOE] => 43 )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How do you split an array into chunks of arrays?

A

array_chunk(array,size,preserve_key);

EXAMPLE:

$cars=array(“Volvo”,”BMW”,”Toyota”,”Honda”,”Mercedes”,”Opel”);
print_r(array_chunk($cars,2));

OUTPUT:

Array ( [0] => Array ( [0] => Volvo [1] => BMW ) [1] => Array ( [0] => Toyota [1] => Honda ) [2] => Array ( [0] => Mercedes [1] => Opel ) )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How do you return the values from a single column in the input array?

A

array_column(array,column_key,index_key);

EXAMPLE:

$a = array(
  array(
    'id' => 5698,
    'first_name' => 'Peter',
    'last_name' => 'Griffin',
  ),
  array(
    'id' => 4767,
    'first_name' => 'Ben',
    'last_name' => 'Smith',
  ),
  array(
    'id' => 3809,
    'first_name' => 'Joe',
    'last_name' => 'Doe',
  )
);

$last_names = array_column($a, ‘last_name’);
print_r($last_names);

OUTPUT:

Array
(
  [0] => Griffin
  [1] => Smith
  [2] => Doe
)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How do you create an array by using the elements from one "keys" array and one "values" array?
array_combine(keys,values); ----------------------- EXAMPLE: $fname=array("Peter","Ben","Joe"); $age=array("35","37","43"); $c=array_combine($fname,$age); print_r($c); OUTPUT: Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
26
How do you count all the values of an array?
array_count_values(array); ----------------------- EXAMPLE: $a=array("A","Cat","Dog","A","Dog"); print_r(array_count_values($a)); OUTPUT: Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
27
How do you compare the VALUES of two (or more) arrays, and return the differences?
array_diff(array1,array2,array3...); ----------------------- EXAMPLE: $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"black","g"=>"purple"); $a3=array("a"=>"red","b"=>"black","h"=>"yellow"); $result=array_diff($a1,$a2,$a3); print_r($result); OUTPUT: Array ( [b] => green [c] => blue )
28
How do you compare the keys AND values of two (or more) arrays, and return the differences?
array_diff_assoc(array1,array2,array3...); ----------------------- EXAMPLE: $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("a"=>"red","b"=>"green","c"=>"blue"); $result=array_diff_assoc($a1,$a2); print_r($result); OUTPUT: Array ( [d] => yellow )
29
How do you compare the KEYS of two (or more) arrays, and return the differences?
array_diff_key(array1,array2,array3...); ----------------------- EXAMPLE: $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("c"=>"yellow","d"=>"black","e"=>"brown"); $a3=array("f"=>"green","c"=>"purple","g"=>"red"); $result=array_diff_key($a1,$a2,$a3); print_r($result); OUTPUT: Array ( [a] => red [b] => green )
30
How can you compare arrays, and return the differences (compare keys AND values, using a user-defined key comparison function)?
array_diff_uassoc(array1,array2,array3...,myfunction); ----------------------- EXAMPLE: ``` function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } ``` $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"red","b"=>"green","d"=>"blue"); $a3=array("e"=>"yellow","a"=>"red","d"=>"blue"); $result=array_diff_uassoc($a1,$a2,$a3,"myfunction"); print_r($result); OUTPUT: Array ( [c] => blue )
31
How do you compare arrays, and return the differences (compare KEYS only, using a user-defined key comparison function)?
array_diff_ukey(array1,array2,array3...,myfunction); ----------------------- EXAMPLE: ``` function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } ``` $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"black","b"=>"yellow","d"=>"brown"); $a3=array("e"=>"purple","f"=>"white","a"=>"gold"); $result=array_diff_ukey($a1,$a2,$a3,"myfunction"); print_r($result); OUTPUT: Array ( [c] => blue )
32
How do you fill an array with values?
array_fill(index,number,value); ----------------------- EXAMPLE: ``` $a1=array_fill(3,4,"blue"); $b1=array_fill(0,1,"red"); print_r($a1); echo "
"; print_r($b1); ``` OUTPUT: Array ( [3] => blue [4] => blue [5] => blue [6] => blue ) Array ( [0] => red )
33
How do you fill an array with values, specifying keys?
array_fill_keys(keys,value); ----------------------- EXAMPLE: $keys=array("a","b","c","d"); $a1=array_fill_keys($keys,"blue"); print_r($a1); OUTPUT: Array ( [a] => blue [b] => blue [c] => blue [d] => blue )
34
How do you filter the values of an array using a callback function?
array_filter(array,callbackfunction); ----------------------- EXAMPLE: ``` function test_odd($var) { return($var & 1); } ``` $a1=array("a","b",2,3,4); print_r(array_filter($a1,"test_odd")); OUTPUT: Array ( [3] => 3 )
35
How do you flip/exchange all keys with their associated values in an array?
array_flip(array); ----------------------- EXAMPLE: $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $result=array_flip($a1); print_r($result); OUTPUT: Array ( [red] => a [green] => b [blue] => c [yellow] => d )
36
How do you compare arrays, and returns the matches (compare values only)?
array_intersect(array1,array2,array3...); ----------------------- EXAMPLE: $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=array_intersect($a1,$a2); print_r($result); OUPUT: Array ( [a] => red [b] => green [c] => blue )
37
How do you compare arrays and returns the matches (compare keys and values)?
array_intersect_assoc(array1,array2,array3...) ----------------------- EXAMPLE: $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("a"=>"red","b"=>"green","c"=>"blue"); $result=array_intersect_assoc($a1,$a2); print_r($result); OUTPUT: Array ( [a] => red [b] => green [c] => blue )
38
How do you compare arrays, and return the matches (compare keys only)?
array_intersect_key(array1,array2,array3...) ----------------------- EXAMPLE: $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"red","c"=>"blue","d"=>"pink"); $result=array_intersect_key($a1,$a2); print_r($result); OUTPUT: Array ( [a] => red [c] => blue )
39
How do you compare arrays, and return the matches (compare keys and values, using a user-defined key comparison function)?
array_intersect_uassoc(array1,array2,array3...,myfunction) ----------------------- EXAMPLE: ``` function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } ``` $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("d"=>"red","b"=>"green","e"=>"blue"); $result=array_intersect_uassoc($a1,$a2,"myfunction"); print_r($result); OUTPUT: Array ( [b] => green )
40
How do you compare arrays, and return the matches (compare keys only, using a user-defined key comparison function)
array_intersect_ukey(array1,array2,array3...,myfunction) ----------------------- EXAMPLE: ``` function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } ``` $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"blue","b"=>"black","e"=>"blue"); $result=array_intersect_ukey($a1,$a2,"myfunction"); print_r($result); OUTPUT: Array ( [a] => red [b] => green )
41
How do you check if the specified key exists in the array?
array_key_exists(key,array) ----------------------- EXAMPLE: ``` $a=array("Volvo"=>"XC90","BMW"=>"X5"); if (array_key_exists("Volvo",$a)) { echo "Key exists!"; } else { echo "Key does not exist!"; } ``` OUTPUT: Key exists!
42
How do you return all the keys of an array?
array_keys(array,value,strict) ----------------------- EXAMPLE: $a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander"); print_r(array_keys($a)); OUTPUT: Array ( [0] => Volvo [1] => BMW [2] => Toyota )
43
How do you send each value of an array to a user-made function, which returns new values?
array_map(myfunction,array1,array2,array3...) ----------------------- EXAMPLE: ``` function myfunction($num) { return($num*$num); } ``` $a=array(1,2,3,4,5); print_r(array_map("myfunction",$a)); OUTPUT: Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25 )
44
How do you merge one or more arrays into one array?
array_merge(array1,array2,array3...) ----------------------- EXAMPLE: $a1=array("red","green"); $a2=array("blue","yellow"); print_r(array_merge($a1,$a2)); OUTPUT: Array ( [0] => red [1] => green [2] => blue [3] => yellow )
45
How do you merge one or more arrays into one array recursively
array_merge_recursive(array1,array2,array3...) ----------------------- EXAMPLE: $a1=array("a"=>"red","b"=>"green"); $a2=array("c"=>"blue","b"=>"yellow"); print_r(array_merge_recursive($a1,$a2)); OUTPUT: Array ( [a] => red [b] => Array ( [0] => green [1] => yellow ) [c] => blue )
46
How do you insert a specified number of items, with a specified value, to an array?
array_pad(array,size,value) ----------------------- EXAMPLE: $a=array("red","green"); print_r(array_pad($a,5,"blue")); OUTPUT: Array ( [0] => red [1] => green [2] => blue [3] => blue [4] => blue )
47
How do you calculate the product of the values in an array?
array_product(array) ----------------------- EXAMPLE: $a=array(5,5); echo(array_product($a)); OUTPUT: 25
48
How do you insert one or more elements to the end of an array?
array_push(array,value1,value2...) ----------------------- EXAMPLE: $a=array("red","green"); array_push($a,"blue","yellow"); print_r($a); OUTPUT: Array ( [0] => red [1] => green [2] => blue [3] => yellow )
49
How do you return one or more random keys from an array?
array_rand(array,number) ----------------------- EXAMPLE: ``` $a=array("red","green","blue","yellow","brown"); $random_keys=array_rand($a,3); echo $a[$random_keys[0]]."
"; echo $a[$random_keys[1]]."
"; echo $a[$random_keys[2]]; ``` OUTPUT: green yellow brown
50
How do you return an array as a string, using a user-defined function?
array_reduce(array,myfunction,initial) ----------------------- EXAMPLE: ``` function myfunction($v1,$v2) { return $v1 . "-" . $v2; } $a=array("Dog","Cat","Horse"); print_r(array_reduce($a,"myfunction")); ``` OUTPUT: -Dog-Cat-Horse
51
How do you replace the values of the first array with the values from following arrays?
array_replace(array1,array2,array3...) ----------------------- EXAMPLE: $a1=array("red","green"); $a2=array("blue","yellow"); print_r(array_replace($a1,$a2)); OUTPUT: Array ( [0] => blue [1] => yellow )
52
How do you replace the values of the first array with the values from following arrays recursively?
array_replace_recursive(array1,array2,array3...) ----------------------- EXAMPLE: $a1=array("a"=>array("red"),"b"=>array("green","blue")); $a2=array("a"=>array("yellow"),"b"=>array("black")); $a3=array("a"=>array("orange"),"b"=>array("burgundy")); print_r(array_replace_recursive($a1,$a2,$a3)); OUTPUT: Array ( [a] => Array ( [0] => orange ) [b] => Array ( [0] => burgundy [1] => blue ) )
53
How do you return an array in the reverse order?
ray_reverse(array,preserve) ----------------------- EXAMPLE: $a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota"); print_r(array_reverse($a)); OUTPUT: Array ( [c] => Toyota [b] => BMW [a] => Volvo )
54
How do you search an array for a given value and return the key?
array_search(value,array,strict) ----------------------- EXAMPLE: ``` $a=array("a"=>"red","b"=>"green","c"=>"blue"); echo array_search("red",$a); ``` OUTPUT: a
55
How do you remove the first element from an array, and return the value of the removed element?
array_shift(array) ----------------------- EXAMPLE: ``` $a=array("a"=>"red","b"=>"green","c"=>"blue"); echo array_shift($a)."
"; print_r ($a); ``` OUPUT: red Array ( [b] => green [c] => blue )
56
How do you return selected parts of an array?
array_slice(array,start,length,preserve) ----------------------- EXAMPLE: $a=array("red","green","blue","yellow","brown"); print_r(array_slice($a,2)); OUTPUT: Array ( [0] => blue [1] => yellow [2] => brown )
57
How do you remove AND replace specified elements of an array?
array_splice(array,start,length,array) ----------------------- EXAMPLE: $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("a"=>"purple","b"=>"orange"); array_splice($a1,0,2,$a2); print_r($a1); OUTPUT: Array ( [0] => purple [1] => orange [c] => blue [d] => yellow )
58
How do you return the sum of the values in an array?
array_sum(array) ----------------------- EXAMPLE: ``` $a=array(5,15,25); echo array_sum($a); ``` OUTPUT: 45
59
How do you compare arrays, and return the differences (compare values only, using a user-defined key comparison function)?
array_udiff(array1,array2,array3...,myfunction) ----------------------- EXAMPLE: ``` function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } ``` $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"blue","b"=>"black","e"=>"blue"); $result=array_udiff($a1,$a2,"myfunction"); print_r($result); OUTPUT: Array ( [a] => red [b] => green )
60
How do you compare arrays, and return the differences (compare keys and values, using a built-in function to compare the keys and a user-defined function to compare the values)?
array_udiff_assoc(array1,array2,array3...,myfunction) ----------------------- EXAMPLE: ``` function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } ``` $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"red","b"=>"blue","c"=>"green"); $result=array_udiff_assoc($a1,$a2,"myfunction"); print_r($result); OUTPUT: Array ( [b] => green [c] => blue )
61
How do you compare arrays, and return the differences (compare keys and values, using two user-defined key comparison functions)
array_udiff_uassoc(array1,array2,array3...,myfunction_key,myfunction_value) ----------------------- EXAMPLE: ``` function myfunction_key($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } ``` ``` function myfunction_value($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } ``` $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"red","b"=>"green","c"=>"green"); $result=array_udiff_uassoc($a1,$a2,"myfunction_key","myfunction_value"); print_r($result); OUTPUT: Array ( [c] => blue )
62
How do you compare arrays, and return the matches (compare values only, using a user-defined key comparison function)
array_uintersect(array1,array2,array3...,myfunction) ----------------------- EXAMPLE: ``` function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } ``` $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"blue","b"=>"black","e"=>"blue"); $result=array_uintersect($a1,$a2,"myfunction"); print_r($result); OUTPUT: Array ( [c] => blue )
63
How do you compare arrays, and return the matches (compare keys and values, using a built-in function to compare the keys and a user-defined function to compare the values)?
array_uintersect_assoc(array1,array2,array3...,myfunction) ----------------------- EXAMPLE: ``` function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } ``` $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"red","b"=>"blue","c"=>"green"); $result=array_uintersect_assoc($a1,$a2,"myfunction"); print_r($result); OUTPUT: Array ( [a] => red )
64
How do you compare arrays, and return the matches (compare keys and values, using two user-defined key comparison functions)?
array_uintersect_uassoc(array1,array2,array3...,myfunction_key,myfunction_value) ----------------------- EXAMPLE: ``` function myfunction_key($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } ``` ``` function myfunction_value($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } ``` $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"red","b"=>"green","c"=>"green"); $result=array_uintersect_uassoc($a1,$a2,"myfunction_key","myfunction_value"); print_r($result); OUTPUT: Array ( [a] => red [b] => green )
65
Removes duplicate values from an array
array_unique(array) ----------------------- EXAMPLE: $a=array("a"=>"red","b"=>"green","c"=>"red"); print_r(array_unique($a)); OUPUT: Array ( [a] => red [b] => green )
66
Adds one or more elements to the beginning of an array
array_unshift(array,value1,value2,value3...) ----------------------- EXAMPLE: $a=array("a"=>"red","b"=>"green"); array_unshift($a,"blue"); print_r($a); OUPUT: Array ( [0] => blue [a] => red [b] => green )
67
Returns all the values of an array
array_values(array) ----------------------- EXAMPLE: $a=array("Name"=>"Peter","Age"=>"41","Country"=>"USA"); print_r(array_values($a)); OUPUT: Array ( [0] => Peter [1] => 41 [2] => USA )
68
Applies a user function to every member of an array
array_walk(array,myfunction,parameter...) ----------------------- EXAMPLE: ``` function myfunction($value,$key) { echo "The key $key has the value $value
"; } $a=array("a"=>"red","b"=>"green","c"=>"blue"); array_walk($a,"myfunction"); ``` OUPUT: The key a has the value red The key b has the value green The key c has the value blue
69
Applies a user function recursively to every member of an array
array_walk_recursive(array,myfunction,parameter...) ----------------------- EXAMPLE: ``` function myfunction($value,$key) { echo "The key $key has the value $value
"; } $a1=array("a"=>"red","b"=>"green"); $a2=array($a1,"1"=>"blue","2"=>"yellow"); array_walk_recursive($a2,"myfunction"); ``` OUPUT: The key a has the value red The key b has the value green The key 1 has the value blue The key 2 has the value yellow
70
Sorts an associative array in descending order, according to the value
arsort(array,sortingtype); ----------------------- EXAMPLE: $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); arsort($age); ``` foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ``` OUPUT: Key=Joe, Value=43 Key=Ben, Value=37 Key=Peter, Value=35
71
Sorts an associative array in ascending order, according to the value
asort(array,sortingtype); ----------------------- EXAMPLE: $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); asort($age); ``` foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ``` OUPUT: Key=Peter, Value=35 Key=Ben, Value=37 Key=Joe, Value=43
72
Create array containing variables and their values
compact(var1,var2...) ----------------------- EXAMPLE: ``` $firstname = "Peter"; $lastname = "Griffin"; $age = "41"; ``` $result = compact("firstname", "lastname", "age"); print_r($result); OUPUT: Array ( [firstname] => Peter [lastname] => Griffin [age] => 41 )
73
Returns the number of elements in an array
count(array,mode); ----------------------- EXAMPLE: ``` $cars=array("Volvo","BMW","Toyota"); echo count($cars); ``` OUPUT: 3
74
Returns the current element in an array
current(array) ----------------------- EXAMPLE: $people = array("Peter", "Joe", "Glenn", "Cleveland"); echo current($people) . "
"; OUPUT: Peter
75
Returns the current key and value pair from an array
each(array) ----------------------- EXAMPLE: $people = array("Peter", "Joe", "Glenn", "Cleveland"); print_r (each($people)); OUPUT: Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 )
76
Sets the internal pointer of an array to its last element
end(array) ----------------------- EXAMPLE: $people = array("Peter", "Joe", "Glenn", "Cleveland"); ``` echo current($people) . "
"; echo end($people); ``` OUPUT: Peter Cleveland
77
Imports variables into the current symbol table from an array
extract(array,extract_rules,prefix) ----------------------- EXAMPLE: $a = "Original"; $my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse"); extract($my_array); echo "\$a = $a; \$b = $b; \$c = $c"; OUPUT: $a = Cat; $b = Dog; $c = Horse
78
Checks if a specified value exists in an array
in_array(search,array,type) ----------------------- EXAMPLE: $people = array("Peter", "Joe", "Glenn", "Cleveland"); ``` if (in_array("Glenn", $people)) { echo "Match found"; } else { echo "Match not found"; } ``` OUPUT: Match found
79
Fetches a key from an array
key(array) ----------------------- EXAMPLE: $people=array("Peter","Joe","Glenn","Cleveland"); echo "The key from the current position is: " . key($people); OUPUT: The key from the current position is: 0
80
Sorts an associative array in descending order, according to the key
krsort(array,sortingtype); ----------------------- EXAMPLE: $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); krsort($age); ``` foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ``` OUPUT: Key=Peter, Value=35 Key=Joe, Value=43 Key=Ben, Value=37
81
Sorts an associative array in ascending order, according to the key
ksort(array,sortingtype); ----------------------- EXAMPLE: $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); ksort($age); ``` foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ``` OUPUT: Key=Ben, Value=37 Key=Joe, Value=43 Key=Peter, Value=35
82
Assigns variables as if they were an array
list(var1,var2...) ----------------------- EXAMPLE: $my_array = array("Dog","Cat","Horse"); list($a, $b, $c) = $my_array; echo "I have several animals, a $a, a $b and a $c."; OUPUT: I have several animals, a Dog, a Cat and a Horse.
83
Sorts an array using a case insensitive "natural order" algorithm
natcasesort(array) ----------------------- EXAMPLE: $temp_files = array("temp15.txt","Temp10.txt", "temp1.txt","Temp22.txt","temp2.txt"); natsort($temp_files); echo "Natural order: "; print_r($temp_files); echo "
"; natcasesort($temp_files); echo "Natural order case insensitve: "; print_r($temp_files); OUPUT: ``` Natural order: Array ( [0] => Temp10.txt [1] => Temp22.txt [2] => temp1.txt [4] => temp2.txt [3] => temp15.txt ) Natural order case insensitve: Array ( [2] => temp1.txt [4] => temp2.txt [0] => Temp10.txt [3] => temp15.txt [1] => Temp22.txt ) ```
84
Sorts an array using a "natural order" algorithm
natsort(array) ----------------------- EXAMPLE: $temp_files = array("temp15.txt","temp10.txt", "temp1.txt","temp22.txt","temp2.txt"); sort($temp_files); echo "Standard sorting: "; print_r($temp_files); echo "
"; natsort($temp_files); echo "Natural order: "; print_r($temp_files); OUPUT: Standard sorting: Array ( [0] => temp1.txt [1] => temp10.txt [2] => temp15.txt [3] => temp2.txt [4] => temp22.txt ) Natural order: Array ( [0] => temp1.txt [3] => temp2.txt [1] => temp10.txt [2] => temp15.txt [4] => temp22.txt )
85
Advance the internal array pointer of an array
next(array) ----------------------- EXAMPLE: $people = array("Peter", "Joe", "Glenn", "Cleveland"); ``` echo current($people) . "
"; echo next($people); ``` OUPUT: Peter Joe
86
Alias of current()
pos(array) ----------------------- EXAMPLE: $people = array("Peter", "Joe", "Glenn", "Cleveland"); echo pos($people) . "
"; OUPUT: Peter
87
Rewinds the internal array pointer
prev(array) ----------------------- EXAMPLE: $people = array("Peter", "Joe", "Glenn", "Cleveland"); ``` echo current($people) . "
"; echo next($people) . "
"; echo prev($people); ``` OUPUT: Peter Joe Peter
88
Creates an array containing a range of elements
range(low,high,step) ----------------------- EXAMPLE: $number = range(0,5); print_r ($number); OUPUT: Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
89
Sets the internal pointer of an array to its first element
reset(array) ----------------------- EXAMPLE: $people = array("Peter", "Joe", "Glenn", "Cleveland"); ``` echo current($people) . "
"; echo next($people) . "
"; ``` echo reset($people); OUPUT: Peter Joe Peter
90
Sorts an indexed array in descending order
rsort(array,sortingtype); ----------------------- EXAMPLE: $cars=array("Volvo","BMW","Toyota"); rsort($cars); $clength=count($cars); for($x=0;$x"; } OUPUT: Volvo Toyota BMW
91
Shuffles an array
shuffle(array) ----------------------- EXAMPLE: $my_array = array("red","green","blue","yellow","purple"); shuffle($my_array); print_r($my_array); OUPUT: Array ( [0] => red [1] => blue [2] => yellow [3] => green [4] => purple ) Refresh the page to see how shuffle() randomizes the order of the elements in the array.
92
Alias of count()
sizeof(array,mode); ----------------------- EXAMPLE: ``` $cars=array("Volvo","BMW","Toyota"); echo sizeof($cars); ``` OUPUT: 3
93
Sorts an indexed array in ascending order
sort(array,sortingtype); ----------------------- EXAMPLE: $cars=array("Volvo","BMW","Toyota"); sort($cars); $clength=count($cars); for($x=0;$x"; } OUPUT: BMW Toyota Volvo
94
Sorts an array by values using a user-defined comparison function
uasort(array,myfunction); ----------------------- EXAMPLE: ``` function my_sort($a,$b) { if ($a==$b) return 0; return ($a4,"b"=>2,"c"=>8,d=>"6"); uasort($arr,"my_sort"); ``` ``` foreach($arr as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ``` OUPUT: Key=b, Value=2 Key=a, Value=4 Key=d, Value=6 Key=c, Value=8
95
Sorts an array by keys using a user-defined comparison function
uksort(array,myfunction); ----------------------- EXAMPLE: ``` function my_sort($a,$b) { if ($a==$b) return 0; return ($a4,"b"=>2,"c"=>8,d=>"6"); uksort($arr,"my_sort"); ``` ``` foreach($arr as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ``` OUPUT: Key=a, Value=4 Key=b, Value=2 Key=c, Value=8 Key=d, Value=6
96
Sorts an array using a user-defined comparison function
usort(array,myfunction); ----------------------- EXAMPLE: ``` function my_sort($a,$b) { if ($a==$b) return 0; return ($a"; } ``` OUPUT: 2 4 6 8