How to create an array of arrays containing key-value pairs from an input string?

convert array to key value pair javascript
convert object to array of objects javascript
convert object to array typescript
convert object to key value pair javascript
javascript object to array of values
object to array javascript
php create array with key and value
javascript key, value map

I am developing a search bar in php, but can't write a regex expression that can do what I require.

I have an input string like this:

$text = "key1=value 1 key2=value 2 key3=val 3";

I would like to be able to split into a multi-dimensional array with this structure:

$array = [["key1", "value 1"], ["key2", "value 2"], ["key3", "val 3"]];

How can this be done with a regex pattern in php?

Here are two ways to generate your desired multidimensional array of key-value pairs with preg_split()...


Approach #1: Split on = or space followed by 1 or more word characters then a =

var_export(array_chunk(preg_split('~=| (?=\w+=)~', $text), 2));

Approach #2: Split on each key, include leading&training characters in match, retain only keys & values

var_export(array_chunk(preg_split('~ ?(\w+)=~', $text, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE), 2));

Output: (from either)

array (
  0 => 
  array (
    0 => 'key1',
    1 => 'value 1',
  1 => 
  array (
    0 => 'key2',
    1 => 'value 2',
  2 => 
  array (
    0 => 'key3',
    1 => 'val 3',

preg_split() can only return a single dimensional array (in this case, key, val, key, val, ...), so array_chunk() is used to generate the desired two-elements-per-subarray structure.

Convert JavaScript array of arrays into object key value pairs , Convert JavaScript array of arrays into object key value pairs I have an input array let data As you can see, the first element in the first array is an empty string. I wrote variations with reduce and map , with for loops, and with just map . We accomplish this by creating thousands of videos, articles, and  Define Employee Array. Define key value pair employee object and push it into employees array. In below example we have employee keys say id , name and age with their respective values. To verify it just iterate the employee array and retrieve the each employee value with the help of keys.

Disclaimer: This is not a regex but solves the problem.

Assuming only a space separates each key/value pair this should do it

function keySplit($pair) {
    return explode("=", $pair, 2);

$array = array_map("keySplit", explode(" ", $text));

If the pairs are separated by an &, just replace the space in the explode function to an & instead.

How to create an array with key value pairs in PHP?, Hire with us! △. How to read user or console input in PHP ? The syntax for creating an Associative Array is as follows: Syntax 1: The comma after the last Key-Value pair is optional. If the type of Key is other that string or integer, it will be cast to string or integer depending on the type of Key. Associative Arrays in PHP. You can implement pairs with tuples, which can be created by separating elements with commas, usually with parenthesis enclosing them for better readability. For the vectors, you can use lists which allow you to remove and add elements to them as you see fit.

function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);

function str_to_keyvalue($text, $separator="="){
    if(strpos($text, $separator)>0){
        $re = '/(\w+)'.$separator.'/';
        preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0);
        for($b=0; $b<count($matches); $b++){
                $beginPos=stripos($text, $matches[$b][0])+strlen($matches[$b][0]);
                array_push($tmpArray, $matches[$b][1],  trim(substr($text, $beginPos, $endPos)));
                array_push($tmpArray,$matches[$b][1], trim(get_string_between($text, $matches[$b][0], $matches[$b+1][0])));
            array_push($array_keyword, $tmpArray);
        array_push($array_keyword, $text);
    return $array_keyword;

How to convert an Object {} to an Array [] of key-value pairs in , The task is to convert an Object {} to an Array [] of key-value pairs using JavaScript. These objects are quite different from JavaScript's primitive data-​types(Number, String, Boolean, null, undefined and symbol). Objects are more complex and each object may contain any combination of document.write(​result[i][z] + " " );. Version 2: This version uses a single array of KeyValuePair structs. Again, the GC.GetTotalMemory method is used. Result: The version that uses 1 array of KeyValuePairs uses a few bytes less of memory. So KeyValuePair wastes no space. Tip: Performance may be affected by the KeyValuePair version,

Create a hash from an array of arrays of key|value pairs, input.each_with_object( { |(k, v), h| h[k] += v } function which folds list elements with a given function, and finally here it is. Total@##[[All, 2]] sums the second elements for each string, #[[1, 1]] (in this case, for "A", "B", and "C"). Another approach, and probably the most efficient way to store and array of double pairs is to use a single array of doubles, and use (2*i) and (2*i + 1) as your indexing scheme. Additionally you gain the advantage that the array will be initialized to all 0s when you create it, no additional steps required.

Working with Arrays, You can create an array of values by extracting pattern-based values from a each row contains an array of all of the values of the input column: You can extract the keys of an Object column into an array of string values. In an Object type, the values are listed in quoted key/value pairs and can be nested. You can index into, reshape, and concatenate strings arrays just as you can with arrays of any other type. You also can access the characters in a string and append text to strings using the plus operator. To rearrange strings within a string array, use functions such as split, join, and sort.

PHP array() Function, Create an indexed array named $cars, assign three elements to it, and then print a text Indexed arrays - Arrays with numeric index; Associative arrays - Arrays with named keys; Multidimensional arrays - Arrays key, Specifies the key (​numeric or string). value Loop through and print all the values of an indexed array:. JavaScript arrays are used to store multiple values in a single variable. An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: However, what if you want to loop through the cars and find a specific one?