niconico Posted November 19, 2006 Share Posted November 19, 2006 I've seen a lot of examples on how to create arrays and manipulate them, but they all concern static arrays like the example below given in the documentation:[code]$fruits = array ( "fruits" => array("a" => "orange", "b" => "banana", "c" => "apple"), "numbers" => array(1, 2, 3, 4, 5, 6), "holes" => array("first", 5 => "second", "third"));[/code]I've searched the documentation and various forums and I can't find how to dynamically build something like the above. In my case I need something like below that I can populate as items are found.[code]$items= array ( "item_1" => array("item_1.1" => array("item_1.1.1" => "orange", "item_1.1.2 => "pear")), "item_2" => array("item_2.1" => array("item_2.1.1" => 126, "item_2.1.2 => 621)));[/code]The third level (item_x.x.x) values would already exist in an array that then needs to be added to either a new or existing second level, which then needs to be added to an existing level 1 item.If someone could point me in the right direction I would appreciate it. Thanks! Link to comment https://forums.phpfreaks.com/topic/27771-building-arrays/ Share on other sites More sharing options...
zq29 Posted November 19, 2006 Share Posted November 19, 2006 You can add to arrays, be it either a value, or another array with $myArray[] = "new value"; or likewise, $myArray[] = array("another key, in another array"); Link to comment https://forums.phpfreaks.com/topic/27771-building-arrays/#findComment-127044 Share on other sites More sharing options...
niconico Posted November 20, 2006 Author Share Posted November 20, 2006 Thanks SemiApocalyptic!I'm not php fluent. But as far as other languages go I find it interesting that $myArray[] = "new value" would add as opposed to create. But this does work, except that when using the above method for adding an element how does one [b]index[/b] the added value? Link to comment https://forums.phpfreaks.com/topic/27771-building-arrays/#findComment-127432 Share on other sites More sharing options...
kenrbnsn Posted November 20, 2006 Share Posted November 20, 2006 Using [code]<?php$myarray[] = "value";?>[/code]will create the array [b]$myarray[/b] if it doesn't already exist. When a new value is added to an existing array, then next numerical index is used.You can also create or add to an associative array in the same manner:[code]<?php$myassoc_array['one'] = 'two';echo '<pre>' . print_r($my assoc_array,true) . '</pre>'; // dump the contents of the array to the screen?>[/code]You can always use the [url=http://www.php.net/var_dump]var_dump()[/url], [url=http://www.php.net/var_export]var_export()[/url], or [ur=http://www.php.net/print_r]print_r()[/url] functions to dump the contents of the array to the screen.Ken Link to comment https://forums.phpfreaks.com/topic/27771-building-arrays/#findComment-127488 Share on other sites More sharing options...
Jenk Posted November 20, 2006 Share Posted November 20, 2006 [quote author=kenrbnsn link=topic=115546.msg470946#msg470946 date=1164038246]Using [code]<?php$myarray[] = "value";?>[/code]will create the array [b]$myarray[/b] if it doesn't already exist.[/quote].. and will generate an error of E_NOTICE level. Link to comment https://forums.phpfreaks.com/topic/27771-building-arrays/#findComment-127495 Share on other sites More sharing options...
kenrbnsn Posted November 20, 2006 Share Posted November 20, 2006 An E_NOTICE is "Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script." as described in the manual.I rarely use that constuct anymore, since it will cause problems if you're not carefull. It is always best to tell PHP that some is an array before using it as one.[code]<?php$myarray = array();$myarray['one'] = 'two';$myarray[] = 'testing';$myarray['two'] = 'one';$myarray[] = 'testing123';echo '<pre>' . print_r($myarray,true) . '</pre>';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/27771-building-arrays/#findComment-127509 Share on other sites More sharing options...
niconico Posted November 21, 2006 Author Share Posted November 21, 2006 I finally get it with the last example.[code]$myarray = array();$myarray['one'] = 'two';[/code]I appreciate it. Thanks guys! Link to comment https://forums.phpfreaks.com/topic/27771-building-arrays/#findComment-127700 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.