Comparer 2 tableaux PHP aider
- damien41630
- Born


- Inscription: Mai 29, 2009
- Messages: 4
- Status: Offline
J'ai l'deux matrices suivantes, contracta $ et $ contractb. J'ai besoin d'un moyen de comparer les deux tableaux et return $ contractc. Fondamentalement, si un numéro de téléphone de cellules existent dans $ contractb j'ai besoin de prendre cette inscription et de remplacer l'entrée en $ contracta donnant une sortie comme $ contractc. J'ai essayé les fonctions intégrées, mais ils comparent chaque valeur me donnant ainsi les doubles entrées pour un seul numéro.
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


- Inscription: 25 Feb 2008
- Messages: ?
- Loc: Ozzuland
- Status: Online
Juin 3rd, 2009, 12:55 pm
- joebert
- Sledgehammer


- Inscription: Fév 10, 2004
- Messages: 13458
- Loc: Florida
- Status: Offline
Je voudrais une boucle sur un tableau et remplacer les indices numériques avec des indices qui utilisent le numéro de téléphone.
Ensuite, je boucle sur contractb $ $ contracta et mettre à jour à chaque itération.
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]);
- }
Ensuite, je boucle sur contractb $ $ contracta et mettre à jour à chaque itération.
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


- Inscription: Mai 29, 2009
- Messages: 4
- Status: Offline
Cela semble fonctionne, je vais faire quelques tests supplémentaires aujourd'hui. Mon autre question que devient cette imprimées dans une table une fois que j'ai mes valeurs final. J'étais en utilisant cette fonction auparavant, mais il ne fonctionne plus.
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 sur 1
Pour répondre à ce sujet, vous devez vous connecter ou vous enregistrer. Il est gratuit.
Afficher de l'information
- Total des messages de ce sujet: 3 messages
- Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 158 invités
- Vous ne pouvez pas poster de nouveaux sujets
- Vous ne pouvez pas répondre aux sujets
- Vous ne pouvez pas éditer vos messages
- Vous ne pouvez pas supprimer vos messages
- Vous ne pouvez pas joindre des fichiers
