|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55389 Feature suggestion for array_search
Submitted: 2011-08-09 23:26 UTC Modified: 2016-06-26 04:22 UTC
Avg. Score:2.5 ± 1.5
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: pgreviews at gmail dot com Assigned: cmb (profile)
Status: No Feedback Package: Arrays related
PHP Version: Irrelevant OS: Ubuntu Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: pgreviews at gmail dot com
New email:
PHP Version: OS:


 [2011-08-09 23:26 UTC] pgreviews at gmail dot com
From manual page:


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:

		"text" => array("txt","php","xml","html","htm","cgi"),
		"image" => array("png","jpg","jpeg","svg","bmp")
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!


-Scott (

Test script:
//Filetypes array, one key for text and one key for image extensions (shortened)
		"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)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-10 07:52 UTC]
Hi, this should be a bogus. array_search does not search recursively.
 [2016-06-14 17:42 UTC]
-Status: Open +Status: Feedback
 [2016-06-14 17:42 UTC]
> 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?
 [2016-06-15 00:07 UTC]
-Assigned To: +Assigned To: cmb
 [2016-06-26 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Nov 29 05:01:27 2023 UTC