如何将新数组与原始数组匹配

It is hard to explain, but i will try my best.

In html, got a row of item A B C D E. They are not in array.

Now got another array which is Num[]={1,2,3,4,5,6,7,8,9}.

Imagine A=1, B=2, C=3, D=4, E=5, 0=6, 0=7, 0=8, 0=9.

Now, i use checkbox to check the item B,C,E and form an array check[]={B,C,E}.

Question is: How to know that the new array check[0]=2, check[1]=3, check[2]=5?

Sorry, i cant explain well, for me its complicated.(no code provided because its too long)

A simple solution would be a map, which maps the content ABC to Num.

$abc = ['A', 'B', 'C', 'D', 'E'];
$num = [1, 2, 3, 4, 5];
$map = [];
foreach ($abc as $index => $val) {
    $map[$val] = $num[$index];
}

$x = ['A', 'D'];
foreach ($x as $index => $val) {
    echo "x[$index] = {$map[$val]}
";
}

If you have to do this only for one element, you could also skip the map creation part and use array_search;

echo $num[array_search('A', $abc)];

Using array_intersect() which matches the two arrays up (the first one and the one with the values you want to find) gives you an array with just those items and the keys from the first array. You can then use array_intersect_key() which extracts the items from the second array with the keys extracted in the first step.

$a1 = ['A', 'B', 'C', 'D', 'E'];
$a2 = [1,2,3,4,5];
$a3 = ['A', 'D'];
$a4 = array_intersect_key($a2, array_intersect($a1, $a3));
print_r($a4);

which outputs... Array

(
    [0] => 1
    [3] => 4
)

You can use array_values() if you want the keys to be sequential.

$a4 = array_values(array_intersect_key($a2, array_intersect($a1, $a3)));

Try with array_map

$abc = array('A', 'B', 'C', 'D', 'E');
$num = array(1, 2, 3, 4, 5);

$newAbc = array('A', 'D');

$newNum = array_map(function($element) use($abc, $num) {
    return $num[array_search($element, $abc)];
}, $newAbc);

// Outputs: 1, 4
print_r($newNum);

The array_intersect solution is very clean too :)

<?php

$chars = ['A', 'B' , 'C' , 'D' , 'E'];
$ints  = [ 1 ,  2 ,   3 ,   4 ,   5];

$given   = ['A', 'D'];
$require = [1, 4];

Combine and intersect:

$ints_chars = array_combine($ints, $chars);
var_export($ints_chars);

$result = array_keys(array_intersect($ints_chars, $given));
var_dump($result === $require);

Output:

array (
  1 => 'A',
  2 => 'B',
  3 => 'C',
  4 => 'D',
  5 => 'E',
)bool(true)

Use a map:

$result = [];
$chars_ints = array_combine($chars, $ints);
foreach($given as $char)
    $result[] = $chars_ints[$char];

var_dump($result === $require);

Output:

bool(true)

Use a map, but notice your number range is just a shifted index:

$result = [];
$chars_flip = array_flip($chars);
foreach($given as $char)
    $result[] = $chars_flip[$char] + 1;

var_dump($result === $require);

Output:

bool(true)

Finally by shifting and mutating the original array:

array_unshift($chars, null);
$result = array_keys(array_intersect($chars, $given));

var_dump($result === $require);

Output:

bool(true)