第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

比較兩個數(shù)組中元素的順序

比較兩個數(shù)組中元素的順序

PHP
慕蓋茨4494581 2023-11-03 15:44:49
我需要 PHP 函數(shù)來比較兩個數(shù)組中元素的順序。第一個數(shù)組是標(biāo)準(zhǔn)數(shù)組,保存正確的順序,第二個數(shù)組是要比較的數(shù)組。數(shù)組中要比較的元素可以重復(fù)。要比較的數(shù)組不能包含標(biāo)準(zhǔn)具數(shù)組中的所有元素。例子:<?php    // Standard    $standard = array(        'taxonomy',        'post_meta',        'author',        'date',        'post_meta_num'    );        // Valid order    $valid_order = array(        'taxonomy',        'taxonomy',        'post_meta',        'date'    );        // Invalid order, 'author' is before 'post_meta'    $invalid_order = array(        'taxonomy',        'author',        'author',        'post_meta'    );?>我試圖在 StackOverflow 上找到一些東西,但已有的答案與我的任務(wù)不兼容。僅當(dāng)要比較的數(shù)組包含標(biāo)準(zhǔn)中的所有元素時,此函數(shù)才能正常工作。<?php    function compare( $standard, $to_compare  ){        if( ! is_array( $standard ) || ! is_array( $to_compare ) ){            return false;        }            $i = 0;            foreach ( $to_compare as $value ) {            if( $value === $standard[$i] ){                $i++;            }        }            return ( $i == count( $standard ) );    }?>最后,如果要比較的標(biāo)準(zhǔn)和數(shù)組中的順序相等,則該函數(shù)應(yīng)返回 true;如果不相等,則該函數(shù)應(yīng)返回 false。謝謝。
查看完整描述

4 回答

?
手掌心

TA貢獻(xiàn)1942條經(jīng)驗 獲得超3個贊

假設(shè)數(shù)組中不能有布爾值,您可以執(zhí)行以下操作:


<?php

/**

 * @param string[] $standard

 * @param string[] $to_compare

 * @return bool

 */

function compare(array $standard, array $to_compare) : bool {

    $standard_item = reset($standard);


    foreach ($to_compare as $item) {

        // Skip standard items assuming they are all optional

        while ($item !== $standard_item) {

            $standard_item = next($standard);

            if ($standard_item === false) {

                return false;

            }

        }


        if ($standard_item === false || $item !== $standard_item) {

            return false;

        }

    }

    return true;

}

如果您想支持false標(biāo)準(zhǔn)數(shù)組中的值,可以修改上面的代碼,以便通過索引引用項目,例如$standard[$i]。但這種方法也有其缺點——鍵必須是數(shù)字且連續(xù)的。對于更通用的解決方案,我可能會使用迭代器,例如ArrayIterator.


查看完整回答
反對 回復(fù) 2023-11-03
?
當(dāng)年話下

TA貢獻(xiàn)1890條經(jīng)驗 獲得超9個贊

我發(fā)現(xiàn)一個可能的解決方案很容易遵循:


class Transition

{

    private string $fromValue;

    private array $toValues;


    public function __construct(string $fromValue, array $toValues)

    {

        $this->fromValue = $fromValue;

        $this->toValues = $toValues;

    }


    public function getFromValue(): string

    {

        return $this->fromValue;

    }


    public function getToValues(): array

    {

        return $this->toValues;

    }

}


function prepareTransitionsMap(array $orderDefinitions): array

{

    $transitions = [];

    $definitionsCount = count($orderDefinitions);

    foreach ($orderDefinitions as $i => $fromValue) {

        $toValues = [];

        for ($j = $i; $j < $definitionsCount; ++$j) {

            $toValues[] = $orderDefinitions[$j];

        }


        $transitions[$fromValue] = new Transition($fromValue, $toValues);

    }


    return $transitions;

}


function isArrayOrderValid(array $orderDefinitions, array $valuesToCheck): bool

{

    $valuesCount = count($valuesToCheck);

    if ($valuesCount === 0) {

        return true;

    }


    $definitionsCount = count($orderDefinitions);

    if ($definitionsCount === 0) {

        return false;

    }


    $transitionsMap = prepareTransitionsMap($orderDefinitions);

    foreach ($valuesToCheck as $i => $iValue) {

        $valueToCheck = $iValue;


        // value is no defined at all

        if (!array_key_exists($valueToCheck, $transitionsMap)) {

            return false;

        }


        // value is the last in the array

        if (!array_key_exists($i + 1, $valuesToCheck)) {

            return true;

        }


        $nextValue = $valuesToCheck[$i + 1];

        $transition = $transitionsMap[$valueToCheck];


        if (!in_array($nextValue, $transition->getToValues(), true)) {

            return false;

        }

    }


    return true;

}


isArrayOrderValid($standard, $valid_order); // true

isArrayOrderValid($standard, $invalid_order); // false


查看完整回答
反對 回復(fù) 2023-11-03
?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗 獲得超5個贊

你想使用usort() https://www.php.net/manual/fr/function.usort.php

它看起來像

function custom_sort(&$my_array) {

    return usort($my_array, function ($a, $b) {

        global $etalon;

        $a_key = array_search($a, $etalon);

        $b_key = array_search($b, $etalon);

        if (($a_key === FALSE) || ($b_key === FALSE) || ($a_key == $b_key)) {

            return 0;

        }

        ($a_key < $b_key) ? -1 : 1;

    });

}


custom_sort($valid_order);

custom_sort($invalid_order)


查看完整回答
反對 回復(fù) 2023-11-03
?
楊魅力

TA貢獻(xiàn)1811條經(jīng)驗 獲得超6個贊

另一個可以提供幫助的解決方案:


 // Etalon

    $etalon = array(

        'taxonomy',

        'post_meta',

        'author',

        'date',

        'post_meta_num'

    );

    

    // Valid order

    $valid_order = array(

        'taxonomy',

        'taxonomy',

        'post_meta',

        'date'

    );

    

    // Invalid order, 'author' is before 'post_meta'

    $invalid_order = array(

        'taxonomy',

        'author',

        'author',

        'post_meta'

    );

    


function checkOrder($array , $etalon)

{

    $array = array_values(array_unique($array));

    $array = array_intersect($array, $etalon );

    

    foreach($array as $key => $value){

        if(!in_array($array[$key],$etalon) || array_search($array[$key], $etalon)<$key){

          return false;

        }

    }

    return true;

}



var_dump(checkOrder($valid_order,$etalon)); // true


var_dump(checkOrder($invalid_order,$etalon)); // false


查看完整回答
反對 回復(fù) 2023-11-03
  • 4 回答
  • 0 關(guān)注
  • 219 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號