[ create a new paste ] login | about

Link: http://codepad.org/UAo9qNwo    [ raw code | output | fork ]

PHP, pasted on Oct 19:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$testArray = array ( 'user' => array ( 'name' => 'John', 'email' => 'test@example.org', 'prefs' => array ( 0 => '1', ), ), 'other' => array ( 'example' => array ( 'var' => 'foo', ), ), );

function toPlain($in,$track=null)
{
    $ret = array();
    foreach ($in as $k => $v) {
        $encappedKey = $track ? "[$k]" : $k; /* If it's a root */
        
        if (is_array($v)) $ret = array_merge($ret,toPlain($v,$track.$encappedKey));
        else $ret = array_merge($ret,array($track.$encappedKey => $v));
    }
    return $ret;
}
print_r(toPlain($testArray));


Output:
1
2
3
4
5
6
7
Array
(
    [user[name]] => John
    [user[email]] => test@example.org
    [user[prefs][0]] => 1
    [other[example][var]] => foo
)


Create a new paste based on this one


Comments: