Memory App
Programming
 



Programming > Php > Array > Operations   ZCE 5.3  |  Patterns  |  Modern Php

Checking

Is_array() is used to check if an array exists (don't use count()).

$a = array(1,2,3);
$b = array();
$c = 2;

echo count($a); // Output: 3
echo count($b); // Output: 0
echo count($c); // Output: 1

echo is_array($c); // False
Array_key_exists() is used to check if a key exists (don't use not isset()).

$a = array ('a' => NULL, 'b' => 2);
    
echo isset ($a['a']); // False
echo array_key_exists('a', $a); // True
(1/5)

Pointer

Array_reverse() inverse the order of the arrray's elements.

$a = array('a', 'b');
$b = array_reverse($a);

print_r($b); // [0] => b [1] => a
Reset(), Key(), Next(), Current() are used when working with internal array pointer.

$arr = array('a' => 1, 'b' => 2, 'c' => 3);
    
reset($arr);

while (key($arr) !== NULL) {
    echo key($arr) . current($arr) . " ";
    next($arr);
}

// Output: a1 b2 c3
End() - You could start your iteration from the last element.

$arr = array('a' => 1, 'b' => 2, 'c' => 3);
    
end($arr);  // Look Here

while (key($arr) !== NULL) {
    echo key($arr) . current($arr) . " ";
    prev($arr);
}

// Output: c3 b2 a1
With PHP 5 the value of an array can be change by reference.

$a = array (1, 2, 3);

foreach ($a as $k => &$v) { // Look Here
    $v *= 2; 
}

var_dump ($a);  // array(3) { [0]=> int(2) [1]=> int(4) [2]=> &int(6) }  
(2/5)

Mapping

array_map() - Same as array_walk (which needs reference)

function cube($n) {
    return pow($n, 3);
}
$a = array(1,2,3);
$b = array_map('cube', $a);

print_r($b); // Array ( [0] => 1 [1] => 8 [2] => 27 ) 

$result = array_map(
    create_function('$x', 'return pow($x, 3);'), 
    array(1,2,3)
);

print_r($result); // [ 1, 8, 27 ]
Array_walk() - Apply a user function to every member of an array.

$arr = array(1,2,3,4);

function setDouble($value) 
{ 
    echo $value * 2;
}

array_walk($arr, 'setDouble'); // output 2468
Common use is passing value by reference in function definition.

$arr = array(1,2,3,4);

function setDouble(&$value) 
{ 
    $value *= 2; // Look Here
}

array_walk($arr, 'setDouble');

print_r($arr); // [0] => 2 [1] => 4 [2] => 6 [3] => 8
(3/5)

Sorting

Sort(), Rsort() destroys all the keys and rearange elements.
    
$arr = array('a'=>'foo', 'b'=>'bar', 'c'=>'baz');

sort($arr);

print_r($arr); //  [0] => bar [1] => baz [2] => foo 
Natsort() maintans all the key-value associations.

// unlike sort(), maintain all the key-value associations in the array

$arr = array('10t', '2t', '3t');

natsort($arr);

print_r($arr); // [1] => 2t [2] => 3t [0] => 10t
Asort(), Arsort() are used in case of associative array.

$arr = array('a'=>'foo', 'b'=>'bar', 'c'=>'baz');

asort($arr);

print_r($arr); // [b] => bar [c] => baz [a] => foo 
Ksort(), Krsort() sorts by key.

$arr = array('a'=>'foo', 'c'=>'bar', 'b'=>'baz');

ksort($arr);

print_r($arr); // ( [a] => foo [b] => baz [c] => bar
Usort(), Uasort() sorts by custom rules.

$arr = array('three', '2two', 'one', 'two');

function myCmp($left, $right) 
{
    if ($diff = strlen($left) - strlen($right)) {
        return $diff;
    }
    return strcmp($left, $right);
}

usort($arr, 'myCmp'); // Look Here

print_r($arr); // [0] => one [1] => two [2] => 2two [3] => three
(4/5)

Stacks

Array_push(), Array_pop() - add at end / extract last

// add two elements to array (array_push)

$arr = array();

array_push($arr, 'bar', 'baz');

print_r($arr); // [0] => bar [1] => baz

// extract the last element added to the array (array_pop)

$lastIn = array_pop($arr);

var_dump($lastIn, $arr); // string(3) "baz" array(1) { [0]=> string(3) "bar" }

$arr[] = 'foo';

// when only one value if being pushed
// faster than array_push (no function call)
Array_shift(), Array_unshift() - extract first, add to begining
    
// push the first element out of the array (array_shift)

$arr = array('bar', 'baz');
$firstElement = array_shift($arr);
print_r($arr); // [0] => baz

// add a value to the beginning of the array (array_unshift)

array_unshift($arr, 'foo');
print_r($arr); // [0] => foo [1] => baz
Array_diff() - keeps only diff value

$a = array (1, 2, 3);

$b = array (1, 3, 4);

print_r(array_diff ($a, $b)); // [1] => 2 )
Array_intersect() - keeps only identical values

$a = array (1, 2, 3);

$b = array (1, 3, 4);

print_r(array_intersect ($a, $b)); // [0] => 1 [2] => 3
String to array

$str = "111';

$arr = str_split($str);


Questions



Top Reference > Programming
0/0 (80)  
Not Logged