Basic question really.
New to PHP..... Java programmer.
I have a series of nested multi dimentional arrays designed to create a referenced data structure relating to one variable. Because of the nature of the data I have used arrays as the Key and the Value.(similar idea to associating data with vectors or treemaps in Java)
This appears to work very well, however when I try and access the data using foreach(), the array I have used as the Key is not recognised by the parser.
Is what I am trying to achieve possible? Can I use an array as a key to store info, then access it again using foreach?
Multi dimensional arrays & arrays as keys
Moderator: General Moderators
-
barnbuster
- Forum Newbie
- Posts: 7
- Joined: Fri May 02, 2003 10:19 am
If I understand you correctly (I have no clue about Java) you are looking for something like this:
Code: Select all
<?php
$bla = array ("fruit"=>"apple","veggie"=>"cucumber","meat"=>"beef");
foreach ($bla as $key=>$value)
{echo "$key = $value";}
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
-
barnbuster
- Forum Newbie
- Posts: 7
- Joined: Fri May 02, 2003 10:19 am
I am not sure if what I'm doing is possible but this is what I am trying to achieve:
<?
$array3=array($array1=>$array2);
foreach ($array3 as $key1=>$value1) {
foreach ($key1 as $key2=>$value2)
{ }
}
?>
I have associated information between $array1, $array2 & $array3, and need to use an iteration to access it and manipulate it, hence using foreach.
I get a warning from the second foreach saying "invalid arguement", is this because foreach casts it out as a key and not an array, if that is the case would it be possible to type cast it as an array?(sounds unlikely)
If this doesn't work, is there any other way of accessing these arrays or will I have to introduce another indexing array which would mean both $array1 and $array2 could be cast out as values?
<?
$array3=array($array1=>$array2);
foreach ($array3 as $key1=>$value1) {
foreach ($key1 as $key2=>$value2)
{ }
}
?>
I have associated information between $array1, $array2 & $array3, and need to use an iteration to access it and manipulate it, hence using foreach.
I get a warning from the second foreach saying "invalid arguement", is this because foreach casts it out as a key and not an array, if that is the case would it be possible to type cast it as an array?(sounds unlikely)
If this doesn't work, is there any other way of accessing these arrays or will I have to introduce another indexing array which would mean both $array1 and $array2 could be cast out as values?
http://www.php.net/manual/en/language.types.array.php
Wouldbe sufficient?
But it's strange it does not produce another error or at least a warningA key is either an integer or a string.
Would
Code: Select all
<?php
$array1 = range(0,9);
$array2 = range(10,19);
$array3 = array(
array($array1, $array2)
);
foreach($array3 as $value)
{
foreach($value[0] as $key2=>$value2)
{
}
}
?>-
barnbuster
- Forum Newbie
- Posts: 7
- Joined: Fri May 02, 2003 10:19 am