Re: gettting last assigned key in an array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Feb 10, 2006 at 11:39:13AM -0800, jonathan wrote:
> 
> i"m mapping some data from a sql select into a custom object that  
> will have a couple associative arrays. I have some code that I think  
> looks a little ugly such as:
> 
>         $x=0;
> 	if($row['slot']>=2)
> 	{
> 	    $this->menu_item[$x]['item_id']=$row['item_id'];
>             $this->menu_item[$x]['name']=$row['name'];
> 	    $this->menu_item[$x]['item_price']=$row['item_price'];
> 	    $x++;
> 	}
> 
> I'd rather do something like this:
> 	if($row['slot']>=2)
> 	{
> 	    $this->menu_item[]['item_id']=$row['item_id'];
>             $y=get_last_key();
>             $this->menu_item[$y]['name']=$row['name'];
> 	    $this->menu_item[$y]['item_price']=$row['item_price'];
> 	}
> 
> but don't know if php has a built in for the "get_last_key()"; any  
> thoughts would be appreciated (including whether you think the  
> original code snippet is fine.

To get the last key:
  end($array);
  $last_key = key($array);

How i would approach the above code:

  $this->menu_item[] = $row;

Or/And if $row had more fields you really wanted to name them
differently:

  $item['item_id'] = $row['item_id'];
  $item['name']    = $row['name'];
  $item['price']   = $row['item_name'];
  $this->menu_item[] = $item;


Curt.
-- 
cat .signature: No such file or directory

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux