-6

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
HaMaDa
  • 1
  • 5

3 Answers3

0

This code will solve your issue.

$array = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
    
foreach ($array as $key => $val) {
                
    echo " ".$key ." ". $val." ,"; 
           
}

enter image description here

Z0OM
  • 1
  • 4
  • 18
  • 29
Aashir Azeem
  • 173
  • 6
  • That wouldn't include the `keys` in the output – akaBase Jul 07 '22 at 08:56
  • That won't output the keys, just the values. A suggestions would be to test the code and see if it actually does what the OP wants before posting it. At least for one-liners like this. – M. Eriksson Jul 07 '22 at 08:56
  • it merge the value like 500,1000 – HaMaDa Jul 07 '22 at 08:56
  • 4
    Tbh, we try to avoid answering questions where the OP hasn't shown _any_ attempt on their end _at all_. Answering questions like that basically just turns SO into a free coding service, which is not what it's for. – M. Eriksson Jul 07 '22 at 09:00
0

Using implode and array_map

$input = [13 => 500, 16 => 1000];
$output = implode(', ', array_map(
                function ($v, $k) {
            return $k . " " . $v;
        }, $input, array_keys($input))
);
var_dump($output);

Using foreach

$input = [13 => 500, 16 => 1000];
$output = "";
foreach ($input as $k => $v) {
    $output .= $k . " " . $v . ", ";
}
$output = rtrim($output, ", ");
var_dump($output);
angel.bonev
  • 2,154
  • 3
  • 20
  • 30
  • @HaMaDa just post what have you tried in your questions, the community hates when OP doesn't try something. It' doesn't matter if it's wrong – angel.bonev Jul 07 '22 at 09:14
  • @HaMaDa I will help you update your question just add your code – angel.bonev Jul 07 '22 at 09:15
  • how it would be ? – HaMaDa Jul 07 '22 at 09:20
  • @HaMaDa you will see my suggestion for your question. If you like it, and that was your attempt you can approve it. Next time just post your attempt, corect formated input and expected output and it will be appreciated from the community. Cheers and have a great day – angel.bonev Jul 07 '22 at 10:27
  • 1
    This advice is demo'ed [here](https://stackoverflow.com/a/37884980/2943403). Please look for dupes before answering. If a new question can be resolved by referencing an earlier page on SO, vote to close instead of answering. [The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place.](https://stackoverflow.com/help/duplicates#:~:text=The%20fundamental%20goal%20of%20closing%20duplicate%20questions%20is%20to%20help%20people%20find%20the%20right%20answer%20by%20getting%20all%20of%20those%20answers%20in%20one%20place.) – mickmackusa Jul 10 '22 at 12:38
0

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]
    ));
    ?>
Makyen
  • 31,849
  • 12
  • 86
  • 121