TJMAudio Posted December 20, 2006 Share Posted December 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/31408-double-arrays/ Share on other sites More sharing options...
linuxdream Posted December 20, 2006 Share Posted December 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/31408-double-arrays/#findComment-145399 Share on other sites More sharing options...
Psycho Posted December 20, 2006 Share Posted December 20, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/31408-double-arrays/#findComment-145400 Share on other sites More sharing options...
TJMAudio Posted December 20, 2006 Author Share Posted December 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/31408-double-arrays/#findComment-145412 Share on other sites More sharing options...
kenrbnsn Posted December 20, 2006 Share Posted December 20, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/31408-double-arrays/#findComment-145438 Share on other sites More sharing options...
Psycho Posted December 21, 2006 Share Posted December 21, 2006 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'] = '[email protected]';$users[1]['id'] = 1;$users[1]['name'] = 'Mary';$users[1]['email'] = '[email protected]';$users[2]['id'] = 2;$users[2]['name'] = 'Alex';$users[2]['email'] = '[email protected]';?>[/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 "[email protected]" Quote Link to comment https://forums.phpfreaks.com/topic/31408-double-arrays/#findComment-145573 Share on other sites More sharing options...
ToonMariner Posted December 21, 2006 Share Posted December 21, 2006 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'] = '[email protected]';$users[1]['id'] = 1;$users[1]['name'] = 'Mary';$users[1]['email'] = '[email protected]';$users[2]['id'] = 2;$users[2]['name'] = 'Alex';$users[2]['email'] = '[email protected]';?>[/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] = '[email protected]';$users['id'][1] = 1;$users['name'][1] = 'Mary';$users['email'][1] = '[email protected]';$users['id'][2] = 2;$users['name'][2] = 'Alex';$users['email'][2] = '[email protected]';?>[/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] Quote Link to comment https://forums.phpfreaks.com/topic/31408-double-arrays/#findComment-145580 Share on other sites More sharing options...
TJMAudio Posted December 21, 2006 Author Share Posted December 21, 2006 To make it so I could have $config['Database'] and $config['Site'], etc, would I just do this:[code]$config = array('Database', 'Site' => array());[/code]Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/31408-double-arrays/#findComment-145703 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.