note 101060 added to language.types.array
| From: | Anonymous at osu1 dot php dot net | Date: | Tue, 23 Nov 2010 22:27:28 +0000 |
| Subject: | note 101060 added to language.types.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174301@lists.php.net to get a copy of this message | ||
In case you had to get an element from a function-returned array without having to save the array to
a variable, here's a workaround I've come up with.
<?php
$stuff = 'something random here';
// To get the first element in the array, use reset
echo reset(explode(' ', $stuff));
// Returns "something"
// To get the last element, use end
echo end(explode(' ', $stuff));
// Returns "here"
// For an in-between element, you can use array_search on the flipped array
$i = 1; //key for the element
echo array_search($i, array_flip(explode(' ', $stuff)));
// Returns "random"
?>
----
Server IP: 80.64.47.13
Probable Submitter: 84.127.60.133
----
Manual Page -- http://www.php.net/manual/en/language.types.array.php
Edit -- https://master.php.net/note/edit/101060
Del: integrated -- https://master.php.net/note/delete/101060/integrated
Del: useless -- https://master.php.net/note/delete/101060/useless
Del: bad code -- https://master.php.net/note/delete/101060/bad+code
Del: spam -- https://master.php.net/note/delete/101060/spam
Del: non-english -- https://master.php.net/note/delete/101060/non-english
Del: in docs -- https://master.php.net/note/delete/101060/in+docs
Del: other reasons-- https://master.php.net/note/delete/101060
Reject -- https://master.php.net/note/reject/101060
Search -- https://master.php.net/manage/user-notes.php