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
Q

How do you create an array by using the elements from one “keys” array and one “values” array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How do you count all the values of an array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

How do you compare the VALUES of two (or more) arrays, and return the differences?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

How do you compare the keys AND values of two (or more) arrays, and return the differences?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

How do you compare the KEYS of two (or more) arrays, and return the differences?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

How can you compare arrays, and return the differences (compare keys AND values, using a user-defined key comparison function)?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

How do you compare arrays, and return the differences (compare KEYS only, using a user-defined key comparison function)?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

How do you fill an array with values?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

How do you fill an array with values, specifying keys?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

How do you filter the values of an array using a callback function?

A

array_filter(array,callbackfunction);

EXAMPLE:

function test_odd($var)
{
return($var &amp; 1);
}

$a1=array(“a”,”b”,2,3,4);
print_r(array_filter($a1,”test_odd”));

OUTPUT:

Array ( [3] => 3 )

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

How do you flip/exchange all keys with their associated values in an array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

How do you compare arrays, and returns the matches (compare values only)?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

How do you compare arrays and returns the matches (compare keys and values)?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

How do you compare arrays, and return the matches (compare keys only)?

A

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 )

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

How do you compare arrays, and return the matches (compare keys and values, using a user-defined key comparison function)?

A

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
Q

How do you compare arrays, and return the matches (compare keys only, using a user-defined key comparison function)

A

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
Q

How do you check if the specified key exists in the array?

A

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
Q

How do you return all the keys of an array?

A

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
Q

How do you send each value of an array to a user-made function, which returns new values?

A

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
Q

How do you merge one or more arrays into one array?

A

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
Q

How do you merge one or more arrays into one array recursively

A

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
Q

How do you insert a specified number of items, with a specified value, to an array?

A

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
Q

How do you calculate the product of the values in an array?

A

array_product(array)

EXAMPLE:

$a=array(5,5);
echo(array_product($a));

OUTPUT:

25

48
Q

How do you insert one or more elements to the end of an array?

A

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
Q

How do you return one or more random keys from an array?

A

array_rand(array,number)

EXAMPLE:

$a=array("red","green","blue","yellow","brown");
$random_keys=array_rand($a,3);
echo $a[$random_keys[0]]."<br>";
echo $a[$random_keys[1]]."<br>";
echo $a[$random_keys[2]];

OUTPUT:

green
yellow
brown

50
Q

How do you return an array as a string, using a user-defined function?

A

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
Q

How do you replace the values of the first array with the values from following arrays?

A

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
Q

How do you replace the values of the first array with the values from following arrays recursively?

A

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
Q

How do you return an array in the reverse order?

A

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
Q

How do you search an array for a given value and return the key?

A

array_search(value,array,strict)

EXAMPLE:

$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);

OUTPUT:

a

55
Q

How do you remove the first element from an array, and return the value of the removed element?

A

array_shift(array)

EXAMPLE:

$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a)."<br>";
print_r ($a);

OUPUT:

red
Array ( [b] => green [c] => blue )

56
Q

How do you return selected parts of an array?

A

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
Q

How do you remove AND replace specified elements of an array?

A

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
Q

How do you return the sum of the values in an array?

A

array_sum(array)

EXAMPLE:

$a=array(5,15,25);
echo array_sum($a);

OUTPUT:

45

59
Q

How do you compare arrays, and return the differences (compare values only, using a user-defined key comparison function)?

A

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
Q

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)?

A

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
Q

How do you compare arrays, and return the differences (compare keys and values, using two user-defined key comparison functions)

A

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
Q

How do you compare arrays, and return the matches (compare values only, using a user-defined key comparison function)

A

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
Q

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)?

A

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
Q

How do you compare arrays, and return the matches (compare keys and values, using two user-defined key comparison functions)?

A

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
Q

Removes duplicate values from an array

A

array_unique(array)

EXAMPLE:

$a=array(“a”=>”red”,”b”=>”green”,”c”=>”red”);
print_r(array_unique($a));

OUPUT:

Array ( [a] => red [b] => green )

66
Q

Adds one or more elements to the beginning of an array

A

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
Q

Returns all the values of an array

A

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
Q

Applies a user function to every member of an array

A

array_walk(array,myfunction,parameter…)

EXAMPLE:

function myfunction($value,$key)
{
echo "The key $key has the value $value<br>";
}
$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
Q

Applies a user function recursively to every member of an array

A

array_walk_recursive(array,myfunction,parameter…)

EXAMPLE:

function myfunction($value,$key)
{
echo "The key $key has the value $value<br>";
}
$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
Q

Sorts an associative array in descending order, according to the value

A

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 "<br>";
    }

