php - array_unshift, force second value -
I am trying to create a table of contents in the .json for the interactive book. My output is to start with:
{"url": "index.html", "hidden": true},
but its After that I want to add arrays, Array_unshift adds this value before and I would like to add the new arrays after that instead in other words index Html should be on top and I should be successful with the arrays generated with my script How do I do it?
post.php
& lt ;? Php // check if a form was submitted (empty! $ _POST)) {$ file = 'entries.json'; $ Json = file_get_contents ($ file); // json back to a php stdClass $ phpClass = json_decode ($ json); $ PostArray = Array ("url" = & gt; $ _POST ['url'], "title" = $ $ _POST ['title'], "thumb" = & gt; $ _POST ['thumb'], "binline "= & Gt; $ _POST ['Bineline']); // push new $ poster at the top of phpClass array_unshift ($ phpClass-> content, $ postArray); // Encrypt php class in json $ new_json = json_encode ($ phpClass); // write it file_put_contents ($ file, $ new_json); }
See a few examples:
// Example array $ array = array ('a', 'b'); // condition, where we want to paste offset = 2 offset new value; $ Result = array_slice ($ array, 0, $ offset - 1, true) + array ("my_key" = & gt; "my_value") + array_slice ($ array, $ offset - 1, count ($ array) - $ offset + 1, true);
And now if we call var_dump ()
on our $ result
we will get:
array 0 = & gt; String 'A' (length = 1) 'my_key' = & gt; String 'my_value' (length = 8) 1 = & gt; You can use your own array in the array ("my_key" = & gt; "my_value")
example instead of
. And it will always be second in the resulting one.
Comments
Post a Comment