Is there a way in PHP to access an array that has only a single row, without having to use the foreach method?
This works:
foreach ($myArray = $my) {
echo "$my['someElement'];
}
But in my specific case, I know ahead of time the array will always only have one row of data returned. In the example above, I just want to get to 'someElement' without needing a loop. Possible?
Sorry if this is a newbie question...only been into PHP for about 2 months now, and trying to learn solely on line is tough sometimes to find answers!
Thanks much,
wtc
Access an array that has only one row
Moderator: General Moderators
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Access an array that has only one row
You can put indexes together to access lower levels of a nested array. For example:
So in your case, you would access 'someElement' like this:
Code: Select all
$array = array(
'hello',
array(
'world',
array(
'how are you?'
)
)
);
echo $array[0]; // "hello"
echo $array[1][0]; // "world"
echo $array[1][1][0]; // "how are you?"Code: Select all
echo $my[0]['someElement'];Re: Access an array that has only one row
Thanks, but not quite what I was looking for.
In the example, I know that $MyArray has only 1 record (if I do this: echo count($MyArray), I get 1).
Assume one of the data elements inside $MyArray is "name".
When I try to write something like echo $MyArray['name'], I get nothing. I have also tried echo $MyArray[0][0] and $MyArray[0]['name'], and I get nothing.
But when I put it in a Foreach loop as in my initial post above (foreach $MyArray as $My { ...} ), $My['name'] contains the value I am expecting. So the foreach method works fine.
It just seems to me, when I only ever have one row of data in the array, there must be a simple way or function that allows the programmer to gain access without the need for the foreach function/method.
So my question is really, is there a way to access the contents of $MyArray, without having to use a Foreach loop and put the data in yet another array? Or, is foreach simply the way to go?
In the example, I know that $MyArray has only 1 record (if I do this: echo count($MyArray), I get 1).
Assume one of the data elements inside $MyArray is "name".
When I try to write something like echo $MyArray['name'], I get nothing. I have also tried echo $MyArray[0][0] and $MyArray[0]['name'], and I get nothing.
But when I put it in a Foreach loop as in my initial post above (foreach $MyArray as $My { ...} ), $My['name'] contains the value I am expecting. So the foreach method works fine.
It just seems to me, when I only ever have one row of data in the array, there must be a simple way or function that allows the programmer to gain access without the need for the foreach function/method.
So my question is really, is there a way to access the contents of $MyArray, without having to use a Foreach loop and put the data in yet another array? Or, is foreach simply the way to go?
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Access an array that has only one row
$MyArray[0]['name'] would be correct, so obviously the first element does not have a key of 0. Put this into the code and post the output here.
This will tell us the index.
Code: Select all
print_r($MyArray);Re: Access an array that has only one row
Wow - print_r came back with something like "Array([15]=>Array..." so I tried stuffing 15 into the statement echo $MyArray[15]['name']; and sure enough, it worked.
Now, why the key was 15....and not 0....I have no clue!
So thanks for putting me on the track. The challenge now will be to know the key...
Ah heck, the foreach loop method is looking better and better!
Now, why the key was 15....and not 0....I have no clue!
So thanks for putting me on the track. The challenge now will be to know the key...
Ah heck, the foreach loop method is looking better and better!
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Access an array that has only one row
How is that array generated? I'm sure it could be changed to set the first index properly.
Boom, done.
Easy cheesy lemon squeezy.wtc wrote:The challenge now will be to know the key...
Code: Select all
$My = current($MyArray);
echo $My['name'];Re: Access an array that has only one row
Ha! that's it - "current"!!! that is essentially what I was looking for, thanks much!
I am actually working on an existing application, and am loathe to go back into the original code to find out - and change - why the array has an odd key in the first place. I'd probably break 100 other things. But, no worries - I love the $my = current($myarray) method - just what I had in mind.
thanks again,
wtc
I am actually working on an existing application, and am loathe to go back into the original code to find out - and change - why the array has an odd key in the first place. I'd probably break 100 other things. But, no worries - I love the $my = current($myarray) method - just what I had in mind.
thanks again,
wtc