Newbie Question on Accessing Arrays via Index Key

Newbie Question on Accessing Arrays via Index Key

am 24.09.2007 22:52:53 von Vik Rubenfeld

I've got an multi-dimensional array that looks like this, when I examine
it in the Zend Debugger:

[]$ArrayOfData = Array[15]
[]1001 = Array[12]
tin = (double) 0.032
iron = (double) 101.24
Alloy_ID = (strin:4) 1001
,,,
[]1019 = Array[12]
tin = (double) 4.49
iron = (double) 53.03
Alloy_ID = (strin:4) 1019
...

Etc. I want to add a new field to each sub-array. For various reasons,
I can't use ForEach; I need to access the sub-arrays by their index key.

I tried doing this:

$theKey = 1001;
data_for_new_field = "55";
$ArrayOfData[$theKey]['new_field'] = $data_for_new_field;

But, that just added a new array element to $ ArrayOfData, like this:

[]$ArrayOfData = Array[15]
[]"1001" = Array [1]
new_field = (string:2) 55;
[]1001 = Array[12]
tin = (double) 0.032
iron = (double) 101.24
Alloy_ID = (strin:4) 1001
,,,
[]1019 = Array[12]
tin = (double) 4.49
iron = (double) 53.03
Alloy_ID = (strin:4) 1019
...

What's the correct way to access elements in $ArrayOfData, by their
index key?

Thanks very much in advance to all for any info.

Re: Newbie Question on Accessing Arrays via Index Key

am 24.09.2007 23:03:06 von Vik Rubenfeld

I got it figured out. I was reading in the string I was using for the
index, from a CSV file, and it came in with the double-quotes still
around it. So instead of accessing $array[1001], I was accessing
$array["1001"].

Re: Newbie Question on Accessing Arrays via Index Key

am 24.09.2007 23:15:38 von Steve

"Vik Rubenfeld" wrote in message
news:vikr-654091.14030624092007@earthlink.vsrv-sjc.supernews .net...
>I got it figured out. I was reading in the string I was using for the
> index, from a CSV file, and it came in with the double-quotes still
> around it. So instead of accessing $array[1001], I was accessing
> $array["1001"].

either way, i always suggest using foreach constructs for iteration since
that makes this problem disappear...and, should a key/index be unset/missing
in your array, foreach won't blow up and iterate over all elements, rather
than the ones you 'think' should be there.

but that could just be me and my 0.02 usd.