Arrays Flashcards
How do you create a new array?
$array = array(); or $array[0] = “info” or range() for numeric arrays.
What are the types of arrays?
Indexed arrays - Arrays with a numeric index
Associative arrays - Arrays with named keys
Multidimensional arrays - Arrays containing one or more arrays
How do you access data from an associative array?
$array[‘string’]
How do you access data from a numeric array?
$array[N] where N = a positive integer
How do you add to the end of an array?
$array[] = value;
Can you add keys to an array out of order?
Yes, keys can be added in any order.
How do you initialize a multidimensional array?
array(array(), array(), etc.)
How do you make a single assignment in a multi-dimensional array?
$array[][] = value
How do you access a single cell in a multidimensional array?
$array[key][key];
How do you search an array by value?
in_array() and array_search()
How do you search an array by key?
array_key_exists() or isset($array[“key”])
What is the difference between array_key_exists() and isset()?
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 do you find the length of an array?
count($array)
What is the difference between sort() and asort()?
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 do you sort a multidimensional array?
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 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));
array_multisort($matrix, SORT_DESC);
array_multisort($matrix[0], SORT_DESC, $matrix[1], SORT_DESC, $matrix[2], SORT_DESC);
How do you remove an array element from the end?
array_pop(array)
EXAMPLE:
$a=array(“red”,”green”,”blue”);
array_pop($a);
print_r($a);
Array ( [0] => red [1] => green )
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);
array_diff($array1, $array2)
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);
$array2 = array_slice($array1, 2, 5);
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');
$array3 = array_merge($array1, $array2);
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);
array(7) { [“a”]=> “Apples” [“b”]=> “Oranges” [“c”]=> “Bananas” [“d”]=> “Kiwi” [“e”]=> “Plums” [“f”]=> “Pears” [“g”]=> “Cherries” }
How do you change all keys in an array to lowercase or uppercase?
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 do you split an array into chunks of arrays?
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 do you return the values from a single column in the input array?
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 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 )
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 )
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 )
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 )
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 )
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 )
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 )
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 "<br>"; print_r($b1);
OUTPUT:
Array ( [3] => blue [4] => blue [5] => blue [6] => blue )
Array ( [0] => red )
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 )
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 )
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 )
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 )
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 )
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 )