Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
2  
Nope, while PHP is pretty rich in functions for most things, that you'll have to code yourself – Mark Baker 2 days ago
Isn't json_encode an option? – Havelock 2 days ago
Thanks @Mark.. are there any similar useful functions that can help in the task? – Tomas 2 days ago
Something like a solution from stackoverflow.com/questions/10849021/… ? – Jon 2 days ago
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? – CBroe 2 days ago
show 2 more comments

2 Answers

up vote 2 down vote accepted

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

share|improve this answer
What happens with objects ? – Baba 2 days ago
The answer was just to give the OP an idea. If OP wants to deal with objects, he will have to induce relevant changes. – Thrustmaster 2 days ago
@Baba Out of curiosity, when/where were objects mentioned in the question? – Jon 2 days ago
@Jon the OP does not know it yet ... he would be back when he realize how json has made his live easier – Baba 2 days ago
1  
@Tomas asked for the first level array not to set []. – Svetlio 2 days ago
show 6 more comments
$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 ..:)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.