Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is what I want to do.

I have an array.

$arr = array('value1','value2','value3','value4','value5','value6');

Is it possible to pair every two values into something like:

$new_arr = array('value1' => 'value2','value3' => 'value4', 'value5' => 'value6');

In the first array, there are no keys. They are all values. But I want to pair them..in the same order every key => value (the next to it..just like the example above)

Is something like that possible? I badly need it..

share|improve this question
    
I.e. you're looking for the php equivalent of perl's %hash = ('value1' , 'value2', 'value3', 'value4'); –  VolkerK Sep 24 '10 at 7:55

5 Answers 5

up vote 2 down vote accepted

Assuming the array has even number of members you can do:

for($i=0 ; $i<count($arr)-1 ; $i+=2) {
   $new_array[$arr[$i]] = $arr[$i+1];
}

Where $arr is your existing array and $new_array is the new resultant associative array.

Working Ideone link

share|improve this answer
    
This seems to work perfectly. And yes the array always has even number of members. It will never be odd number. –  Ahmad Fouad Sep 24 '10 at 8:02

This might do the trick:

$res = array();

for ($i = 0; $i + 1 < count($arr); $i = $i + 2) {
    $res[$arr[$i]] = $arr[$i + 1];
}

share|improve this answer
    
This will not work for associative arrays. –  tdammers Sep 24 '10 at 7:53
1  
And it will generate an error when the array length is not even. –  Lekensteyn Sep 24 '10 at 7:55
    
As previously stated by the original author, the length is always even. –  mbanzon Sep 24 '10 at 8:04
    
The array length is always even.. it will never be odd –  Ahmad Fouad Sep 24 '10 at 8:05
    
The example given didn't show an assosiative array - it wouldn't make sense to use this approach on such arrays. –  mbanzon Sep 24 '10 at 8:05

Try something like this:

$new_arr = array();
for ($i = 0; $i < count($arr); $i += 2) {
    $new_arr[$arr[$i]] = $arr[$i + 1];
}

Note that the value indexed by the last key is undefined if $arr contains an odd number of items.

share|improve this answer
    
This, too, won't work for associative arrays. –  tdammers Sep 24 '10 at 7:55
1  
@tdammers: The OP did not specify an associative array. This small chunk of code does exactly what he asked for. –  elusive Sep 24 '10 at 7:58
    
it works perfectly... yes it does just what i want. simple is better. –  Ahmad Fouad Sep 24 '10 at 8:04
    
@elusive: the requirements just specify an array. If you give it an associative or sparse array, it will run without warning, but it will return unexpected results. This is the kind of bug that can take hours to find, because you don't have an exception or error dump that tells you where exactly things go wrong - you'll just end up with inexplicable results, and you'll have to dig through large amounts of code to find the flaw. –  tdammers Sep 24 '10 at 8:18
    
@tdammers: Using an associative array just would not make any sense here. I do not know how you debug your code, but i do not think that it is difficult to tell if your output is where you want it to be, and where it got lost. Some simple logging-statements will do the trick. –  elusive Sep 24 '10 at 8:24

Of course it's possible.

function array_pair($arr) {
    $retval = array();
    foreach ($arr as $a) {
        if (isset($key)) {
            $retval[$key] = $a;
            unset($key);
        }
        else {
            $key = $a;
        }
    }
    return $retval;
}

Or you could do:

function array_pair($arr) {
    $retval = array();
    $values = array_values($arr);
    for ($i = 0; $i < count($values); $i += 2)
         $retval[$values[$i]] = $values[$i + 1];
    return $retval;
}
share|improve this answer

An approach with odd / even indices.

$new_arr = array();
$key = NULL;
for($i=0; $i<count($arr); $i++){
   if($i % 2){ // even indices are values, store it in $new_arr using $key
      $new_arr[ $key ] = $arr[$i];
   }
   else{ // odd indices are keys, store them for future usage
      $key = $arr[$i];
   }
}

Note: the last value will be ignored if the array length is odd.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.