• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

PHP: add items to array

My code doesn't work....
<pre><?php


$a =array(
            ['id'] = 123,
            ['name'] = 'abc',
            ['link'] = 'xyz',
);

print_r($a);


?></pre>

Open in new window

0
hankknight
Asked:
hankknight
2 Solutions
 
Ludwig DiehlSystems ArchitectCommented:
my friend it's not the correct way. You can try either of these:


1. $a=array('id'=>123,'name'=>'abc','link'=>'xyz');

2. $a['id']=123;
     $a['name']='abc';
     $a['link']='xyz';
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please try:
$a =array(
            'id' => 123,
            'name' =>> 'abc',
            'link' => 'xyz',
);

Open in new window

0
 
hankknightAuthor Commented:
Thanks, I fixed a typo in angelIII's post:
$a =array(
            'id' => 123,
            'name' => 'abc',
            'link' => 'xyz',
);

Open in new window

0
 
Ray PaseurCommented:
I think angelIII has it right with the slight typo on line 3 where one of the > should be omitted.

I like to do array definitions like this.  It is a trick I learned from programmers at Noblis.  Makes it VERY easy to spot your errors.
$a = array
( 'id'   => 123
, 'name' => 'abc'
, 'link' => 'xyz'
)
;

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now