-1

I have an array like below format:

Array(
        [0] => 25/1
        [1] => 10/1
     );

But I want to convert the array so it would look like below format

Array(
            [0] => "25/1"
            [1] => "10/1"
         );

I have already tried lots of things about this issue but can't get the desire solution yet.

Anybody help please ?

5
  • 1
    Are you sure you don't want these quotes in the view only? Commented Oct 18, 2021 at 14:36
  • 2
    It's already a string (there's no such primitive type as the one formatted in such way) Commented Oct 18, 2021 at 14:36
  • 1
    Does this answer your question? How to cast array elements to strings in PHP? Commented Oct 18, 2021 at 14:37
  • Just put the quotes in your view. The elements are already strings. Commented Oct 18, 2021 at 14:39
  • Yup do in the output or use array_map if you really for some reason want the array values quoted 3v4l.org/2PJur#v8.0.11 Commented Oct 18, 2021 at 15:00

2 Answers 2

1

Thx to the community. 25/1 are allready a string. If you echo 25/1 echo 25/1;you will get 25. PHP will convert it to an integer echo getType(25/1). That means if you @Praful have these values you have already strings.

But in general you can cast (Explicit cast) integer or other types with (string). or you can use the php function strval()

Sign up to request clarification or add additional context in comments.

Comments

0

If you really need to wrap array values in double quotes, here is a simple method with array_map():

$current_array = array(red, green);
$new_array = array();

$new_array  = array_map(function($rec){
//concatenate record with "" 
$result = $rec= '"'.$rec.'"';
return $result;
},$current_array);

print_r($result);

Output will be:

Array ( [0] => "red" [1] => "green" )

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.