Monday 3 September 2018

PHP set array with variable

I want to make an array with a variable which has some array values. The problem is I am trying to add dynamic value in an array which has a fixed format. Here is my code:

$array_value="'id','name'";
$aColumns = array( $array_value );

But it's not working. So array should be like(expected):
$aColumns = array( 'id','name' );

Thanks in advance.

You can use explode to convert your string to an array and str_replace for replacing the apostrophes.
$aColumns = explode(',', str_replace('\'', '', $array_value));

0 comments:

Post a Comment