4

I have an array of data like so

[
 'one', 'two', 'three'
]

I need to convert it like so

[
  'one' => 'one',
  'two' => 'two'
]

I found out about array_flip which gives me

[
   'one' => 0,
   'two' => 0,
   'three' => 0
]

What can I do from there? Any clean PHP way to do this?

0

4 Answers 4

8

array_combine() is the way to go

$a = array('one', 'two', 'three');
$output = array_combine($a, $a);
Sign up to request clarification or add additional context in comments.

Comments

5

use array_combine()

array_combine — Creates an array by using one array for keys and another for its values

$a = array('one', 'two', 'three');
$a = array_combine($a, $a);

Comments

2

Just use array_combine() with the same array used for the keys and the values:

$array = [
 'one', 'two', 'three'
];

$new_array = array_combine($array , $array);

Demo

Comments

0

Try this code

<?php
     $arr = array('one', 'two', 'three');
     $result = array();
     foreach ($arr as $value) {
         $result[$value] = $value;
     }
     print_r($result);
?>

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.