1

I have the following method that I use to recursively build my php arrays then turn them into a json string:

protected function stringKeyToMultArray(&$newarr, $keys, $value) {
    if(count($keys) > 1) {
        $key = array_shift($keys);

        if(!isset($newarr[$key]) || !is_array($newarr[$key])) {
            $newarr[$key] = array();
        }

        $this->stringKeyToMultArray($newarr[$key], $keys, $value);
    } else {
        $newarr[array_shift($keys)] = $value;
    }
}

EDIT: This is how it is called:

$path_arr = [];
foreach ($product_row['fields'] as $field) {
    //gets the path for this field like pathto/field
    $strpath = $tags[$field['id']]['str_path'];
    $paths = explode('/', $strpath);
    $this->stringKeyToMultArray($path_arr, $paths, $field['value']);
}

Then after this is simply json_encode it like json_encode($path_arr) END EDIT The issue is the formatting if it gets to a second level should use an array to wrap the parts. So what I am getting now is:

{  
    "product_title":"Test Title",
    "ASIN":"1234567890",
    "codes":{  
        "type":"UPC",
        "number":"030878249270"
    },
    "quantity":"1"
}

What I need is the following:

{  
    "product_title":"Test Title",
    "ASIN":"1234567890",
    "codes":[
        {  
        "type":"UPC",
        "number":"030878249270"
        }
    ],
    "quantity":"1"
}

Any ideas on how I could do this with the recursive method?

9
  • 2
    Do you have sample input that is incorrectly encoding? Commented Mar 11, 2016 at 0:12
  • json_encode will give you an object.. if you want an array/associative array, use json_decode. That being said, I don't even see json_encode being used here. Commented Mar 11, 2016 at 0:21
  • "if it gets to a second level should use an object to wrap the parts" - but what you're showing after "What I need is the following:" is an object wrapped in an array. Commented Mar 11, 2016 at 0:38
  • @VolkerK Sorry just edited, I need it wrapped in an array. Commented Mar 11, 2016 at 1:07
  • $strpath = $tags[$field.... is that really $strpath or $jetpath? And if it's not $jetpath, then where does $jetpath come from? Commented Mar 11, 2016 at 1:11

1 Answer 1

2

Given the (very) specific conditions stated in the question and the comments:

protected function stringKeyToMultArray(&$newarr, $keys, $value) {
    if (count($keys) > 1) {
        $key = array_shift($keys);
        if ( !isset($newarr[$key]) || !is_array($newarr[$key])) {
            $newarr[$key] = array(array());
        }
        $this->stringKeyToMultArray($newarr[$key][0], $keys, $value);

    } else {
        $newarr[array_shift($keys)] = $value;
    }
}

see https://3v4l.org/4Hpek

Sign up to request clarification or add additional context in comments.

1 Comment

Given the awesomeness of the answer... this works perfectly.

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.