Your browser is very old. You might enjoy surfing the web more if you used something newer like:

Google Chrome

Even Firefox would be OK.

If you're being forced at gunpoint to use Internet Explorer, you should at least upgrade it. Version 8 is tolerable and 9 will be OK when it comes out.

Posts tagged “arrays”

A Caveat When Declaring Javascript Arrays

A friend at work was having a bizarre Javascript array problem yesterday. The solution seems perfectly obvious now but took a minute to figure out.

He was writing a small page the involved doing a bunch of calculations based on some options. Various numeric values to be used in those calculations were stored in a set of arrays, sort of like this:

var foo = new Array(4001, 1234, 5678);
var bar = new Array(5921);

While foo was behaving nicely

[4001, 1234, 5678]

bar kept showing up like this:

[undefined, undefined, undefined, undefined, 5917 more...]

Have you figured out the problem yet?

There are lots of different ways to declare a Javascript Array object

var a = new Array(1,2,3,4); //This contains the values 1,2,3,4

var a = new Array();
a[0] = 1;
a[1] = 2; //This array now has the values 1,2

var a = [1,2,3,4]; //Literal notation, this is the ideal way to declare an array.

var a = new Array(5);  //This is an empty array with 5 elements

See that last one? Our code for bar up above doesn’t realize we want to put the value 5921 into element 0 of the array. It thinks we wanted to create an empty array with 5921 elements!

Changing the code to this fixed the problem:

var bar = new Array(5921,0);

Saving the world with PHP array functions

I’m not to going to cover all of the PHP array functions but I thought I’d go over a few. I see a lot of code examples online that could be easily be shortened by taking advantage of these functions.

So here are a few interesting ones that might save you some work down the road.

array_map

This applies a callback function to every element of an array and returns the resulting array.

array array_map ( callback $callback , array $arr1 [, array $... ] )

For example: Applying the trim function to every element of an array.

$a = array(' a ', '   b', '   c   ');
$a = array_map('trim', $a);
print_r($a);

array_flip

This exchanges all keys with their associated values.

array array_flip ( array $trans )

$a = array( 'foo' => 1, 'bar' => 2, 'baz' => 3);
$b = array_flip($a);
print_r($b);

Array
(
[1] => foo
[2] => bar
[3] => baz
)

usort

This sorts an array by value but uses a custom function for the comparison which is handy if you have an array of objects and you want to sort by some property within those objects. According to the PHP docs, “The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.”

bool usort ( array &$array , callback $cmp_function )

(To do the same thing by keys instead of values, use uksort)

function cmp_func($a, $b) {
    if( $a->val < $b->val ) {
        return -1;
    }elseif( $a->val == $b->val ) {
        return 0;
    }elseif( $a->val > $b->val ) {
        return 1;
    }
}

class Foo {
    public $val;

    public function __construct($val) {
        $this->val = $val;
    }
}

$a = array();

$a[] = new Foo(3);
$a[] = new Foo(2);
$a[] = new Foo(1);

echo '
' . print_r($a, true) . '

';

usort($a, 'cmp_func');

echo '

' . print_r($a, true) . '

';

array_slice

Extracts a section of an array.

array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys= false ]] )

$a = array( 'a', 'b', 'c', 'd');

$b = array_slice($a, 2);

echo '
' . print_r($b,true) . '

';

Array
(
[1] => c
[2] => d
)

array_sum

This returns the sum of the values in an array.

number array_sum ( array $array )

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

echo array_sum($a);

list

Assign values from an array to individual variables.

void list ( mixed $varname [, mixed $... ] )

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

list($val1, $val2, $val3) = $a;

echo "$val1, $val2, $val3";

a, b, c