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.

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

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)

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
- 4 回答
- 0 關(guān)注
- 219 瀏覽
添加回答
舉報