0

How can I convert this flat indexed array:

Array (
    [0] => fruit
    [1] => apple
    [2] => vegetable
    [3] => corn
    ... etc
)

into this associative array?

Array (
    [fruit] => apple
    [vegetable] => corn
    ... etc
)

Theoretically I'd need to set each even items as keys and every odd items as the values.

3
  • 1
    and what would you do if you got an odd number of elements ? Commented Sep 10, 2013 at 14:45
  • There're some problems that don't need heavy use of native language function use and this is one of them: 3v4l.org/ta9gE Commented Sep 10, 2013 at 14:50
  • @alfasin The array is only a 2 field CSV, and have some evaluations to make sure its correct format and have both fields. Commented Sep 10, 2013 at 14:55

4 Answers 4

4

Loop through the array, incrementing the counter by 2, and then create the new array.

$newArray = array();

$len = count($oldArray);
for($i = 0; $i < $len; $i+=2){
    $key = $oldArray[$i];
    $val = $i+1 < $len ? $oldArray[$i+1] : '';

    $newArray[$key] = $val;
}
Sign up to request clarification or add additional context in comments.

7 Comments

this will give a notice when given array length is not even.
@developerCK: I know, I was assuming his data was in the "correct" format.
but this might help isset($oldArray[$i+1])?$oldArray[$i+1]:'';
@developerCK This would get executed on each iteration, checking the array before seems to be a little bit faster for huge arrays: /* remove last item from odd arrays */ if( count( $input ) % 2 != 0 ) { array_pop( $input ); }
@developerCK: That's up to him. He could pop it, then add it back at the end.
|
3

Just for the joy of playing around...

<?php

$arr = array('fruit', 'apple', 'vegetable', 'corn');

$i = $j = 0;
$newArr = array_combine(
    array_filter($arr, function()use(&$i){ return ++$i%2; }),
    array_filter($arr, function()use(&$j){ return $j++%2; })
);
unset($i, $j, $arr);

print_r($newArr);

working example

Comments

3

Just loop over the input array and increment the counter on each iteration by two:

$input = array(
    0 => 'fruit',
    1 => 'apple',
    2 => 'vegetable',
    3 => 'corn',
);

/* remove last item from odd arrays */
if( count( $input ) % 2 != 0 )
{
    array_pop( $input );
}

$output = array();
$inputLength = count( $input );

for( $i = 0; $i < $inputLength; $i = $i + 2 )
{
    $output[$input[$i]] = $input[$i + 1];
}

var_dump( $output );

Comments

0

For an elegant, concise functional-style solution, use array_chunk() to make pairs of values, then extract the second column as values and the first column as keys. (Demo)

$array = ['fruit', 'apple', 'vegetable', 'corn', 'meat', 'pork'];

var_export(
    array_column(array_chunk($array, 2), 1, 0)
);

Output:

array (
  'fruit' => 'apple',
  'vegetable' => 'corn',
  'meat' => 'pork',
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.