-1

I have an array like so:

Array
(
    [0] => 2
    [1] => 8
)

I want change my array like this:

Array
(
    ['present'] => 2
    ['absent'] => 8
)
2
  • Whats the basis? if index 2 = 'present'? 1 = 'absent'? Commented Nov 24, 2017 at 7:05
  • 5
    Possible duplicate of PHP: Change Array Integer Index to Key String Commented Nov 24, 2017 at 7:44

2 Answers 2

1

Simple as this

$array = array(2 => 2, 1 => 8);

$output['present'] = $array[2];
$output['absent'] = $array[1];

print_r($output);

If it's a multi-dimensional array of absents and presents

$array = array(
    array(2 => 2, 1 => 8),
    array(2 => 3, 1 => 7)
);

foreach ($array as $value) {
   $output[] = array('present' => $value[2], 'absent' => $value[1]);
}

print_r($output);
Sign up to request clarification or add additional context in comments.

2 Comments

What if the first array has 1000 records ? Try to make it reusable Code..
Thanks its working...
0
$res = array_combine(array('present', 'absent'), 
                array_values(array(2 => 2, 1 => 8)));
print_r($res);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.