1

我对 PHP 数组和一些我想做的比较有一点麻烦。对不起,我的英语不好 :(

我正在构建一个图片管理系统。用户可以一次选择多张图片,并编辑所有选中图片的信息。但是有些字段已经填写了 - 如果特定字段填充了多张图片的不同信息,我想向用户显示一个 '*' 字符。

我的问题如下: - 我有一个二维数组,其中包含所有选定图片的 mySQL 查询结果(这个数字当然是不确定的)。这是结果数组的示例:

Array
(

[0] => Array
    (
        [Folder_id] => 40
        [Title_fr] => test
        [Title_en] => 
        [Desc_fr] => 
        [Desc_en] => 
        [Place_fr] => Koksijde, Belgique
        [Place_en] => Koksijde, Belgium
        [Place_ICAO] => EBBE
        [Place_city] => 
        [Place_country] => be
    )

[1] => Array
    (
        [Folder_id] => 40
        [Title_fr] => test
        [Title_en] => 
        [Desc_fr] => 
        [Desc_en] => 
        [Place_fr] => Koksijde, Belgique
        [Place_en] => Koksijde, Belgium
        [Place_ICAO] => EBBE
        [Place_city] => 
        [Place_country] => be
    )

[2] => Array
    (
        [Folder_id] => 40
        [Title_fr] => test
        [Title_en] => 
        [Desc_fr] => 
        [Desc_en] => 
        [Place_fr] => Koksijde, Belgique
        [Place_en] => Koksijde, Belgium
        [Place_ICAO] => EBBE
        [Place_city] => 
        [Place_country] => be
    )

[3] => Array
    (
        [Folder_id] => 51
        [Title_fr] => test
        [Title_en] => 
        [Desc_fr] => 
        [Desc_en] => 
        [Place_fr] => FRANC
        [Place_en] => ANGLE
        [Place_ICAO] => ENGL
        [Place_city] => Münich2
        [Place_country] => de
    )

如您所见,有些字段可能对所有记录都是通用的,有些字段可能不同。例如,[Place_fr] 并非所有记录都相同,但 [Desc_fr] 是。

我想编写一个函数,该函数返回一个以所有字段为键的数组,如果该字段在记录之间包含不同的值,则以“*”为值,如果该字段对所有记录都是通用的,则为该值。

例如,这里的结果是:

 Array
 ( 
        [Folder_id] => *
        [Title_fr] => test
        [Title_en] => 
        [Desc_fr] => 
        [Desc_en] => 
        [Place_fr] => *
        [Place_en] => *
        [Place_ICAO] => *
        [Place_city] => *
        [Place_country] => *
 )

我知道这可以使用 array_diff_assoc() 来完成。使用两个数组很容易完成。问题是我必须将不确定数量的参数传递给这个函数,我不知道如何解决这个问题。

抱歉这个菜鸟问题 - 我只是数组的初学者。

谢谢您的帮助 !

4

2 回答 2

1

这应该这样做:

// get common keys => values
$common  = call_user_func_array('array_intersect_assoc', $arr);

// and append missing keys to them, with "*" values
$common += array_fill_keys(array_keys($arr[0]), '*');

(假设这$arr是你上面的数组)

于 2013-02-08T16:31:23.340 回答
0
$arrays = array (
    array (
        "Folder_id" => 40,
        "Title_fr" => "test",
        "Title_en" => "",
        "Desc_fr" => "",
        "Desc_en" => "",
        "Place_fr" => "Koksijde, Belgique",
        "Place_en" => "Koksijde, Belgium",
        "Place_ICAO" => "EBBE",
        "Place_city" => "",
        "Place_country" => "be"
    ),
    array (
        "Folder_id" => 40,
        "Title_fr" => "test",
        "Title_en" => "",
        "Desc_fr" => "",
        "Desc_en" => "",
        "Place_fr" => "Koksijde, Belgique",
        "Place_en" => "Koksijde, Belgium",
        "Place_ICAO" => "EBBE",
        "Place_city" => "",
        "Place_country" => "be"
    ),
    array (
        "Folder_id" => 40,
        "Title_fr" => "test",
        "Title_en" => "",
        "Desc_fr" => "",
        "Desc_en" => "",
        "Place_fr" => "Koksijde, Belgique",
        "Place_en" => "Koksijde, Belgium",
        "Place_ICAO" => "EBBE",
        "Place_city" => "",
        "Place_country" => "be"
    ),
    array (
        "Folder_id" => 51,
        "Title_fr" => "test",
        "Title_en" => "",
        "Desc_fr" => "",
        "Desc_en" => "",
        "Place_fr" => "FRANC",
        "Place_en" => "ANGLE",
        "Place_ICAO" => "ENGL",
        "Place_city" => "Münich2",
        "Place_country" => "de"
    )
);

function compare($arrays) {
    foreach ($arrays as $array) {
        foreach ($array as $key => $value) {
            if (!isset($result[$key])) {
                $result[$key] = $value;
            } else if ($result[$key] !== "*") {
                if ($value !== $result[$key]) {
                    $result[$key] = "*";
                }
            }
        }
    }
    return $result;
}

echo "<pre>";
print_r(compare($arrays));
echo "</pre>";
于 2013-02-08T16:18:12.663 回答