Home > Back-end >  Arrays with keys to custom string php
Arrays with keys to custom string php

Time:07-08

I have this array

Array ( [13] => 500 [16] => 1000 )
Array ( [12] => 1 [13] => 1111 )

how can I make them a string as this shape

13 500, 16 1000
12 1, 13 1111

CodePudding user response:

Using enter image description here

CodePudding user response:

assuming you searching for a function with multiple pair array values (as you describe) and each result should be the format: key1[sp]val1,[sp]key2[sp]val2 and you want an array of all these values to use later i did this function:

    <?php
    
    function ar(){
        $a=func_get_args();
        foreach($a as $ar){
            $s='';
            $i=0;
            $s='';
            foreach($ar as $ch =>$vl){
                $s.=$ch.' '.$vl;
                if($i<count($ar)-1){
                    $s.=', ';
                }
                $i  ;
            }
            $res[]=$s;
        }
        return $res;
    }
    

/* output values by sending multiple arrays to parse */
    var_dump(ar(
        [13 => 500,16=> 1000]
        ,[12 => 1,13 => 1111]
    ));
    ?>
  • Related