1

I have the following array, I'm trying to append the following ("","--") code

Array
(
    [0] => Array
        (
            [Name] => Antarctica
        )

)

Current JSON output

[{"Name":"Antarctica"}]

Desired output

{"":"--","Name":"Antarctica"}]

I have tried using the following:

$queue = array("Name", "Antarctica");
array_unshift($queue, "", "==");

But its not returning correct value.

Thank you

2
  • 2
    $queue = array("Name", "Antarctica"); is not the same as what you show at the beginning of the question. Commented Jan 9, 2015 at 20:39
  • If Your goal is to create such JSON, You should know that JSON objects are unordered lists of key-value pairs, so the effort would be senseless. See json.org - "An object is an unordered set of name/value pairs.". Commented Jan 9, 2015 at 20:43

5 Answers 5

4

You can prepend by adding the original array to an array containing the values you wish to prepend

$queue = array("Name" => "Antarctica");
$prepend = array("" => "--");
$queue = $prepend + $queue;

You should be aware though that for values with the same key, the prepended value will overwrite the original value.

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

1 Comment

This doesn't make any changes to the array's numerical index too. If it had any. Nice.
2

The translation of PHP Array to JSON generates a dictionary unless the array has only numeric keys, contiguous, starting from 0.

So in this case you can try with

$queue = array( 0 => array( "Name" => "Antarctica" ) );

$queue[0][""] = "--";

print json_encode($queue);

If you want to reverse the order of the elements (which is not really needed, since dictionaries are associative and unordered - any code relying on their being ordered in some specific way is potentially broken), you can use a sort function on $queue[0], or you can build a different array:

$newqueue = array(array("" => "--"));
$newqueue[0] += $queue[0];

which is equivalent to

$newqueue = array(array_merge(array("" => "--"), $queue[0]));

This last approach can be useful if you need to merge large arrays. The first approach is probably best if you need to only fine tune an array. But I haven't ran any performance tests.

Comments

1

Try this:

$queue = array(array("Name" => "Antarctica")); // Makes it multidimensional 
array_unshift($queue, array("" => "--"));

Edit

Oops, just noticed OP wanted a Prepend, not an Append. His syntax was right, but we was missing the array("" => "--") in his unshift.

Comments

0

You can try this :

$queue = array("Name" => "Antarctica");
$result = array_merge(array("" => "=="), $queue);

2 Comments

No, this only generates a single dictionary, not an array of one dictionary. You can use $result=array(array_merge...); though.
You are totally right:) Not fully understand this problem.@Iserni
0
var_dump(array_merge(array(""=>"--"), $arr));

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.