Fastest way to implode an associative array with keys

后端 未结 11 1763
梦如初夏
梦如初夏 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 :)

    0 讨论(0)
  • 2020-12-07 20:08

    A one-liner for creating string of HTML attributes (with quotes) from a simple array:

    $attrString = str_replace("+", " ", str_replace("&", "\" ", str_replace("=", "=\"", http_build_query($attrArray)))) . "\"";
    

    Example:

    $attrArray = array("id"    => "email", 
                       "name"  => "email",
                       "type"  => "email",
                       "class" => "active large");
    
    echo str_replace("+", " ", str_replace("&", "\" ", str_replace("=", "=\"", http_build_query($attrArray)))) . "\"";
    
    // Output:
    // id="email" name="email" type="email" class="active large"
    
    0 讨论(0)
  • 2020-12-07 20:13

    This is my solution for example for an div data-attributes:

    <?
    
    $attributes = array(
        'data-href'   => 'http://example.com',
        'data-width'  => '300',
        'data-height' => '250',
        'data-type'   => 'cover',
    );
    
    $dataAttributes = array_map(function($value, $key) {
        return $key.'="'.$value.'"';
    }, array_values($attributes), array_keys($attributes));
    
    $dataAttributes = implode(' ', $dataAttributes);
    
    ?>
    
    <div class="image-box" <?= $dataAttributes; ?> >
        <img src="http://example.com/images/best-of.jpg" alt="">
    </div>
    
    0 讨论(0)
  • 2020-12-07 20:13

    One way is using print_r(array, true) and it will return string representation of array

    0 讨论(0)
  • 2020-12-07 20:14

    What about this shorter, more transparent, yet more intuitive with array_walk

    $attributes = array(
      'data-href'   => 'http://example.com',
      'data-width'  => '300',
      'data-height' => '250',
      'data-type'   => 'cover',
    );
    
    $args = "";
    array_walk(
        $attributes, 
        function ($item, $key) use (&$args) {
            $args .= $key ." = '" . $item . "' ";  
        }
    );
    // output: 'data-href="http://example.com" data-width="300" data-height="250" data-type="cover"
    
    0 讨论(0)
  • 2020-12-07 20:14
    echo implode(",", array_keys($companies->toArray()));
    

    $companies->toArray() -- this is just in case if your $variable is an object, otherwise just pass $companies.

    That's it!

    0 讨论(0)
提交回复
热议问题