0

This is my array:

  Array(
[Priority] => 194.49
[Xpresspost] => 147.49
[Expedited] => 48.57
[Regular] => 48.57

)

I want to show my array like this:

'Priority'=> '194.49','Xpresspost'=>'40','Expedited'=> '48.57','Regular'=>'48.57'
2
  • use var_dump post your array Commented Apr 1, 2017 at 16:23
  • How are you getting your input Array( [Priority] => 194.49 [Xpresspost] => 147.49 [Expedited] => 48.57 [Regular] => 48.57 Commented Apr 1, 2017 at 16:37

1 Answer 1

2

If you want your array to be printed in a single line like that you could use:

$array = array(
    'Priority' => 194.49,
    'Xpresspost' => 147.49,
    'Expedited' => 48.57,
    'Regular' => 48.57
);

$output = array();
foreach($array as $key => $value) {
    $output[] = " '$key' => '$value'";
}
echo implode(',', $output);
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, @Hicaro it works like charm. Now i want to assign my array to some variable like this : $neil = array('Priority'=> '194.49','Xpresspost'=>'40','Expedited'=> '48.57','Regular'=>'48.57'); How can i do that ?
Hey @NeilDhakal, the question is, were you trying to convert your array to a string or you wanted to convert the float numbers to string type and still have an array?
Yes, i'm trying to return array like this from controller in codeigniter : return array('Priority' => '194.49', 'Xpresspost' => '147.49', 'Expedited' => '48.57', 'Regular' => '48.57');
I am still confused with what you want to do now. Do you want to make the string as a first element of a new array or convert it back to be an array?
You could do foreach($array as $key => $value) {$array[$key] = "'$value'";} then return the array itself as return $array.

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.