Jump to content

Double Arrays?


TJMAudio

Recommended Posts

I was just reading through a popular script and I noticed something about it, it seems to use "double arrays".  How do these work?  Here is an example..

[code]$config['Database']['host'] = 'localhost';
$config['Database']['user'] = ' ';[/code]

Can you just define these, without calling an array?  They are just like that, they don't have a $config = array() in the script that I can see.
Link to comment
Share on other sites

You should check out the PHP documentation on arrays to get a thorough understanding of arrays (http://us3.php.net/types.array). Arrays in PHP can store pretty much everything including other arrays, values, and even objects. I've used arrays up to 5 levels deep (though not by choice) and I'm sure you can go much deeper than that.
Link to comment
Share on other sites

It's called a multidimensional array, and you don't technically need to declare any array.

There are many uses for mutidimensional arrays.

One example would be to copy database results into an array for further processing. The firrst index could identify the record and the second index could identify the column (data item) inthat record
Link to comment
Share on other sites

So if I were to make my own to hold data, I would write it like this?

[code]$config = array("Database" => array("type", "host", "user", "pass"));

// Set Database Information, do not edit database type
$config['Database']['type'] = 'MySQL';
$config['Database']['host'] = 'localhost';
$config['Database']['user'] = '';
$config['Database']['pass'] = '';

?>[/code]
Do I have it right?  Also, what are some benefits (or needs) to use multidimensional arrays?  I understand that you gave me an example, but it went right over my head.
Link to comment
Share on other sites

No, your example creates an array that looks like:
[code]
Array
(
    [Database] => Array
        (
            [0] => type
            [1] => host
            [2] => user
            [3] => pass
        )

)
[/code]

To do what you want, do this:
[code]<?php
$config = array("Database" => array());

// Set Database Information, do not edit database type
$config['Database']['type'] = 'MySQL';
$config['Database']['host'] = 'localhost';
$config['Database']['user'] = '';
$config['Database']['pass'] = '';

?>[/code]

Which generates:
[code]
Array
(
    [Database] => Array
        (
            [type] => MySQL
            [host] => localhost
            [user] =>
            [pass] =>
        )

)
[/code]

Ken
Link to comment
Share on other sites

Here's an example that may better illustrate the use of multidimensional arrays. Let's say you have three users who's data you want to put into an array. You want to include their id#, name, & email address. You could do something like this (using id as the first index as well as a value):

[code]<?php
$users = array();

$users[0]['id'] = 0;
$users[0]['name'] = 'John';
$users[0]['email'] = 'john@yahoo.com';
$users[1]['id'] = 1;
$users[1]['name'] = 'Mary';
$users[1]['email'] = 'mary@gmail.com';
$users[2]['id'] = 2;
$users[2]['name'] = 'Alex';
$users[2]['email'] = 'alex@hotmail.com';
?>[/code]

You could then access any user's data by using their id in the first index and the data name you want as the wnd index:

echo $users[1]['email] would output "mary@gmail.com"
Link to comment
Share on other sites

in many cases these arrays would be more useful if the keys were switched. I'll use mjdamato's as an example.

with this array
[code]<?php
$users[0]['id'] = 0;
$users[0]['name'] = 'John';
$users[0]['email'] = 'john@yahoo.com';
$users[1]['id'] = 1;
$users[1]['name'] = 'Mary';
$users[1]['email'] = 'mary@gmail.com';
$users[2]['id'] = 2;
$users[2]['name'] = 'Alex';
$users[2]['email'] = 'alex@hotmail.com';
?>[/code]

All you can really do is loop through it. There is no simply way to find anything in there.

if the array were like so however...
[code]<?php
$users['id'][0] = 0;
$users['name'][0] = 'John';
$users['email'][0] = 'john@yahoo.com';
$users['id'][1] = 1;
$users['name'][1] = 'Mary';
$users['email'][1] = 'mary@gmail.com';
$users['id'][2] = 2;
$users['name'][2] = 'Alex';
$users['email'][2] = 'alex@hotmail.com';
?>[/code]

I can now search that array for information based on another piece of info.

so say I wanted to print email address of all the people called mary (image a bigger array with more than one mary). I can use
[code]<?php
$marys = array_keys($users['name'], 'Mary');
foreach($marys as $key => $val)
{
echo $users['email'][$val];
}

keeping string keys as the major key in a 2D array has distinct advantages...[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.