How to create string out of dynamic array keys in php?

php dynamic array key
php array
php dynamic multidimensional associative array
php array key value
associative array dynamic key php
php array same key different value
php add to associative array
php add dynamic key to array

This array is dynamic and can have any number of records and can have any level of nested array:

Array
(
    [name] => richard
    [email] => richard@gmail.com
    [address] => Array
        (
            [city] => paris
            [zip] => 12121
        )

    [address1] => Array
        (
            [city] => paris
            [zip] => 12121
        )

    [address3] => Array
        (
            [city] => paris
            [zip] => 12121

        )

)

I am trying to construct a string from nested array keys. I need to construct string like this from keys using loop:

Result should be like:

 address.city
   address.zip
   address1.city
   address1.zip....so on

How can it be achieved using recursion ?

This recursive function will do what you want. It checks each item at the current level of the array, and if it is an array, appends the current key to all the keys of that array, recursing as deep as necessary. If the item isn't an array, it's key is simply appended to the output for that level.

function list_keys($array) {
    $output = array();
    foreach ($array as $k => $v) {
        if (is_array($v)) {
            foreach (list_keys($v) as $path) {
                $output[] = "$k.$path";
            }
        }
        else {
            $output[] = $k;
        }
    }
    return $output;
}

print_r(list_keys($array));

Output (for your sample data)

Array (
    [0] => name
    [1] => email
    [2] => address.city
    [3] => address.zip
    [4] => address1.city
    [5] => address1.zip
    [6] => address3.city
    [7] => address3.zip 
)

Demo on 3v4l.org

array_fill_keys - Manual, Safe Mode � Command line usage � Garbage Collection � DTrace Dynamic Tracing array_fill_keys — Fill an array with values, specifying keys array_combine() - Creates an array by using one array for keys and another for its values now string key "1" become an integer value 1, be careful. The output will be. Array� array_keys (array $array, mixed $search_value [, bool $strict = FALSE ]) : array array_keys () returns the keys, numeric and string, from the array. If a search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

To get this result, you just have to loop over the array - and if the items is an array again, loop over the inner item as well.

This could be done like this:

$str = '';
foreach ($array as $key => $item) {
    if (is_array($item)) {
        foreach ($item as $other_key => $dummy) {
            $str .= ($str ? "\n" : '') . $key . '.' . $other_key;
        }
    }
}

echo $str;

array_combine - Manual, Mode � Command line usage � Garbage Collection � DTrace Dynamic Tracing array_combine — Creates an array by using one array for keys and another for keys. Array of keys to be used. Illegal values for key will be converted to string. the array_combine around, to where the vars are the keys, and this will output� Parameters. keys. Array of values that will be used as keys. Illegal values for key will be converted to string.. value. Value to use for filling

function func($arr, $prefix = '') : void {
    $prefix = $prefix!='' ? "$prefix." : "";
    foreach ($arr as $key => $value) {
        if (!is_array($value)) {
            echo "$prefix$key\n";
        }
        else {
            func($value, "$prefix$key");
        }
    }
}

func($theArrayInTheQuestion);

array_keys - Manual, array_keys() returns the keys, numeric and string, from the array . array. This created an issue with in_array and doing a lookup on characters from a string. In general PHP arrays are maps, which mean that it is a type that maps values tokens. In our example it means that where the key is 0 there the value is "red" and where the key is 1 there the value is "green". However you have the possibility to use more meaningful keys. Associative array means that you can assign an arbitrary key to every value.

How to create dynamic Arrays and display them using PHP, In this article we will see how we can create dynamic arrays in PHP, display the These functions display that array as key-value pairs. It means that 'a' differs from 'A' but '1' is the same as 1. And you can display the content similar to the normal array, but here you need to use the string value instead of the number. In PHP, the implode () method joins array elements and outputs them as a single string. This is useful when you need to create one string out of a set of array values. The function can be invoked in two ways (with or without the glue string): implode (string $glue, array $pieces) – The glue is used to combine the array pieces.

How to create a dynamic array key path? - PHP, I have a string var that get's built with a string something like: $selection = "[ selection1][selection2]"; selection1 and selection2 can really be any� There's a lot of multidimensional array_keys function out there, but each of them only merges all the keys in one flat array. Here's a way to find all the keys from a multidimensional array while keeping the array structure.

Creating dynamic array and set keys, You can do this using the twig attribute function: {% set data = [ {'column':'1','title':' Quick Facts'}, {'column':'2','title':'Inspiration'}, {'column':'1','title':'Agencies'},� Arrays. An array in PHP is actually an ordered map. A map is a type that associates values to keys.This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.

Comments
  • What about name and email keys? They're not listed in your result.
  • this maybe work, but only into 1st and 2nd level of array. Not recursion
  • Check the last sentence in the question