简体   繁体   中英

How can i find the last key of an array where can be found a value from another array?

I have this array that has some sql operators:

Array
(
    [0] => =
    [1] => <
    [2] => >
    [3] => !=
    [4] => <>
    [5] => >=
    [6] => <=
)

And this array that is a preg_split of an sql statement:

Array
(
    [0] => Array
        (
            [0] => name
            [1] => 0
        )

    [1] => Array
        (
            [0] => =
            [1] => 5
        )

    [2] => Array
        (
            [0] => 'Emma'
            [1] => 7
        )

    [3] => Array
        (
            [0] => AND
            [1] => 14
        )

    [4] => Array
        (
            [0] => age
            [1] => 18
        )

    [5] => Array
        (
            [0] => <=
            [1] => 22
        )

    [6] => Array
        (
            [0] => '21'
            [1] => 25
        )

)

In second array at [5][0] i have a '<=' operator that can be found at [6] in that first array(that contains the sql operators). Now this is not a static statement, sometimes i might want to use '=' or '>' instead of '<='.

Is there a way to dynamically determine where the last operator can be found in this statement array?

Thank you!:D

提取0列并搜索:

$result = array_search($first_array[6], array_column($second_array, 0));

For the last position of an operator and the last operator from all matches returned by the split operation:

$lastIndex = null;
$lastPosition = null;
$lastOperator = null;

foreach ($matches as $index => $match) {
    $isOperator = in_array($match[0], $operators, true);
    if ($isOperator) {
        $lastIndex = $index;
        $lastPosition = $match[1];
        $lastOperator = array_keys($operators, $match[0])[0];
    }
}

var_dump($lastIndex, $lastPosition, $lastOperator);

Last time I misunderstood your question. Now it does exactly what you want:

    <?php
        $operators= array(
            '=',
            '<',
            '>',
            '!=',
            '<>',
            '>=',
            '<=',
        );

        $statement = array(
            array("name",   0),
            array("=",      5),
            array("Emma",   7),
            array("AND",    14),
            array("age",    18),
            array("<=",     22),
            array("21",     25)
        );


        $result = find_last_operator($statement,$operators);
        echo $result; //output: 5

        function find_last_operator($statement, $operators) {
            for($key=count($statement)-1;$key > -1; --$key) {
               if (array_search($statement[$key][0],$operators) !== true ) return $key; 
            }
         }
    ?>

Output: 5

Test it online here

I would simply do this:

foreach (array_reverse($statement, true) as $key => $segment)
  if (in_array($segment[0], $operators))
    return print($key);

EDIT: this way it's faster, it won't loop thru all of it. array_reverse(..., true) will preserve the keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM