0

i have following array . i want to json_encode for this array Result like [{"dietary_options":"234"},{"dietary_options":"123"}] using array method not for loop.

Array
(
[0] => Array
    (
        [dietary_options] => 
    )

[1] => Array
    (
        [dietary_options] => 
    )

[2] => Array
    (
        [dietary_options] => 
    )

[3] => Array
    (
        [dietary_options] => 
    )

[4] => Array
    (
        [dietary_options] =>234 
    )

[5] => Array
    (
        [dietary_options] => 123
    )

)

6
  • and why is it that you can't use a for or a foreach? it just takes a few lines using them Commented Dec 5, 2018 at 6:37
  • 1
    json_encode($my_array); Commented Dec 5, 2018 at 6:40
  • Take a look at array_filter Commented Dec 5, 2018 at 6:40
  • first priority to using array function . i already done with for loop but it is good if using array function. Commented Dec 5, 2018 at 6:41
  • @SvenLiivak no its not working its return null too. i dont want that . Commented Dec 5, 2018 at 6:42

6 Answers 6

1

Simply array_filter is the easy way

$data = json_encode(array_filter($data, function ($item) { 
    return !!$item['dietary_options'];
}));
Sign up to request clarification or add additional context in comments.

Comments

0

Try this code,

 $data = array(array('dietary_options'=>''),
                  array('dietary_options'=>'one'),
                 array('dietary_options'=>'two'));


$data = array_map('array_filter', $data);
$data = array_values(array_filter($data));
print_r(json_encode($data,true));
// print_r(json_encode(array_filter($data),true));

1 Comment

Thanks Work for me but u don't need to be write array_filter in json_encode print_r(json_encode($data,true)); @kmg kumar
0

please try below code.

$data = array(array("dietary_options"=>""),array("dietary_options"=>"789"),array("dietary_options"=>"123456"));
$data = array_filter(array_map('array_filter', $data));

echo json_encode(array_values($data));

Comments

0

This works fine for me,

print_r(json_encode(array_filter($myArray)));

Comments

0

try this . It's working as your expecting format without loop.

$array=array(
array("dietary_options"=>"234"),
array("dietary_options"=>""),
array("dietary_options"=>""),
array("dietary_options"=>"123")
);

$array_data = array_map('array_filter', $array);
$array_data = array_filter($array_data);
$array_data = array_values($array_data);
print_r(json_encode($array_data));

3 Comments

hey, did you check your code ?, they gives the object in json value.
{"5":{"dietary_options":"234"},"7":{"dietary_options":"123"}} i got this kind of result i want [{"dietary_options":"234"},{"dietary_options":"123"}] .. BTW thanks ... @VinothRaja
Hi @Kanewilliam , could you explain what the different between VinothRaja and Kmg kumar answer ?
0

php array_filter good option.

$data = array(
            array("dietary_options" => ''),
            array("dietary_options" => ''),
            array("dietary_options" => ''),
            array("dietary_options" => 123),
            array("dietary_options" => 234),
        );
return new JsonResponse(array_values(array_filter($data, function($k){
        return !!$k["dietary_options"];
})));

Comments

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.