English 中文(简体)
如何检查 PHP 组合数组是否相等, 忽略密钥排序?
原标题:How to check if PHP associative arrays are equal, ignoring key ordering?

假设我在PHP有两个复杂的嵌套阵列,像这样:

$a = array(
    "x" => array(4, 5, 6),
    "y" => array("z" => "foo", "q" => "bar")
    );
$b = array(
    "y" => array("q" => "bar", "z" => "foo"), 
    "x" => array(4, 5, 6)
    );

假设内容可以任意嵌套,但不会包含任何循环引用。

检查它们是否相等, 忽略按键顺序的最直截了当的方式是什么? 例如, 以上两个应该对等 。 但是, 如果 < code>$b[ "x"] array( 4, 6, 5) , 则不会是 < code > (4, 6, 5)

我可以循序 ksort 比较结果,但我并不想修改任何一种操作,这似乎可能有一个我所不知道的简单单线解决方案。是否有东西?

最佳回答

你已经提到过最好的方法 但你忘了一件事

  1. Copy the arrays
  2. Sort the Arrays
  3. Compare the 2

很好,你还有原版的

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...