Fastest way to implode an associative array with keys

后端 未结 11 1777
梦如初夏
梦如初夏 2020-12-07 19:42

I\'m looking for a fast way to turn an associative array in to a string. Typical structure would be like a URL query string but with customizable separators so I can use \'<

11条回答
  •  醉酒成梦
    2020-12-07 20:04

    As an aside, I was in search to find the best way to implode an associative array but using my own seperators etc...

    So I did this using PHP's array_walk() function to let me join an associative array into a list of parameters that could then be applied to a HTML tag....

    // Create Params Array
    $p = Array("id"=>"blar","class"=>"myclass","onclick"=>"myJavascriptFunc()");
    
    // Join Params
    array_walk($p, create_function('&$i,$k','$i=" $k=\"$i\"";'));
    $p_string = implode($p,"");
    
    // Now use $p_string for your html tag
    

    Obviously, you could stick that in your own function somehow but it gives you an idea of how you can join an associative array using your own method. Hope that helps someone :)

提交回复
热议问题