Jump to content

Building Arrays


niconico

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.