PHP Comparing 2 Arrays help

  • damien41630
  • Born
  • Born
  • damien41630
  • Posts: 4

Post 3+ Months Ago

I have the following two arrays, $contracta and $contractb. I need a way to compare the two arrays and return $contractc. Basically if a cell phone number exist in $contractb I need to take that entry and replace the entry in $contracta giving an output like $contractc. I have tried the built in functions but they compare each value thus giving me double entries for one number.


Code: [ Select ]
$contracta
Array ( [0] => Array ( [Cell Number] => 6063395293 [Contract Date] =>N/A [Renew] =>add )
    [1] => Array ( [Cell Number] => 6067910479 [Contract Date] => N/A [Renew] =>add)
    [2] => Array ( [Cell Number] => 6067911645 [Contract Date] => N/A [Renew] =>add )
    [3] => Array ( [Cell Number] => 6067911982 [Contract Date] => N/A [Renew] =>add )
    [4] => Array ( [Cell Number] => 6067913446 [Contract Date] => N/A [Renew] =>add)
    [5] => Array ( [Cell Number] => 6067916343 [Contract Date] => N/A [Renew] =>add )
    [6] => Array ( [Cell Number] => 6067917882 [Contract Date] => N/A [Renew] =>add )
    [7] => Array ( [Cell Number] => 6067919042 [Contract Date] => N/A[Renew] =>add) )
  1. $contracta
  2. Array ( [0] => Array ( [Cell Number] => 6063395293 [Contract Date] =>N/A [Renew] =>add )
  3.     [1] => Array ( [Cell Number] => 6067910479 [Contract Date] => N/A [Renew] =>add)
  4.     [2] => Array ( [Cell Number] => 6067911645 [Contract Date] => N/A [Renew] =>add )
  5.     [3] => Array ( [Cell Number] => 6067911982 [Contract Date] => N/A [Renew] =>add )
  6.     [4] => Array ( [Cell Number] => 6067913446 [Contract Date] => N/A [Renew] =>add)
  7.     [5] => Array ( [Cell Number] => 6067916343 [Contract Date] => N/A [Renew] =>add )
  8.     [6] => Array ( [Cell Number] => 6067917882 [Contract Date] => N/A [Renew] =>add )
  9.     [7] => Array ( [Cell Number] => 6067919042 [Contract Date] => N/A[Renew] =>add) )


Code: [ Select ]
$contractb
Array ( [0] => Array ( [Cell Number] => 6067913446 [Contract Date] =>04-23-2010[Renew] => N/A)
    [1] => Array ( [Cell Number] => 6067917882 [Contract Date] =>02-27-2010[Renew] => N/A)     [2] => Array ( [Cell Number] => 6067919042 [Contract Date] =>11-15-2009[Renew] =>add) )
  1. $contractb
  2. Array ( [0] => Array ( [Cell Number] => 6067913446 [Contract Date] =>04-23-2010[Renew] => N/A)
  3.     [1] => Array ( [Cell Number] => 6067917882 [Contract Date] =>02-27-2010[Renew] => N/A)     [2] => Array ( [Cell Number] => 6067919042 [Contract Date] =>11-15-2009[Renew] =>add) )



Code: [ Select ]
$contractc
Array ( [0] => Array ( [Cell Number] => 6063395293 [Contract Date] =>N/A [Renew] =>add )
    [1] => Array ( [Cell Number] => 6067910479 [Contract Date] => N/A [Renew] =>add)
    [2] => Array ( [Cell Number] => 6067911645 [Contract Date] => N/A [Renew] =>add )
    [3] => Array ( [Cell Number] => 6067911982 [Contract Date] => N/A [Renew] =>add )
    [0] => Array ( [Cell Number] => 6067913446 [Contract Date] =>04-23-2010[Renew]=> N/A)
            [5] => Array ( [Cell Number] => 6067916343 [Contract Date] => N/A [Renew] =>add )
    [1] => Array ( [Cell Number] => 6067917882 [Contract Date]=>02-27-2010[Renew] => N/A)     [2] => Array ( [Cell Number] => 6067919042 [Contract Date] =>11-15-2009[Renew]=>add) )
  1. $contractc
  2. Array ( [0] => Array ( [Cell Number] => 6063395293 [Contract Date] =>N/A [Renew] =>add )
  3.     [1] => Array ( [Cell Number] => 6067910479 [Contract Date] => N/A [Renew] =>add)
  4.     [2] => Array ( [Cell Number] => 6067911645 [Contract Date] => N/A [Renew] =>add )
  5.     [3] => Array ( [Cell Number] => 6067911982 [Contract Date] => N/A [Renew] =>add )
  6.     [0] => Array ( [Cell Number] => 6067913446 [Contract Date] =>04-23-2010[Renew]=> N/A)
  7.             [5] => Array ( [Cell Number] => 6067916343 [Contract Date] => N/A [Renew] =>add )
  8.     [1] => Array ( [Cell Number] => 6067917882 [Contract Date]=>02-27-2010[Renew] => N/A)     [2] => Array ( [Cell Number] => 6067919042 [Contract Date] =>11-15-2009[Renew]=>add) )
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

