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 873721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:59:10+00:00 2026-05-15T10:59:10+00:00

I have two arrays. Example of the first array: $arrayOne = array ( ‘fruit’

  • 0

I have two arrays.

Example of the first array:

$arrayOne = array
(
    'fruit' => array(
        'apples' => array(),
        'oranges' => array(),
        'bananas' => array()
    ),
    'vegetables' => array(
        'tomatoes' => array(),
        'carrots' => array(),
        'celery' => array(),
        'beets' => array
        (
            'bears' => array(),
            'battlestar-galactica' => array()
        ),
    ),
    'meat' => array(),
    'other' => array()
);

2nd:

$arrayTwo = array
(
    'frewt' => array(
        'aplz' => array(),
        'orangeez' => array(),
        'bunanahs' => array()
    ),
    'vetchteblz' => array(
        'toem8ohs' => array(),
        'careodds' => array(),
        'sell-R-e' => array(),
        'beats' => array
        (
            'bare z' => array(),
            'tablestar-neglectia' => array()
        ),
    ),
    'neat' => array(),
    'mother' => array()
);

Notice that the two arrays are in the exact same “format” (same number of dimensions, number of keys, order, etc., etc.), only the names of the keys differ. (The array keys basically hold all the data.)

I have a few variables that address the keys of the first array ($arrayOne). E.g. $one would address the first dimension of the first array, so it’s value (string) would be one out of 'fruit', 'vegetables', 'meat' or 'other'.
$two would be 'apples' or 'oranges' or 'bananas' or 'tomatoes' or 'carrots', etc., you get the idea. (There’s vars for each dimension)
As I said, those variables only address $arrayOne. I want to be able to address the keys in the second array too, though. Meaning, by looking at the value of $one I want to be able to get the array_key of both arrays.

  • 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-15T10:59:11+00:00Added an answer on May 15, 2026 at 10:59 am
    $arrayOne = //...
    
    $arrayTwo = //...
    
    function getPosition(array $arr, $key) {
        $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr),
            RecursiveIteratorIterator::SELF_FIRST);
        $pos = array();
        foreach ($it as $k => $v) {
            if (count($pos) - 1 > $it->getDepth()) {
                array_pop($pos);
                $pos[$it->getDepth()]++;
            }
            elseif (count($pos) - 1 < $it->getDepth()) {
                array_push($pos, 0);
            }
            else {
                $pos[$it->getDepth()]++;
            }
            if ($k === $key) {
                return $pos;
            }
        }
    }
    
    function getElementKey(array $arr, array $position) {
        $cur = $arr;
        $curkey = null;
        foreach ($position as $p) {
            reset($cur);
            for ($i = 0; $i < $p; $i++) {
                next($cur);
            }
            $curkey = key($cur);
            $cur = current($cur);
        }
        return $curkey;
    }
    
    var_dump(getPosition($arrayOne, "battlestar-galactica"));
    var_dump(getElementKey($arrayTwo, array(1, 3, 1)));
    

    gives:

    array(3) {
      [0]=>
      int(1)
      [1]=>
      int(3)
      [2]=>
      int(1)
    }
    string(19) "tablestar-neglectia"
    

    You can feed the result of getPosition to getElementKey:

    getElementKey($arrayTwo, getPosition($arrayOne, "battlestar-galactica"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that contains two arrays, the first is a slope array:
I have two arrays. The first array contains some values while the second array
I have two arrays, each containing strings. The first array is a list of
I have two arrays, the first one contents numbers and the second one use
I have two arrays... $Name = array(a, b, c, d); $Value = array(1, 2,
I have two pair of arrays. In each pair one array contains the index
I have two arrays. The first one contains an id, username, and password from
I have two arrays. For example: var bool = [true, false, true]; var vals
I have two arrays, @names and @employees , that are filled with strings that
I have two arrays. I'd like to copy ranges of data from one of

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.