0

I am looking for a function that will convert this array:

array(1) {
  ["a"]=>
  array(2) {
    ["b"]=>
    int(1)
    ["c"]=>
    string(5) "hello"
  }
}

into something like:

array(2) {
  ["a[b]"]=>
  int(1)
  ["a[c]"]=>
  string(5) "hello"
}

I.e. to "serialize" structured array so that I can easily put it to HTML form as hidden fields and when I read it back from the $_POST, I get exactly the same structure! Is there any PHP built-in function for that?

7
  • 2
    Nope, while PHP is pretty rich in functions for most things, that you'll have to code yourself Commented Apr 15, 2013 at 11:13
  • Isn't json_encode an option? Commented Apr 15, 2013 at 11:13
  • Thanks @Mark.. are there any similar useful functions that can help in the task? Commented Apr 15, 2013 at 11:15
  • Something like a solution from stackoverflow.com/questions/10849021/… ? Commented Apr 15, 2013 at 11:15
  • Or using serialize/unserialize with just one input field – if you don’t need the data client-side for access to the single values. Or, if you don’t need it client-side, why not put it into the session in the first place and avoid the round-trip to the client? Commented Apr 15, 2013 at 11:16

2 Answers 2

3

You would need to flatten it yourself. There's in no built-in one-line function that could do that.

function flattenArr($arr, $key, &$result) {
    foreach ($arr as $k => $v) {
        if (is_array($v)) {
            flattenArr($v, $key . "[$k]", $result);
        } else {
            $result[$key . "[$k]"] = $v;
        }
    }
}

$obj = array("a" => array("b" => "c", "d" => array("1"=>array(1,2,3,5),"2", "3")));
$result = array();
flattenArr($obj, "", $result);
foreach ($result as $k => $v) {
    echo "result$k = $v\n";
}

OUTPUTS:

result[a][b] = c
result[a][d][1][0] = 1
result[a][d][1][1] = 2
result[a][d][1][2] = 3
result[a][d][1][3] = 5
result[a][d][2] = 2
result[a][d][3] = 3

This might not work out the way it is now, you will probably have to modify it a bit. HTH

Sign up to request clarification or add additional context in comments.

8 Comments

What happens with objects ?
The answer was just to give the OP an idea. If OP wants to deal with objects, he will have to induce relevant changes.
@Baba Out of curiosity, when/where were objects mentioned in the question?
@Jon the OP does not know it yet ... he would be back when he realize how json has made his live easier
@Tomas asked for the first level array not to set [].
|
2
$arr = array(
        "a"=> array( "b"=>1, "c"=>"hello", "d"=> array( "e"=>3, "j"=>"wow") ),
        "z"  => array( "za"=>22, "tt"=>'wos')
    );

function loop_it($array, $arr=array(), $Mkey = false){
    foreach ($array as $key => $value) {
        $_key = $Mkey ? $Mkey . "[" . $key . "]" : $key;
        if(is_array($value)){
            $arr += loop_it($value, $arr, $_key);
        } else{
            $arr[$_key] = $value;
        }
    }
    return $arr;
}

$return = loop_it($arr);    
var_dump($return);

Here is working function ..:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.