1

I have this array :

Array
(
   [0] =>
   [1] =>
   [2] => test1
   [3] => test2
)

Now I make :

if(!empty($a_data)){
        $a_return = array(implode(',"', array_filter($a_data)));
    }

And I get this :

aReturn": [
   "test1,\"test2"
]

But I want to get :

aReturn": [
   "test1","test2"
]

Can you help me please ? Thx in advance and sorry for my english

2
  • 1
    ," why are u using this? why not array(implode(',', array_filter($a_data)));? Commented Oct 26, 2016 at 13:38
  • So you're converting this array to json to display it? Learn how json works if you're going to do that; but consider imploding on '","' Commented Oct 26, 2016 at 13:38

2 Answers 2

1

    $value  = array_values(array_filter($a_data));
    var_dump($value);
Sign up to request clarification or add additional context in comments.

Comments

0

Here no need to store comma separated values again in an array you can use array_values() function here which will return the all values inside the array than you just need to use json_encode() for your desired output as:

<?php
$a_data = array('','','test','test2');
$a_return = array_values(array_filter($a_data));
echo json_encode($a_return);
?>

Output: ["test","test2"]

And if you still want to use your code than you just need to use stripslashes() for strip slashes as:

<?php
$a_data = array('','','test','test2');
$a_return = array(implode('","', array_filter($a_data)));
echo stripslashes(json_encode($a_return));
?>

Output: ["test","test2"]

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.