Access an array that has only one row

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
wtc
Forum Newbie
Posts: 12
Joined: Thu Oct 07, 2010 12:16 pm

Access an array that has only one row

Post by wtc »

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
User avatar
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

Post by Jonah Bron »

You can put indexes together to access lower levels of a nested array. For example:

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?"
So in your case, you would access 'someElement' like this:

Code: Select all

echo $my[0]['someElement'];
wtc
Forum Newbie
Posts: 12
Joined: Thu Oct 07, 2010 12:16 pm

Re: Access an array that has only one row

Post by wtc »

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?
User avatar
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

Post by Jonah Bron »

$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.

Code: Select all

print_r($MyArray);
This will tell us the index.
wtc
Forum Newbie
Posts: 12
Joined: Thu Oct 07, 2010 12:16 pm

Re: Access an array that has only one row

Post by wtc »

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! :?
User avatar
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

Post by Jonah Bron »

How is that array generated? I'm sure it could be changed to set the first index properly.
wtc wrote:The challenge now will be to know the key...
Easy cheesy lemon squeezy.

Code: Select all

$My = current($MyArray);
echo $My['name'];
Boom, done.
wtc
Forum Newbie
Posts: 12
Joined: Thu Oct 07, 2010 12:16 pm

Re: Access an array that has only one row

Post by wtc »

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
Post Reply