How to implode array with key and value without foreach in PHP

后端 未结 11 1397
悲&欢浪女
悲&欢浪女 2020-12-04 07:20

Without foreach, how can I turn an array like this

array(\"item1\"=>\"object1\", \"item2\"=>\"object2\",.......\"item-n\"=>\"objec         


        
相关标签:
11条回答
  • 2020-12-04 08:01

    You could use PHP's array_reduce as well,

    $a = ['Name' => 'Last Name'];
    
    function acc($acc,$k)use($a){ return $acc .= $k.":".$a[$k].",";}
    
    $imploded = array_reduce(array_keys($a), "acc");
    
    0 讨论(0)
  • 2020-12-04 08:04

    Using array_walk

    $a = array("item1"=>"object1", "item2"=>"object2","item-n"=>"object-n");
    $r=array();
    array_walk($a, create_function('$b, $c', 'global $r; $r[]="$c=$b";'));
    echo implode(', ', $r);
    

    IDEONE

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

    Change

    -    return substr($result, (-1 * strlen($glue)));
    +    return substr($result, 0, -1 * strlen($glue));
    

    if you want to resive the entire String without the last $glue

    function key_implode(&$array, $glue) {
        $result = "";
        foreach ($array as $key => $value) {
            $result .= $key . "=" . $value . $glue;
        }
        return substr($result, (-1 * strlen($glue)));
    }
    

    And the usage:

    $str = key_implode($yourArray, ",");
    
    0 讨论(0)
  • 2020-12-04 08:11

    I spent measurements (100000 iterations), what fastest way to glue an associative array?

    Objective: To obtain a line of 1,000 items, in this format: "key:value,key2:value2"

    We have array (for example):

    $array = [
        'test0' => 344,
        'test1' => 235,
        'test2' => 876,
        ...
    ];
    

    Test number one:

    Use http_build_query and str_replace:

    str_replace('=', ':', http_build_query($array, null, ','));
    

    Average time to implode 1000 elements: 0.00012930955084904

    Test number two:

    Use array_map and implode:

    implode(',', array_map(
            function ($v, $k) {
                return $k.':'.$v;
            },
            $array,
            array_keys($array)
        ));
    

    Average time to implode 1000 elements: 0.0004890081976675

    Test number three:

    Use array_walk and implode:

    array_walk($array,
            function (&$v, $k) {
                $v = $k.':'.$v;
            }
        );
    implode(',', $array);
    

    Average time to implode 1000 elements: 0.0003874126245348

    Test number four:

    Use foreach:

        $str = '';
        foreach($array as $key=>$item) {
            $str .= $key.':'.$item.',';
        }
        rtrim($str, ',');
    

    Average time to implode 1000 elements: 0.00026632803902445

    I can conclude that the best way to glue the array - use http_build_query and str_replace

    0 讨论(0)
  • 2020-12-04 08:12

    and another way:

    $input = array(
        'item1'  => 'object1',
        'item2'  => 'object2',
        'item-n' => 'object-n'
    );
    
    $output = implode(', ', array_map(
        function ($v, $k) {
            if(is_array($v)){
                return $k.'[]='.implode('&'.$k.'[]=', $v);
            }else{
                return $k.'='.$v;
            }
        }, 
        $input, 
        array_keys($input)
    ));
    

    or:

    $output = implode(', ', array_map(
        function ($v, $k) { return sprintf("%s='%s'", $k, $v); },
        $input,
        array_keys($input)
    ));
    
    0 讨论(0)
提交回复
热议问题