I would loop through array A and replace the numeric indexes with indexes which use the phone number.

Code: [ Select ]
foreach($contracta as $key => $val)
{
    $contracta["_{$val['Cell Number']}"] = $val;
    unset($contracta[$key]);
}
  1. foreach($contracta as $key => $val)
  2. {
  3.     $contracta["_{$val['Cell Number']}"] = $val;
  4.     unset($contracta[$key]);
  5. }


Then I would loop through $contractb and update $contracta with each iteration.

Code: [ Select ]
foreach($contractb as $key => $val)
{
    $_key = "_{$val['Cell Number']}";
    if(empty($contracta[$_key]))
    {
        $contracta[$_key] = $val;
    }
    else
    {
        foreach($val as $__key => $__val)
        {
            $contracta[$_key][$__key] = $__val;
        }
    }
}
  1. foreach($contractb as $key => $val)
  2. {
  3.     $_key = "_{$val['Cell Number']}";
  4.     if(empty($contracta[$_key]))
  5.     {
  6.         $contracta[$_key] = $val;
  7.     }
  8.     else
  9.     {
  10.         foreach($val as $__key => $__val)
  11.         {
  12.             $contracta[$_key][$__key] = $__val;
  13.         }
  14.     }
  15. }
  • damien41630
  • Born
  • Born
  • damien41630
  • Posts: 4

Post 3+ Months Ago

This seems to works, I will do some further testing today. My only other issue is getting this printed into a table once I have my final values. I was using this function before but it no longer works.

Code: [ Select ]
function array2table($array, $recursive = false, $null = ' ')
{

    // Sanity check
    if (empty($array) || !is_array($array)) {
        return false;
    }

    if (!isset ($array[0]) || !is_array ($array[0])) {
        $array =($array[0]);
    }

    // Start the table
    $table = "<table>\n";

    // The header
    $table .= "\t<tr>";
    // Take the keys from the first row as the headings
    foreach (array_keys($array[0]) as $heading) {
        $table .= '<th>' . $heading . '</th>';
    }
    $table .= "</tr>\n";

    // The body
    foreach ($array as $row) {
        $table .= "\t<tr>" ;
        foreach ($row as $cell) {
            $table .= '<td>';

            // Cast objects
            if (is_object($cell)) { $cell = $cell; }
                    
            if ($recursive === true && is_array($cell) && !empty($cell)) {
                // Recursive mode
                $table .= "\n" . array2table($cell, true, true) . "\n";
            } else {
                $table .= (strlen($cell) > 0) ?
                 // htmlspecialchars((string) $cell) :
                    $cell :
                    $null;
            }

            $table .= '</td>';
        }

        $table .= "</tr>\n";
    }

    $table .= '</table>';
    $cc['table'] = $table;
    return $table;
}
  1. function array2table($array, $recursive = false, $null = '&nbsp;')
  2. {
  3.     // Sanity check
  4.     if (empty($array) || !is_array($array)) {
  5.         return false;
  6.     }
  7.     if (!isset ($array[0]) || !is_array ($array[0])) {
  8.         $array =($array[0]);
  9.     }
  10.     // Start the table
  11.     $table = "<table>\n";
  12.     // The header
  13.     $table .= "\t<tr>";
  14.     // Take the keys from the first row as the headings
  15.     foreach (array_keys($array[0]) as $heading) {
  16.         $table .= '<th>' . $heading . '</th>';
  17.     }
  18.     $table .= "</tr>\n";
  19.     // The body
  20.     foreach ($array as $row) {
  21.         $table .= "\t<tr>" ;
  22.         foreach ($row as $cell) {
  23.             $table .= '<td>';
  24.             // Cast objects
  25.             if (is_object($cell)) { $cell = $cell; }
  26.                     
  27.             if ($recursive === true && is_array($cell) && !empty($cell)) {
  28.                 // Recursive mode
  29.                 $table .= "\n" . array2table($cell, true, true) . "\n";
  30.             } else {
  31.                 $table .= (strlen($cell) > 0) ?
  32.                  // htmlspecialchars((string) $cell) :
  33.                     $cell :
  34.                     $null;
  35.             }
  36.             $table .= '</td>';
  37.         }
  38.         $table .= "</tr>\n";
  39.     }
  40.     $table .= '</table>';
  41.     $cc['table'] = $table;
  42.     return $table;
  43. }
Moderator Remark: Added [code] tags

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 58 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.