Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an assoc array with equal keys and values from a regular array

Tags:

arrays

php

I have an array that looks like

$numbers = array('first', 'second', 'third'); 

I want to have a function that will take this array as input and return an array that would look like:

array( 'first' => 'first', 'second' => 'second', 'third' => 'third' ) 

I wonder if it is possible to use array_walk_recursive or something similar...

like image 763
jimiyash Avatar asked Jul 01 '09 01:07

jimiyash


People also ask

Which function will create a new associative array by taking keys from one array and values from another?

Here array() function is used to create associative array.

What is associative array with example?

For example, the following statement defines an associative array a with key signature [ int, string ] and stores the integer value 456 in a location named by the tuple [ 123, "hello" ]: a[123, "hello"] = 456; The type of each object contained in the array is also fixed for all elements in a given array.

How do I create an array value as an array key in PHP?

keyN => valueN, ]; Note: The comma after the last Key-Value pair is optional. The Key can be of either integer or string type.


1 Answers

You can use the array_combine function, like so:

$numbers = array('first', 'second', 'third'); $result = array_combine($numbers, $numbers); 
like image 122
Noah Medling Avatar answered Sep 22 '22 17:09

Noah Medling