0

I'm working in PHP. I have a dynamic number of arrays called 'placeholder0, placeholder1, placeholder2....' and I want to json_encode them

$encode = json_encode(array('tabs' => $tabs, 'placeholder0' => $placeholder0, 'placeholder1' => $placeholder1, 'placeholder2' => $placeholder2 ..... ));

The number of values inside the 'tabs' array is the same as the number of 'placeholder' arrays.

How can I do this?

1 Answer 1

1
$result = array('tabs' => $tabs);

for ($i = 0; $i < count($tabs); $i++) {
    $result['placeholder' . $i] = ${'placeholder' . $i};
}

file_put_contents($jsonLocation, json_encode($result));
Sign up to request clarification or add additional context in comments.

4 Comments

:)) sorry.. post it and I will delete mine :))
Nope this answer is great!
when I try file_put_contents($jsonLocation, var_dump(json_encode($result)) ); it only comes up on the screean and doesn't get outputed in the file
I've updated the answer.. var_dump is just a debugging function which helps you to see the type and value of a variable..

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.