0

I'm new to json and decided to use it because it looks promising. How does the syntax work?

Here is my simple php script:

$usernames = array('jake', 'john', 'jackie', 'jared');
$users = array();
for($i = 0; $i < 4; $i++)
{
    $users[$i]['id'] = $i;
    $users[$i]['name'] = $usernames[i];
}
$data = json_encode( $users );
echo $data;

It Outputs:

{"i":{"id":3,"name":null}}

I want it to output:

[{"id": 1,"name": "jake"},{"id": 2,"name": "john"},{"id": 3,"name": "jackie"},{"id": 4,"name": "jared"}]
MysteryDev
  • 610
  • 2
  • 12
  • 31
  • 3
    You have a syntax error inside the `for` loop. `i` should be `$i`. See the working demo: https://eval.in/95092 – Amal Murali Jan 26 '14 at 08:57
  • 1
    **Pro tip**: [Enable error reporting during debugging](http://stackoverflow.com/a/6575502/) – Amal Murali Jan 26 '14 at 08:59
  • avoid hard coding iteration quantity into your logic, and either get `count($usernames)` or use `foreach()`, if you change the length of your names list you may miss something. later on. – Scuzzy Jan 26 '14 at 09:00

5 Answers5

1

Try to use:

$usernames = array('jake', 'john', 'jackie', 'jared');
$users = array();
foreach($usernames as $id => $username)
{
    $users[$id]['id'] = $id + 1;
    $users[$id]['name'] = $username;
}
$data = json_encode( $users );
echo $data;
Victor Bocharsky
  • 11,930
  • 13
  • 58
  • 91
  • That would overwrite $users with every iteration, would need `$users[]` or `array_push()` and the structure would still be a bit incorrect. – Scuzzy Jan 26 '14 at 09:02
  • 4
    I'd just simply have `$users[] = array('id'=>$id,'name'=>$username);` pretty much the same thing just not going to throw any chance of a notice of `$users[$id]` not existing as an array first – Scuzzy Jan 26 '14 at 09:06
  • @Scuzzy, Yes, you right, but Abhik Chakraborty already do this – Victor Bocharsky Jan 26 '14 at 09:09
1

Just one line inside the loop will do it

$usernames = array('jake', 'john', 'jackie', 'jared');
$users = array();
for($i = 0; $i < 4; $i++)
{
    $users[] = array("id"=>$i+1,"name"=>$usernames[$i]);

}
$data = json_encode( $users );
echo $data;

Output as you wanted in your question

[{"id":1,"name":"jake"},{"id":2,"name":"john"},{"id":3,"name":"jackie"},{"id":4,"name":"jared"}]
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
0
$usernames = array('jake', 'john', 'jackie', 'jared');
$users = array();
for($i = 0; $i < 4; $i++)
{
    $user=array();
    $user['id'] = $i;
    $user['name'] = $usernames[$i];
    $users[]=$user;
}
$data = json_encode( $users );
echo $data;

Output:

[{"id":0,"name":"jake"},{"id":1,"name":"john"},{"id":2,"name":"jackie"},{"id":3,"name":"jared"}]
undefined_variable
  • 6,180
  • 2
  • 22
  • 37
0

Try this,

$usernames = array('jake', 'john', 'jackie', 'jared');
$users = array();
for($i = 0; $i < 4; $i++)
{
    $users[$i]['id'] = $i;
    $users[$i]['name'] = $usernames[$i];
}
$data = json_encode( $users );
echo $data;

Changed,

$usernames[i]

To,

$usernames[$i]

And I think its better using a foreach loop instead of for..

akhil.cs
  • 691
  • 1
  • 5
  • 12
0
$usernames = array('jake', 'john', 'jackie', 'jared');

$users = array();

foreach($usernames as $id=>$username{
    $id++;
    $users[]=array('id'=>$id,'name'=>$username);
}

$data=json_encode($users);echo $data;
JGrinon
  • 1,453
  • 1
  • 14
  • 36