Req #55389 [Opn->Fbk]: Feature suggestion for array_search
| From: | cmb@php.net | Date: | Tue, 14 Jun 2016 17:42:46 +0000 |
| Subject: | Req #55389 [Opn->Fbk]: Feature suggestion for array_search | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-201612@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55389&edit=1
ID: 55389
Updated by: cmb@php.net
Reported by: pgreviews at gmail dot com
Summary: Feature suggestion for array_search
-Status: Open
+Status: Feedback
Type: Feature/Change Request
Package: Arrays related
Operating System: Ubuntu Linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
> array_search does not search recursively.
Actually, this is what the OP requests as enhancement. I'm not
sure if that would make sense, generally, though. At least the
given use case appears to be bogus, as the $filetypes array could
be laid out like so:
$filetype = array(
"txt" => "text",
"php" => "text",
"xml" => "text",
"html" => "text",
"htm" => "text",
"cgi" => "text",
"png" => "image",
"jpg" => "image",
"jpeg" => "image",
"svg" => "image",
"bmp" => "image"
);
And the "search" be done so:
echo $type = $filetypes['png'];
That's easy to understand and much more effecient, anyway.
Can you present a better use-case, pgreviews?
Previous Comments:
------------------------------------------------------------------------
[2011-08-10 07:52:36] laruence@php.net
Hi, this should be a bogus. array_search does not search recursively.
------------------------------------------------------------------------
[2011-08-09 23:26:04] pgreviews at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.array-search
---
Hello!
I am trying to create a simple function to check whether to show an image or display the contents of
a file. If the input is an image-type, show it as an image, and if it's text-type, show
it's source code.
I figured the best way to do this was to grab the file extension, and work from there. Here's
my code:
----------
$filetypes=array(
"text" =>
array("txt","php","xml","html","htm","cgi"),
"image" =>
array("png","jpg","jpeg","svg","bmp")
);
$type=array_search("png",$filetypes);
echo $type;
----------
Basically, I want it to return 'image', in this case, because the supplied input
'png' is in the 'image' key. However, array_search doesn't check arrays
within arrays.
If array_search could possibly be updated to provide a search within multidimensional arrays, that
would be fantastic!
Thanks!
-Scott (pgreviews@gmail.com)
Test script:
---------------
//Filetypes array, one key for text and one key for image extensions (shortened)
$filetypes=array(
"text" =>
array("txt","php","xml","html","htm","cgi"),
"image" =>
array("png","jpg","jpeg","svg","bmp")
);
//Look through the array for 'png', and return it's respective key. Then echo it.
echo $type=array_search("png",$filetypes);
Expected result:
----------------
I expect it to return 'image'.
Actual result:
--------------
(null, no output)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55389&edit=1