PHP Comparing 2 Arrays help
- damien41630
- Born


- Joined: May 29, 2009
- Posts: 4
- Status: Offline
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) )
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) )
- $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) )
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) )
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) )
- $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) )
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) )
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) )
- $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) )
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
June 3rd, 2009, 12:55 pm
- joebert
- Sledgehammer


- Joined: Feb 10, 2004
- Posts: 13458
- Loc: Florida
- Status: Offline
I would loop through array A and replace the numeric indexes with indexes which use the phone number.
Then I would loop through $contractb and update $contracta with each iteration.
Code: [ Select ]
foreach($contracta as $key => $val)
{
$contracta["_{$val['Cell Number']}"] = $val;
unset($contracta[$key]);
}
{
$contracta["_{$val['Cell Number']}"] = $val;
unset($contracta[$key]);
}
- foreach($contracta as $key => $val)
- {
- $contracta["_{$val['Cell Number']}"] = $val;
- unset($contracta[$key]);
- }
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;
}
}
}
{
$_key = "_{$val['Cell Number']}";
if(empty($contracta[$_key]))
{
$contracta[$_key] = $val;
}
else
{
foreach($val as $__key => $__val)
{
$contracta[$_key][$__key] = $__val;
}
}
}
- 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;
- }
- }
- }
Strong with this one, the sudo is.
- damien41630
- Born


- Joined: May 29, 2009
- Posts: 4
- Status: Offline
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;
}
{
// 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;
}
- 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;
- }
Moderator Remark: Added [code] tags
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 3 posts
- Users browsing this forum: No registered users and 175 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
