tags:

views:

45

answers:

5

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..

+2  A: 
mbanzon
This will not work for associative arrays.
tdammers
And it will generate an error when the array length is not even.
Lekensteyn
As previously stated by the original author, the length is always even.
mbanzon
The array length is always even.. it will never be odd
Ahmad Fouad
The example given didn't show an assosiative array - it wouldn't make sense to use this approach on such arrays.
mbanzon
+2  A: 

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

codaddict
This seems to work perfectly. And yes the array always has even number of members. It will never be odd number.
Ahmad Fouad
+2  A: 

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.

elusive
This, too, won't work for associative arrays.
tdammers
@tdammers: The OP did not specify an associative array. This small chunk of code does exactly what he asked for.
elusive
it works perfectly... yes it does just what i want. simple is better.
Ahmad Fouad
@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
@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
I'm not going to argue here, it's just that PHP isn't strongly typed, nor compiled, and so provides no way to catch unexpected input for you. My point is that when the input array, for whatever reasons, is not in the correct format (i.e., it is sparse or associative), then the assumption that it can be iterated over using sequential integers from 0 to count($array)-1 leads to results that don't make any sense at all. And because your array is only passed literally in trivial examples, I stand by my opinion that these things can be dangerous in production code.
tdammers
A: 

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.

Lekensteyn
+1  A: 

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;
}
tdammers