Home > front end >  How to add double quote to array values php
How to add double quote to array values php

Time:10-19

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 ?

CodePudding user response:

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()

CodePudding user response:

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" )
  •  Tags:  
  • php
  • Related