DingDongDev
DingDongDev

Reputation: 53

PHP array manipulation

I have an array as $arr = array("name" => "Fom Xong" , "Sales" => "100");

From this array I want to generate a string something like this

$str = 'name="Fom Xon" Sales="100"';

Is it possible???

thanks in advance

Upvotes: 1

Views: 148

Answers (2)

Hck
Hck

Reputation: 9167

For example you can do like this:

$tmp_arr = array();
foreach ($arr as $key => $val)
  $tmp_arr[] = $key.'="'.$val.'"';

$str = implode(' ', $tmp_arr);

Upvotes: 3

deceze
deceze

Reputation: 522382

$output = array();
foreach ($arr as $key => $value) {
    $output[] = "$key=\"$value\"";
}
echo join(' ', $output);

Or:

echo join(' ', array_map(function ($key, $value) { return "$key=\"$value\""; }, array_keys($arr), $arr));

Upvotes: 1

Related Questions