PHP: get keys of independent arrays

2019-02-18 12:59发布

问题:

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:

$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"));


回答2:

Check out array_keys from both arrays and then map the positions. Like array_keys for your arrays will give you -

$arrayKeyOne = array('fruit', 'vegetables', 'meat', 'other');
$arrayKeyTwo = array('frewt', 'vetchteblz', 'meat', 'mother');

Then a $arrayKeyTwo[array_search($one, $arrayKeyOne)] shuld give you what you want. Let know if this helps.