missing function to return single array element?

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
shy
Forum Newbie
Posts: 5
Joined: Tue Aug 26, 2003 1:31 pm
Location: Portugal
Contact:

missing function to return single array element?

Post by shy »

Really, I am no PHP newbie and I have searched the array-functions list thoroughly (at least I think I did.)

But... it seems to me there is no way to do this in ONE step:
Create an array and have one element returned as a string.

in Java / JavaScript you can simply append the desired elements index to the function creating the array like this:

Code: Select all

var myFilename = myFileWithExtention.slice('.')ї0];
In PHP it seems you have to code:

Code: Select all

$tmp = explode('.', $myFileWithExtention);
$myFilename = $tmpї0]
Sure shift() and pop() can do when it is the first or the last element you want and you don't need the original array anymore, but that is not always the case.

For example:
With an image file upload, you can determine the image type by calling getimagesize() on the file. The image type information is in the 3rd element of the array returned.
Now it would be so nice to code:

Code: Select all

$image_format = getimagesize($ul_file)ї2];
Or wouldn't it?!

Please tell me that I am just plain blind and that, of course, there is a way to do this in PHP!

Thanx! :)
Last edited by shy on Tue Mar 23, 2004 9:50 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I think what you're looking for is: [php_man]array_slice[/php_man].

I'm not too familiar with curly braces, but will this work for you?

Code: Select all

$desired_string = {array_slice($target_array,2,1)}[0];
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Re: missing function to return single array element?

Post by m3mn0n »

shy wrote: But... it seems to me there is no way to do this in ONE step:
Create an array and have one element returned as a string.
Sure it's possible.

Research at these pages:

[php_man]array[/php_man]()
[php_man]foreach[/php_man]()
[php_man]array_slice[/php_man]()

I'm sure the answer you're looking for is there.
shy wrote:In PHP it seems you have to code:

Code: Select all

$tmp = explode($myFileWithExtention);
$myFilename = $tmpї0]
That would return an error. [php_man]explode[/php_man]() requires two parameters, and the second one must be a string. The first is what to seperate the string by.
shy wrote:With an image file upload, you can determine the image type by calling getimagesize() on the file. The image type information is in the 3rd element of the array returned.
Now it would be so nice to code:

Code: Select all

$image_format = getimagesize($ul_file)ї2];
Or wouldn't it?!
This is correct, as shown here: [php_man]getimagesize[/php_man]()
shy
Forum Newbie
Posts: 5
Joined: Tue Aug 26, 2003 1:31 pm
Location: Portugal
Contact:

Post by shy »

To Pickle:
No, it's not array_slice() as this still returns an array.

And, unfortunately, the curlies don't seem to do the trick either.. :(

To Sami:
I noticed my ommitance of the first argument my the example of explode() and corrected it.

As to your suggestions:
array() - I don't want to create an array; I want to extract a single element from an array as a string.

foreach() - I don't want to iterate through the array either; this would leave me with even more code to write.

array_slice() - as I mentioned in regard to Pickles post: it again returns an array.


Please tell me if I am overseeing something here!
Post Reply