How do you refer to arrays within arrays in PHP
If I did this:
<?php
$array = array();
$array['bar'] = "bar";
$array['foo'] 开发者_StackOverflow= array();
?>
How do I add values into the $array['foo'] array? (but not like add another array into that just keys and values)
$array['foo']['blah'] = 'asdf';
<?php
$array = array();
$array['bar'] = 'bar';
$array['foo'] = array();
$array['foo']['key'] = 'var'; // if you want it to be hash table
or
$array['foo'][] = 'var'; // generates the index for you.
?>
Then if you want to access that thing:
<?php echo $array['foo']['key']; ?>
Or if you used the [] way:
<?php echo $array['foo'][0]; ?>
$array['foo']['x'] = 'something';
$array['foo'][] = $your_value_to_insert;
You can do something like this:
$array['foo'] = array('id' => 1, 'name' => 'abc');
like @cyclone and:
$array['foo'] = array("foo" => "bar", 12 => true);
精彩评论