Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Returning an array from a function


[SOLVED] Returning an array from a function

Reply
  #1 (permalink)  
Old 07-23-08, 11:13 AM
Ligx Ligx is offline
Newbie Coder
 
Join Date: Nov 2005
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Returning an array from a function

Possible to use the array returned from a function directly?
PHP Code:

<?
function getTypeDetails($id){
    
$q mysql_query("SELECT * FROM types WHERE type_id=$id LIMIT 1");
    
$r mysql_fetch_array($q);
    
$a[0] = $id;
    
$a[1] = $r["name"];
    return 
$a;
}
?>
<body>
<?=getTypeDetails($myid)[1]?>
</body>
Or do I have to set the var first ? Because the above obviously doesn't work.
PHP Code:

<?
$type 
getTypeDetails($myid);
?>
<?=$type
[1]?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-23-08, 11:23 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
You can't do it this way. You have to assign it to a variable first, UNLESS:

You just want to get the first or the last item of the array. Then you can do:
PHP Code:

echo current(getTypeDetails($myid)); // Shows first item
echo end(getTypeDetails($myid)); // Shows last item 
... but if you're planning on using more than one of the items from the array, then I suggest signing it to variable first, because this way you don't have to call the function multiple times.


(In Javascript you can use this syntax, and I think it's very practical. I've seen quite a lot of people requesting this for PHP 6, so I guess we'll see what happens.)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 08-13-08, 12:30 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
I just want to add a quick alternative way using Objects.

PHP Code:

function getTypeDetails($id)

{
    
$query mysql_query("
        SELECT `type_id` AS `id`, `name`
        FROM `types`
        WHERE `type_id` = 
$id
        LIMIT 1
    "
);

    return 
mysql_fetch_object($query);
}

echo 
getTypeDetails(43)->id;
echo 
getTypeDetails(82)->name
(I was just in a similar situation and remembered this topic. )
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 08-13-08, 01:44 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
**********
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 08-13-08, 04:03 PM
Ligx Ligx is offline
Newbie Coder
 
Join Date: Nov 2005
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
I've always been using fetch array. Never really crossed my mind, that mysql has some different fetching functions =)

-Thanks Nico
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 04:54 PM
ASP upload prob minority ASP 1 06-27-05 09:35 AM
PHP Error Fairnie PHP 8 06-26-04 08:15 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM
Help trim code down TheLaughingBandit JavaScript 0 09-02-03 10:50 AM


All times are GMT -5. The time now is 07:19 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.