Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert array first value as key and second value as value

Tags:

arrays

php

Hi I am working on some array operations.

I need to convert first value of array as key and second value of array as value.

I have one variable $testArray which stores array like below.

 Array
(
    [0] => Array
        (
            [0] => Color
            [1] => White on Red
        )

    [1] => Array
        (
            [0] => Depicted Text
            [1] => EMPTY
        )

    [2] => Array
        (
            [0] => Depth [Nom]
            [1] => 0.004 in
        )

    [3] => Array
        (
            [0] => Language
            [1] => English
        )

    [4] => Array
        (
            [0] => Length [Nom]
            [1] => 10 in
        )

    [5] => Array
        (
            [0] => Material
            [1] => Adhesive Vinyl
        )

    [6] => Array
        (
            [0] => Mounting
            [1] => Surface
        )

    [7] => Array
        (
            [0] => Width [Nom]
            [1] => 14 in
        )

    [8] => Array
        (
            [0] => Wt.
            [1] => 0.056 lb
        )

)

Expected output :

    Array
(
    [0] => Array
        (
            [Color] => White on Red
        )

    [1] => Array
        (
            [Depicted Text] => EMPTY
        )

    [2] => Array
        (
            [Depth [Nom]] => 0.004 in
        )

    [3] => Array
        (
            [Language] => English
        )

    [4] => Array
        (
            [Length [Nom]] => 10 in
        )

    [5] => Array
        (
            [Material] => Adhesive Vinyl
        )

    [6] => Array
        (
            [Mounting] => Surface
        )

    [7] => Array
        (
            [Width [Nom]] => 14 in
        )

    [8] => Array
        (
            [Wt.] => 0.056 lb
        )

)

I have already tried with array function array_keys and array_values but it won't working

like image 343
Manthan Dave Avatar asked Apr 22 '17 13:04

Manthan Dave


People also ask

How do you find the key and value of an array?

The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

What is the use of array_flip () function?

The array_flip() function flips/exchanges all keys with their associated values in an array.

Can I use an array as a key in JavaScript?

To use an array as object keys in JavaScript, we can convert the array to a string. to create the key array. Then we convert it to a string with JSON. stringify so we can use it as a property key.

How to convert an object to an array[] of key-value pairs?

We can convert an Object {} to an Array [] of key-value pairs using methods discussed below: Method 1: In this method, we will use Object.keys () and map () to achieve this. Approach: By using Object.keys (), we are extracting keys from the Object then this key passed to map () function which maps the key and corresponding value as an array, ...

How do I convert an array to an object in Java?

To convert an array's values to object keys, use the forEach () method. First, create an empty object that will store array values as the keys. Call the forEach () method to loop through the array. On each iteration, use the array element as a key of the object and assign an empty string ("") to it.

How to get the same value from another array in JavaScript?

To get the same value from another array and insert it into an object of the array we need to. Before jumping into the code, you can read the following articles. It will help you to understand it better, Method 1: Using forEach () and push (), includes () method of array. Method 2: filter (), push () and includes () of array.

How do you add a key to an array in Python?

let emp: [Int: String] = [1: "john", 2: "franc", 3: "andrew"] print (emp) var keyArray=Array(emp.keys); print (keyArray) You can also use for -in the loop to iterate and add keys to an array. First, create an Array int for holding key values.


2 Answers

Simple solution using array_map function:

$result = array_map(function($v){
    return [$v[0] => $v[1]];
}, $testArray);
like image 154
RomanPerekhrest Avatar answered Oct 24 '22 11:10

RomanPerekhrest


Assuming that structure will always be the same, you could do this:

$output = array();
foreach($testArray as $v){
    $output[] = array($v[0] => $v[1]);
}

See it in action here.

like image 5
FirstOne Avatar answered Oct 24 '22 11:10

FirstOne