0

I search a lot in stack and google try to find the answer which seems to be easy but I'm still stuck with it

I write a code to encode json with values I wanted from . and I would like to add a key / value to the JSON

the JSON is as following structure

    {
   - files: [
      {
        title: "works",
       - tracks: [
           {
             title: "File",
             format: "mp3"
           }
        ]
      },
   -{
       title: "season1",
       tracks: [
         {
             title: "Button-1",
             format: "wav"
         },
   -{
        title: "Beep-9",
        format: "wav"
     }
    ]
   }
  ]
}

I want to add to that a key and its value at the beginning to the json as properties under the title files , I mean that can be read by code as

json[files][new_key]

I tried to set that value like this

$json['new_key'] = "new_value";

but this causes adding numbers to the arrays in json , I don't why they numbered

enter image description here

this numbers affect my reading way of the json as JSONModel in my iOS app so , I hope you can help me thanks in advance

Twinkle
  • 430
  • 1
  • 5
  • 21

3 Answers3

0

Assuming that the new value you want to add varies by file, you would need to loop through $json[files] and insert them per key/value pair.

<?php

for($i=0; $i<count($json); $i++)
    $json[files][$i]["new_key"] = "value";

?>
  • I just need it once in the json – Twinkle Jan 30 '14 at 21:58
  • In that case, `$json[files]['new_key'] = "new_value";` should work fine. The numbers are there no matter what, except they are visible in the JSON model now (due to PHP's implementation of `json_encode()`).\ –  Jan 30 '14 at 22:01
  • I I don't have a title yet when I want to add the value , I have the array contains the data needed and I want to add the kay pair before using echo '{"files": '.json_encode($arr2).' }'; – Twinkle Jan 30 '14 at 22:05
  • @Twinkle I don't understand. WHat is `$arr2`? –  Jan 30 '14 at 22:10
  • it's the array includes the data to be encoded as json – Twinkle Jan 30 '14 at 22:15
0

The reason you're getting numbers appearing is because you're adding a key to an array (which functions more or less as a list in JS). So before you basically have the object "files" as a list of objects zero-indexed like any other JS array. When you add the key, JS simply adds your key to the end of your present keys (in your case 0 and 1).

It seems like you have a list of multimedia objects where each has a title and a list of tracks. The most straightforward way to solve your issue would be as follows:

$fileItem['title'] = 'works';
$fileItem['tracks'] = array(
    array(
        'title' => 'File',
        'format' => 'mp3'
    )
);
$json['files'][] = $fileItem;

$fileItem['title'] = 'season1';
$fileItem['tracks'] = array(
    array(
        'title' => 'Button-1',
        'format' => 'wav'
    ),
    array(
        'title' => 'Beep-9',
        'format' => 'wav'
    )
);
$json['files'][] = $fileItem;

Then you JSON encode it and return it as you normally would. You can put the above in a loop as well. I lack enough context to recommend exactly how.

clarkatron
  • 529
  • 5
  • 12
  • Yes that is what happening , but I don't get how to fix it (sorry) I am using by the end this echo sentence echo '{"files": '.json_encode($arr2).' }'; where $arr2 includes data needed and should include the value too – Twinkle Jan 30 '14 at 22:10
  • Can you help me understand what constitutes a fix here? What structure would you like to see? – clarkatron Jan 30 '14 at 22:12
  • I mean how to avoid the appearing of numbers – Twinkle Jan 30 '14 at 22:14
  • @Twinkle I've added a PHP snippet that would result in the JSON structure you were looking for. – clarkatron Jan 31 '14 at 02:01
0

I'm still not sure what you have exactly, but it seems you are trying to manipulate the json string.

If done correctly, that is probably the most efficient solution, but what you could also do is:

  • use json_decode to generate an array from your json string;
  • locate the correct section / sub-array where you want to add your data;
  • use array_unshift to prepend your new key - value pair;
  • use json_encode to generate a json string from your complete array.
jeroen
  • 91,079
  • 21
  • 114
  • 132
  • can I set key and value in array_unshift ? array_unshift($arr,'key' => 'value'); this doesn't work – Twinkle Jan 30 '14 at 22:28
  • @Twinkle Hmmmmm, maybe not, in that case you would have to use array_merge or something similar... – jeroen Jan 30 '14 at 22:29