array_diff_uassoc()函数在php中做了什么?

时间:2017-11-05 11:26:49

标签: php arrays

我是php新手,我在 php.net w3schools 以及其他资源上阅读了有关 array_diff_uassoc()功能的文档在互联网上,但未能得到这个功能的用途。根据我的说法,它是无意义的功能,因为它的文档很混乱。

正如我所知,第一个参数和第二个参数是数组,但是第三个参数是什么需要函数,它必须返回小于,大于或等于0的这个和这个。这个无意义的文档意味着什么?

按照所有示例生成相同的结果。

示例1

function test($a,$b){
$a > $b ? 1 : -1;
}

$arrayOne = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$arrayTwo = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$x = array_diff_uassoc($arrayOne,$arrayTwo,'test');

输出

  

阵   (       [two] => elementTwo       [三] => elementThree   )

示例2

function test($a,$b){
$a > $b ? -1 : 1;
}

$arrayOne = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$arrayTwo = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$x = array_diff_uassoc($arrayOne,$arrayTwo,'test');

输出

  

阵   (       [two] => elementTwo       [三] => elementThree   )

示例3

function test($a,$b){
$a < $b ? 0 : 1;
}

$arrayOne = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$arrayTwo = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$x = array_diff_uassoc($arrayOne,$arrayTwo,'test');

输出

  

阵   (       [two] =&gt; elementTwo       [三] =&gt; elementThree   )

示例4

function test($a,$b){
$a < $b ? 0 : 1;
}

$arrayOne = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$arrayTwo = array(
    "one"=>"elementOne",
    "two"=>"elementTwo",
    "three"=>"elementThree"
);

$x = array_diff_uassoc($arrayOne,$arrayTwo,'test');

输出

  

阵   (       [two] =&gt; elementTwo       [三] =&gt; elementThree   )

那么这个无意义的功能意味着什么?谁能告诉或者我错了?

2 个答案:

答案 0 :(得分:2)

比较功能允许您创建自定义逻辑以确定两个条目是否相同。

这两个数组中的键看起来完全不同,因为它们使用不同的语言。

$data1 = [
  'red' => true,
  'yellow' => true,
  'green' => true,
  'blue' => true,
  'black' => true,
];

$data2 = [
  'rouge' => true,
  'jaune' => true,
  'bleu' => true,
  'vert' => true,
  'blanc' => true,
];

但我们仍然可以使用自定义比较函数对它们进行差异,该函数可识别两种语言具有等效值的位置

function colourLanguageTest($a, $b) {
    static $comparator = [
        'red' => 'rouge',
        'yellow' => 'jaune',
        'green' => 'vert',
        'blue' => 'bleu',
        'black' => 'noir',
        'white' => 'blanc',
    ];

    if (isset($comparator[$a])) {
        return $comparator[$a] != $b;
    } elseif(isset($comparator[$b])) {
        return $comparator[$b] != $a; 
    }

    return true;
}

$result = array_diff_uassoc($data1, $data2, 'colourLanguageTest');

var_dump($result);

比较函数检查比较器表中的条目,以便识别redrouge是否相同,并将它们视为匹配。如果匹配,则返回布尔值false(0)如果没有匹配,则返回布尔值true(1)。 因为这是一个diff函数,所以它会从我们的自定义逻辑返回0(表示匹配)的第一个数组中过滤掉所有条目,并且只留下我们的比较逻辑不返回0的条目(即返回1或-1或999)或-23456)

因为'red','yellow','green'和'blue'在第二个数组中都有相应的条目,根据语言查找匹配,只有'black'在第二个数据中没有相应的条目数组,所以我们调用array_diff_uassoc()的结果返回

array(1) {
  ["black"]=>
  bool(true)
}

答案 1 :(得分:-1)

回调“test”函数中必须有返回值

public class HttpClient {
private static final String TAG = "HttpClient";

public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) {

    try {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json");
        httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
        Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");

        // Get hold of the response entity (-> the data):
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            // Read the content stream
            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                instream = new GZIPInputStream(instream);
            }

            // convert content stream to a String
            String resultString= convertStreamToString(instream);
            instream.close();
            resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"

            // Transform the String into a JSONObject
            JSONObject jsonObjRecv = new JSONObject(resultString);
            // Raw DEBUG output of our received JSON object:
            Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>");

            return jsonObjRecv;
        }

    }
    catch (Exception e)
    {
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.
        e.printStackTrace();
    }
    return null;
}


private static String convertStreamToString(InputStream is) {
    /*
     * To convert the InputStream to String we use the BufferedReader.readLine()
     * method. We iterate until the BufferedReader return null which means
     * there's no more data to read. Each line will appended to a StringBuilder
     * and returned as String.
     *
     * (c) public domain: http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
     */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}