0

I have the following two arrays...

1) how could i get only the different key->value one? 2) how can i insert to mysql the second array?

// 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
                            )

                    )

// second array

 $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
                                )

                        )

I have used the array_diff function but i get NULL.

please some one help?

2 Answers2

1
$aa=(array)$aa;
$bb=(array)$bb;
$result=array_diff($aa,$bb);
Pranay Bhardwaj
  • 1,059
  • 8
  • 9
  • need to convert $aa and $bb into single array respectively – Pranay Bhardwaj Aug 09 '13 at 12:57
  • how do i convert to single array? – Mr Internet Aug 09 '13 at 12:58
  • function array_flatten($array) { if (!is_array($array)) { return FALSE; } $result = array(); foreach ($array as $key => $value) { if (is_array($value)) { $result = array_merge($result, array_flatten($value)); } else { $result[$key] = $value; } } return $result; } $aa=array_flatten($aa); $bb=array_flatten($bb); – Pranay Bhardwaj Aug 09 '13 at 13:01
  • check this for above code http://stackoverflow.com/questions/6785355/convert-multidimensional-array-into-single-array – Pranay Bhardwaj Aug 09 '13 at 13:02
1

It's unclear what you want. Please give an example or your desired output. Here's one possibility:

$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);

Output:

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] => 
                )

        )

)
Expedito
  • 7,771
  • 5
  • 30
  • 43