1

I have seen source code like:

$something = $sql['value']

I have searched a lot about it and I found that it's from arrays. But I didn’t understand the exact meaning.

For example,

$people = [

 'Susan' => [
    'Age' => 24,
'Phone' => '555-123-4567'
],

  'Jack' => [

  'Age' => 27,

  'Phone' => '555-9876-5432'

  ]
];

echo $people['Jack']['Age']; // 27

Can we write code like the following?

if(!empty($people)

    $something = $people['a value']

I just need to know how we can declare a variable and give a value in square brackets.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
code mania
  • 31
  • 2
  • 9
  • 1
    Please see PHP manual on arrays – Marcin Orlowski Jan 29 '14 at 10:56
  • Could you clarify, perhaps? What about accessing arrays indices puzzles you? The basic indices are numbers, like `$my_array[0];` but you can also use words or keys, as they are called, to do `$my_array["mrsnuggles"];`. – Lauri Elias Jan 29 '14 at 10:56

4 Answers4

0

If you are using $something = $people['a value'] it means you are assigning a value of the $people array having an index of a value.

So you don't have that and so it will throw you undefined index error.

You are using a nested associative array and you have to output using something like:

echo $people['Jack']['Age'];

As you wanted a brief example, say you have an array like

$people = array('name'=>'Jack');

Now, when you want to store the name in a variable, you use

$store_name = $people['name'];

echo $store_name; // Echoes "Jack"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • can you explain more breifly .In which situatio we use $something = $people['a value'] ? – code mania Jan 29 '14 at 10:57
  • Thanks ..is it used on in arrays ? – code mania Jan 29 '14 at 11:01
  • Thanks a lot .can you do me a help please ...i have another doubt too ..what is $row also act like in this way ?..http://stackoverflow.com/questions/21423303/rowcolumn-in-php – code mania Jan 29 '14 at 11:04
  • @codemania You are storing the results fetched from the database to array, and later you echo them with keys, it's just like what I explained above – Mr. Alien Jan 29 '14 at 11:05
  • So can we use another varibales instead of $row .? – code mania Jan 29 '14 at 11:09
  • @codemania why not, you can, but make sure you change the variables here too `$row['COLUMN_NAME']` <--- – Mr. Alien Jan 29 '14 at 11:10
  • you mean we must change the key value according to the condition ..right ? – code mania Jan 29 '14 at 11:17
  • Thanks alien have a look at here too http://stackoverflow.com/questions/21429441/update-a-variable-on-click....SEE $user_data is not defined as an array but the value is given as $username = $user_data['username'];..why is it like so ? – code mania Jan 29 '14 at 11:36
  • @codemania I can go on helping you but actually it won't help you, learn first, if you jump like this to advance topic, you will never learn – Mr. Alien Jan 29 '14 at 11:36
  • please tell me whats it ...i have learnt ..why is it not declaired as an array ? – code mania Jan 29 '14 at 11:48
  • as you explained above its applicable in arrays only ..but here $user_data is not declaired as array ? – code mania Jan 29 '14 at 11:48
  • In that code $username = $user_data['username']; is given .but $user_data is not declaired as an array ..so from where the value username is taken ? – code mania Jan 29 '14 at 12:02
  • i didnt get it ?..can you please explain why it is ? – code mania Jan 29 '14 at 13:10
0

Try this:

$people = array(
    'Susan' => array('Age' => 24, 'Phone' =>  '555-123-4567'),
    'Jack'  => array('Age' => 27, 'Phone' => '555-9876-5432')
);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NLSaini
  • 257
  • 1
  • 10
0

You can use array and write it like this

$people = array(
    'Susan' => array(
        'Age' => 24,
        'Phone' => '555-123-4567'
    ),
    'Jack' => array(
        'Age' => 27,
        'Phone' => '555-9876-5432'
    )
);

echo $people['Jack']['Age']; // 27


if(!empty($people)

    $something = $people['a value']
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
0

Square brackets mean index, so $people['a value'] is a value that lays under the 'a value' index of the $people array.

Square brackets are also used as a shortcut for array().. See it here-

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ellizory
  • 41
  • 4