Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:15:48+00:00 2026-05-28T19:15:48+00:00

Say I have an array such as: $arr[] = array(id => 11, name =>

  • 0

Say I have an array such as:

$arr[] = array("id" => 11, "name" => "First");
$arr[] = array("id" => 52, "name" => "Second");
$arr[] = array("id" => 6, "name" => "Third");
$arr[] = array("id" => 43, "name" => "Fourth");

I would like to get the name correspondent to a certain ID so that I can do:

$name = findNameFromID(43);

and get, for instance, “Fourth”.

I thought of using array_filter but I am a bit stuck on how to correctly pass a variable. I have seen questions such as this one but I don’t seem to be able to extend the solution to a multidimensional array.

Any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:15:48+00:00Added an answer on May 28, 2026 at 7:15 pm
    findNameFromID($array,$ID) {
         return array_values(array_filter($array, function($arrayValue) use($ID) { return $arrayValue['id'] == $ID; } ));
    }
    
    $name = findNameFromID($arr,43);
    if (count($name) > 0) {
        $name = $name[0]['name'];
    } else {
        echo 'No match found';
    }
    

    PHP 5.3.0 and above

    EDIT

    or variant:

    findNameFromID($array,$ID) {
        $results = array_values(array_filter($array, function($arrayValue) use($ID) { return $arrayValue['id'] == $ID; } ));
        if (count($results) > 0) {
            return $name[0]['name'];
        } else {
            return FALSE;
        }
    }
    
    $name = findNameFromID($arr,43);
    if (!$name) {
        echo 'No match found';
    }
    

    EDIT #2

    And from PHP 5.5, we can use array_column()

    findNameFromID($array, $ID) {
        $results = array_column($array, 'name', 'id');
        return (isset($results[$ID])) ? $results[$ID] : FALSE;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an array arr . When would the following not give
Let's say I have an array, @theArr, which holds 1,000 or so elements such
Let's say I have array of bytes: byte[] arr = new byte[] { 0,
Say I have an array of table DDL queries and I want to get
Say I have an array like this: [white, red, blue, red, white, green, red,
Can I store an array such as [1,2,3,4] or some JSON like {name:John Johnson,street:Oslo,
Let's say I have a 2D array: int[][] a = new int[4][3]; populated such
let's say I have a matrix (array) like this example, but much larger: 0
Lets say I have a array defined in Groovy like this def int[] a
Lets say I have an array of interlaced data, such as 1a2b3c4d5e, and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.