显示两个数组的不同结果

时间:2013-08-09 12:32:25

标签: php arrays

我有以下两个阵列......

1)我怎么才能得到不同的key->值一? 2)如何向mysql插入第二个数组?

// first array
$aa =  Array
            (
                [t_a] => Array
                    (
                        [0] => Array
                            (
                                [f_c] => LAL
                                [p_r] => RN
                                [id] => 
                                [gender] => m
                            )
                    )

                [t_b] => Array
                    (
                    )

                [t_l] => Array
                    (
                        [0] => Array
                            (
                                [p_lev] => 2
                                [p_date] => 
                                [p_r] => 
                            )
                    )

                [t_r] => Array
                    (
                        [0] => Array
                            (
                                [I_r] => 19
                            )

                    )

//第二个数组

 $bb = Array
                (
                    [t_a] => Array
                        (
                            [0] => Array
                                (
                                    [f_c] => NAN
                                    [p_r] => RN
                                    [id] => 1214125
                                    [gender] => m
                                )
                        )

                    [t_b] => Array
                        (
                        )

                    [t_l] => Array
                        (
                            [0] => Array
                                (
                                    [p_lev] => 2
                                    [p_date] => 21
                                    [p_r] => 25
                                )
                        )

                    [t_r] => Array
                        (
                            [0] => Array
                                (
                                    [I_r] => 19
                                )

                        )

我使用了array_diff函数但是我得到了NULL。

请帮助一些人?

2 个答案:

答案 0 :(得分:1)

$aa=(array)$aa;
$bb=(array)$bb;
$result=array_diff($aa,$bb);

答案 1 :(得分:1)

目前还不清楚你想要什么。请举例或您想要的输出。这是一种可能性:

$ser_aa = array_map(function($e){return serialize($e);}, $aa);
$ser_bb = array_map(function($e){return serialize($e);}, $bb);
$diff = array_diff($ser_aa, $ser_bb);
$out = array_map(function($e){return unserialize($e);}, $diff);
print_r($out);

输出:

Array
(
    [t_a] => Array
        (
            [0] => Array
                (
                    [f_c] => LAL
                    [p_r] => RN
                    [id] => 
                    [gender] => m
                )

        )

    [t_l] => Array
        (
            [0] => Array
                (
                    [p_lev] => 2
                    [p_date] => 
                    [p_r] => 
                )

        )

)