0

I have print_r result like this :

Array
(
    [0] => A, B, C, D
    [1] => 15,20,24,19
)

how to make them like this :

Array
(
    [A] => 15
    [B] => 20
    [C] => 24
    [D] => 19
)

Great thanks for help :)

1

4 Answers 4

1

Try this:

$a = array('A', 'B', 'C', 'D');
$b = array(15, 20, 24, 19);
$c = array();

foreach ($a as $index => $value) {
    if (isset($b[$index])) {
        $c[$value] = $b[$index];
    }
}

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

Comments

0

You need explode() and array_combine(). Assuming your initial array, $arr:

$new_arr = array_combine(explode(', ', $arr[0]), explode(',', $arr[1]));

See demo

1 Comment

Mark, how if the array is result of loop process, such result of for / while .. then [0] and [1] is automatically ..
0

Try to explode() your array index by comma and combine both array with keys and values using array_combine()

$a = explode(',',$arr[0]);
$b = explode(',',$arr[1]);
$new = array_combine($a,$b);
print_r($new); //Array ( [A] => 15 [ B] => 20 [ C] => 24 [ D] => 19 ) 

Comments

0

array_combine is the way

<?php

$myArray = array(
    array('A', 'B', 'C', 'D'),
    array(15, 20, 24, 19)
);

$combinedArray = array_combine($myArray[0], $myArray[1]);

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.