1

I have a below array in PHP

$arrOrders['remains'] = 'ASC';
$arrOrders['rate']    = 'DESC';

How can I use combine to a string like just with native function?

$str = 'remain ASC, rate DESC';
1
  • 1
    ever considered to use foreach to handle an array? Commented Oct 27, 2011 at 7:09

4 Answers 4

5
$a = array(
    'remains' => 'ASC',
    'rate'      => 'DSC'
);  

echo implode(', ', array_map(function($v1, $v2) {
    return sprintf('%s %s', $v1, $v2);
}, array_keys($a), array_values($a)));
// remains ASC, rate DSC
Sign up to request clarification or add additional context in comments.

8 Comments

are you always use all PHP function you know for the every simple operation?
+1 for use of functions, anyone know how would this preform vs the simple foreach HL. posted?
Not really - but I must admit that I like the functional approach on arrays. It's a matter of taste...
it's matter of bloating your code, dude. unnecessarily bloating.
Don't worry so much, because I want to have a approach like that... I know if I care about performance I could be consider with just loop. :)
|
3

use a foreach and implode()

foreach ($arrOrders as $key => $val) {
$temp[] = "$key $val";
}
$order = implode(',', $temp);

Comments

2

You can do this:

$string = '';
foreach($arrOrders as $key => $value)
   $string = $string . "$key $value, ";
$string  = rtrim($string, ', '); // cut the last comma at the end of the string

echo $string // remains ASC, rate DESC

5 Comments

I'll +1 it anyway now, as it's the only sensible answer here.
yeah, sense of community, as you said. this very community is a sick one. almost every answer is unusable or propagationg whatever bad practice. that's why I am too strict with votes. it is a custome here to upvote FOR NOTHING, just for a sympathy. Ever complained for the unjust upvote?
LOL, so, you have never complained. I thought so. it's only downvotes your concern :)
no. as i said already, this site suffer from huge lack of downvotes.
that's impossible. most of them do care only of votes, not of knowledge
1

you are looking for implode, but it dose not include the keys, but the first comment on the page is a function for including keys

/**
 * Implode an array with the key and value pair giving
 * a glue, a separator between pairs and the array
 * to implode.
 * @param string $glue The glue between key and value
 * @param string $separator Separator between pairs
 * @param array $array The array to implode
 * @return string The imploded array
 */
function array_implode( $glue, $separator, $array ) {
    if ( ! is_array( $array ) ) return $array;
    $string = array();
    foreach ( $array as $key => $val ) {
        if ( is_array( $val ) )
            $val = implode( ',', $val );
        $string[] = "{$key}{$glue}{$val}";

    }
    return implode( $separator, $string );

}


$arrOrders = array(
    'remains' => 'ASC',
    'rate'      => 'DSC'
);
$str = array_implode( ' ', ', ', $arrOrders);

2 Comments

do you get an error or something? are you sure you used it correct, it is working fine for me
@vietean ever considered possibility to learn from that code, not just copypaste it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.