Page 1 of 1

missing function to return single array element?

Posted: Tue Mar 23, 2004 7:38 am
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! :)

Posted: Tue Mar 23, 2004 9:45 am
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];

Re: missing function to return single array element?

Posted: Tue Mar 23, 2004 9:55 am
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]()

Posted: Tue Mar 23, 2004 10:06 am
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!