Multi dimensional arrays & arrays as keys

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
barnbuster
Forum Newbie
Posts: 7
Joined: Fri May 02, 2003 10:19 am

Multi dimensional arrays & arrays as keys

Post by barnbuster »

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?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

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";}
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Could you show us your current code?

Mac
barnbuster
Forum Newbie
Posts: 7
Joined: Fri May 02, 2003 10:19 am

Post by barnbuster »

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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://www.php.net/manual/en/language.types.array.php
A key is either an integer or a string.
But it's strange it does not produce another error or at least a warning 8O
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)
	{
		
	}
}
?>
be sufficient?
barnbuster
Forum Newbie
Posts: 7
Joined: Fri May 02, 2003 10:19 am

Post by barnbuster »

Thanks volka, It looks as though your solution should work for me, i'll give it a try.
Post Reply