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

  • Home
  • SEARCH
  • 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 7818655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:39:06+00:00 2026-06-02T06:39:06+00:00

I am trying to write a piece of code that searches one column of

  • 0

I am trying to write a piece of code that searches one column of 2-D array values and returns the key when it finds it. Right now I have two functions, one to find a value and return a boolean true or false and another (not working) to return the key. I would like to merge the two in the sense of preserving the recursive nature of the finding function but returning a key. I cannot think how to do both in one function, but working key finder would be much appreciated.

Thanks

function in_array_r($needle, $haystack, $strict = true) {
foreach ($haystack as $item) {
    if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
        return true;
    }
}

return false;

}

function loopAndFind($array, $index, $search){
     $returnArray = array();
     foreach($array as $k=>$v){
           if($v[$index] == $search){   
                $returnArray[] = $k;
           }
     }
     return $returnArray;

}`

Sorry, I meant to add an example. For instance:

Array [0]{
[0]=hello
[1]=6
}
[1]
{
[0]=world
[1]=4
}

and I want to search the array by the [x][0] index to check each string of words for the search term. If found, it should give back the index/key in the main array like “world” returns 1

  • 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-06-02T06:39:08+00:00Added an answer on June 2, 2026 at 6:39 am

    This works:

    $array = array(array('hello', 6), array('world', 4));
    $searchTerm = 'world';
    
    foreach ($array as $childKey => $childArray) {
        if ($childArray['0'] == $searchTerm) {
            echo $childKey; //Your Result
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a unit test for a piece of code that generates
I'm trying to write a piece of code that reads a file line by
I'm trying to write a piece of code that find shortest path in an
I'm trying to write a small piece of code that merges lines alternatively from
I am trying to write a piece of code that can parse any xml
Am trying to write a piece of python code that calculate and print 1000
I'm trying to write a piece of code that will do the following: Take
I'm trying to write a small piece of code that passes a small formula
Hi I'm trying to write a piece of code for a simple verification method
Begginer's question. I'm trying to write small piece of code in Visual Studio (VB.net)

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.