Tag Archives: Associative array

PHP: What is More Powerful Than list() – Perhaps extract()

list() in PHP

Recently I wrote about list() in PHP which is indeed very powerful when assigning variable values from array elements.

$a = array(10, array('here', 'are', 'some', 'tests'));
list($count, $list) = $a;

Actually my example in the post was not correct, because I wrote that you can pass an associative array, but the truth is that you cannot, and thus the array should be always with numeric keys. After noticing the comments of that post, and thanks to @Philip,  I searched a bit about how this problem can be overcome.

There is a Solution

As always PHP gives a perfect solution! You can see on the list() doc page that there is a function that may help you use an associative array.

Extract

extract() is perhaps less known than list(), but it does the right thing!

$a = array('count' => 10, 'list' => array('here', 'are', 'some', 'tests'));
extract($a);
 
echo $count;    // 10
print_r($list); // array('here'....

Note that now both $count and $list are defined.

PHP: preg_match Give Names to the Matches

So Called – Subpatterns

Patterns - preg_match

In PHP 5.2.2+ you can name the sub patterns returned from preg_match with a specific syntax.

Named subpatterns now accept the syntax (?<name>) and (?’name’) as well as (?P<name>). Previous versions accepted only (?P<name>)

This is extremely helpful, when dealing with long patterns. As you may know you can simply use the “old school” way and to call the matches by their number based index:

    $haystack = '01 Jan 1970';
    $pattern = '/(\d{1,2})\ (Jan|Feb)\ (19\d\d)/';
 
    preg_match($pattern, $haystack, $matches);
 
    print_r($matches);

Although it may look difficult to maintain, now you can simply name the sub patterns of preg_match and to call them with their associative array keys. This is more clear when writing code and it’s definitely more maintainable.

    $haystack = '01 Jan 1970';
    $pattern = '/(?<day>\d{1,2})\ (?<month>Jan|Feb)\ (?<year>19\d\d)/';
 
    preg_match($pattern, $haystack, $matches);
 
    print_r($matches);
 
    // now there's $matches['day'], $matches['month'] ...

JavaScript Objects Coding Style

JavaScript vs. PHP

Continuing from my post about PHP arrays coding style and following the comments of that post, I’d like to write a bit about JavaScript objects’ coding style.

You perhaps know that the term object is quite undefined or under estimated in the JavaScript world, but I’d speak about the key/value pairs in JS commonly formatted like that:

var obj = { key : 'value' }

Here you can add more and more key/value pairs, but what’s different from the PHP associative arrays and what’s the same and should be cosidered.

The Same as PHP?

I wrote about the alignment in PHP and hashes. Than I showed how I align them:

$arr = array(
   'short'   => 'val',
   'longkey' => 'val'
);

In JavaScript you should use the same technique of alignment:

var obj = {
   'short'   : 'val',
   'longkey' : 'val'
};

Some Differences

Yes, there are more differences, which is normal. First of all you don’t have the => notation in JavaScript and a : is used. Second and most important you cannot add a trailing comma after the last key/value pair. Note that in PHP that’s fine!

// that will throw an error in MSIE
var obj = {
   'short'   : 'val',
   'longkey' : 'val',
};

while this is OK in PHP and it’s encouraged:

$arr = array(
   'short'   => 'val',
   'longkey' => 'val',
);