OUPUT:

Key=Joe, Value=43
Key=Ben, Value=37
Key=Peter, Value=35

71
Q

Sorts an associative array in ascending order, according to the value

A

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 "<br>";
    }

OUPUT:

Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43

72
Q

Create array containing variables and their values

A

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
Q

Returns the number of elements in an array

A

count(array,mode);

EXAMPLE:

$cars=array("Volvo","BMW","Toyota");
echo count($cars);

OUPUT:

3

74
Q

Returns the current element in an array

A

current(array)

EXAMPLE:

$people = array(“Peter”, “Joe”, “Glenn”, “Cleveland”);

echo current($people) . “<br></br>”;

OUPUT:

Peter

75
Q

Returns the current key and value pair from an array

A

each(array)

EXAMPLE:

$people = array(“Peter”, “Joe”, “Glenn”, “Cleveland”);
print_r (each($people));

OUPUT:

Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 )

76
Q

Sets the internal pointer of an array to its last element

A

end(array)

EXAMPLE:

$people = array(“Peter”, “Joe”, “Glenn”, “Cleveland”);

echo current($people) . "<br>";
echo end($people);

OUPUT:

Peter
Cleveland

77
Q

Imports variables into the current symbol table from an array

A

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
Q

Checks if a specified value exists in an array

A

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
Q

Fetches a key from an array

A

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
Q

Sorts an associative array in descending order, according to the key

A

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 "<br>";
    }

OUPUT:

Key=Peter, Value=35
Key=Joe, Value=43
Key=Ben, Value=37

81
Q

Sorts an associative array in ascending order, according to the key

A

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 "<br>";
    }

OUPUT:

Key=Ben, Value=37
Key=Joe, Value=43
Key=Peter, Value=35

82
Q

Assigns variables as if they were an array

A

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
Q

Sorts an array using a case insensitive “natural order” algorithm

A

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 “<br></br>”;

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
Q

Sorts an array using a “natural order” algorithm

A

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 “<br></br>”;

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
Q

Advance the internal array pointer of an array

A

next(array)

EXAMPLE:

$people = array(“Peter”, “Joe”, “Glenn”, “Cleveland”);

echo current($people) . "<br>";
echo next($people);

OUPUT:

Peter
Joe

86
Q

Alias of current()

A

pos(array)

EXAMPLE:

$people = array(“Peter”, “Joe”, “Glenn”, “Cleveland”);

echo pos($people) . “<br></br>”;

OUPUT:

Peter

87
Q

Rewinds the internal array pointer

A

prev(array)

EXAMPLE:

$people = array(“Peter”, “Joe”, “Glenn”, “Cleveland”);

echo current($people) . "<br>";
echo next($people) . "<br>";
echo prev($people);

OUPUT:

Peter
Joe
Peter

88
Q

Creates an array containing a range of elements

A

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
Q

Sets the internal pointer of an array to its first element

A

reset(array)

EXAMPLE:

$people = array(“Peter”, “Joe”, “Glenn”, “Cleveland”);

echo current($people) . "<br>";
echo next($people) . "<br>";

echo reset($people);

OUPUT:

Peter
Joe
Peter

90
Q

Sorts an indexed array in descending order

A

rsort(array,sortingtype);

EXAMPLE:

$cars=array(“Volvo”,”BMW”,”Toyota”);
rsort($cars);

$clength=count($cars);
for($x=0;$x”;
}

OUPUT:

Volvo
Toyota
BMW

91
Q

Shuffles an array

A

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
Q

Alias of count()

A

sizeof(array,mode);

EXAMPLE:

$cars=array("Volvo","BMW","Toyota");
echo sizeof($cars);

OUPUT:

3

93
Q

Sorts an indexed array in ascending order

A

sort(array,sortingtype);

EXAMPLE:

$cars=array(“Volvo”,”BMW”,”Toyota”);
sort($cars);

$clength=count($cars);
for($x=0;$x”;
}

OUPUT:

BMW
Toyota
Volvo

94
Q

Sorts an array by values using a user-defined comparison function

A

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 "<br>";
    }

OUPUT:

Key=b, Value=2
Key=a, Value=4
Key=d, Value=6
Key=c, Value=8

95
Q

Sorts an array by keys using a user-defined comparison function

A

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 "<br>";
    }

OUPUT:

Key=a, Value=4
Key=b, Value=2
Key=c, Value=8
Key=d, Value=6

96
Q

Sorts an array using a user-defined comparison function

A

usort(array,myfunction);

EXAMPLE:

function my_sort($a,$b)
{
if ($a==$b) return 0;
   return ($a";
   }

OUPUT:

2
4
6
8