PinoyTech.org

CodeIgniter, Kohana, Mootools, jQuery and CSS

Array Combine in PHP 4

Posted by teejay on May 27, 2009

Coding for PHP 4 is like coding for Internet Explorer 6. You hate it but some people still use it. There are also some things that just don't work.

Anyway, array_combine is one function that isn't available in PHP 4 but available in PHP 5.

But since I do need my code to be PHP 4 compatible, I needed to emulate it. Luckily, I did not need to peruse a lot of web pages as I got what I was looking for here.

function array_combine($array1, $array2)
{
    while ($key = each($array1) AND $value = each($array2))
    {
        $options[$key['value']] = $value['value'];
    }
    return $options;
}

A simple usage sample should be exactly how you use array_combine

<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>

Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
)

It can't get any sweeter than that.

Categories: How To, Web Development

Tags: php

No Comments

Comments