-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
2
  • 3
    Please see: How to Ask, What have you tried, etc? Commented Jul 7, 2022 at 8:55
  • Specifically this answer Commented Jul 10, 2022 at 12:34

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

5 Comments

@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
@HaMaDa I will help you update your question just add your code
how it would be ?
@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
This advice is demo'ed here. 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.
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

4 Comments

That wouldn't include the keys in the output
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.
it merge the value like 500,1000
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.
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]
    ));
    ?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.