1

I have json element

{"element":{},
 "gallery":{
    "images":[
    {"id":"1","description":{},"image_path":"1.jpg"},
    {"id":"2","description":"Test","image_path":"2.jpg"}
    ]},
 "additional_value":"Test"}

php function json_decode($json, TRUE) return

Array
(
    [element] => Array()
    [gallery] => Array(
            [images] => Array(
                    [0] => Array(
                            [id] => 1
                            [description] => Array()
                            [image_path] => 1.jpg)
                    [1] => Array(
                            [id] => 2
                            [description] => Test
                            [image_path] => 2.jpg)
                    )
             )
    [additional_value] => Test
)

How I can replace/convert empty arrays to string? For example:

 [0] => Array([id] => 1
              [description] => ""
              [image_path] => 1.jpg)

Thank you!

3
  • If you cannot avoid description being set to a JSON object you will have to do the conversion manually in PHP. Commented Oct 2, 2015 at 18:10
  • 1
    To add to @RhinoDevel, you can use array_walk_recursive(). Commented Oct 2, 2015 at 18:14
  • @frz3993 That turned out not to work. Commented Oct 3, 2015 at 9:09

2 Answers 2

3

I think the best way to do this would seem to be (but it is not!) to decode the json and use array_walk_recursive afterwards to convert the empty arrays to empty strings.

This function traverses all items of the array recursively. Each of them is passed through the specified callback function by reference.

However, it turns out that this function doesn't call the callback for items that are themselves arrays, but only for items inside those arrays. This behaviour makes it impossible to find the empty arrays using array_walk_recursive.

Therefore, I've written a replacement function that should do exactly the same, except it always also calls the callback for items that are themselves arrays, before going deeper into the recursion.

That function and the calling code can be found below.

<?php
// The replacement function for array_walk_recursive()
function my_array_walk_recursive(&$array, $callback, $userdata = null) {
  if (!is_array($array)) return false;

  foreach ($array as $key => &$value) {
    // Difference: PHP's array_walk_recursive will only call the callback
    // for items that are not arrays themselves. Here, the callback is always called.
    call_user_func_array($callback, array(&$value, $key, $userdata));
    if (is_array($value)) {
      my_array_walk_recursive($value, $callback, $userdata);
    }
  }
  return true;
}

// The calling code.
$json = 
  '{"element":{},
   "gallery":{
      "images":[
      {"id":"1","description":{},"image_path":"1.jpg"},
      {"id":"2","description":"Test","image_path":"2.jpg"}
      ]},
   "additional_value":"Test"}';

$yourArray = json_decode($json, TRUE);

my_array_walk_recursive(
  $yourArray, 
  function(&$item, $key){
    if (is_array($item) && count($item) === 0) {
      $item = "x";
    }
  });

var_dump($yourArray);
Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately array_walk_recursive skip these values. Anyway thanks for link, I think it help me in future.
Ah. It skips it because array_walk_recursive only visits the items of the arrays, not the arrays themselves. These are the 'leaf nodes' mentioned by the top comment on the documentation page. One way to solve this is by iterating over the arrays yourself using a recursive function.
I've written a replacement. And this time I've tested it, so it should work now. :-)
Improved versions that should also support other types of callbacks than inline functions.
0

Correct work next code/trick

$json_upd = str_replace('{}', '""', $json);
$result = json_decode($json_upd,TRUE);

If someone have better solution, please add comment - I will be grateful.

1 Comment

This is a possibility, but may have side effects, for instance when a string in the json contains {}.

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.