hello all,
i have a php page in which user added certain combinations and clicks on add. when added, i create a array which stores the values and user can add multiple rows.
if ($action=="Add") {
$Arr[] = array (
"scopeid" => $scopeid, // values are comma seperated e.g 1,5,6
"basis" => $basis,
"rate" => $rate,
"currencyid" => $currencyid
);
}

i save it to DB. while editing any of the rows in edit mode, i should create a new version of the latest updates. i have tried to do it but could not do it
how to do this comparison. below is my code. hope some one will help me with it
// $arrar1 = values from DB
// $arrar2 = current page values
// or vice versa
function Array_Compare($array1, $array2) {

if (count($array1) != count($array2) ) { // no further comparison reqd
$NewVersionFlag=1; // new version reqd
return $NewVersionFlag;
} else {
$NewVersionFlag=0; // check further values
}

if ($NewVersionFlag==0) {
// comparing of values goes here
}
return $NewVersionFlag;
} // end function

Completely lost
Thanks in advance

Dani AI

Generated

described a common gotcha: each row is a small associative array and one field (scopeid) is a comma list whose order should not matter. and pointed at array_search / array_diff — those help detect differences but won't correctly handle unordered lists inside a field or rows that are the same but appear in different order. A robust approach is to canonicalize each row (normalize types, sort the scope ids, remove whitespace/dups), sort the set of rows deterministically, then compare the canonical representations with a strict equality check.

Example canonicalization + comparison:

function normalize_row(array $r) {
    $r = array_map('trim', $r);
    if (isset($r['rate'])) $r['rate'] = (float) $r['rate'];
    if (isset($r['scopeid'])) {
        $ids = array_filter(array_map('trim', explode(',', $r['scopeid'])), 'strlen');
        $ids = array_map('intval', $ids);
        sort($ids, SORT_NUMERIC);
        $ids = array_values(array_unique($ids));
        $r['scopeid'] = implode(',', $ids);
    }
    ksort($r);
    return $r;
}

function canonical_json(array $rows) {
    $norm = array_map('normalize_row', $rows);
    usort($norm, function($a,$b){ return strcmp(json_encode($a), json_encode($b)); });
    return json_encode($norm);
}

if (canonical_json($dbRows) !== canonical_json($formRows)) {
    // new version required
}

Notes and caveats: unset any DB-only fields (auto IDs, timestamps) before compare; round floats (rates) to a fixed precision to avoid tiny differences; if duplicates matter, compare counts of row-hashes instead of set equality; for very large datasets compute a hash per normalized row (sha1/json) and compare counts for better memory use. This keeps version detection deterministic and resilient to ordering/formatting noise.

Recommended Answers

All 2 Replies

U can use the array_search() function
http://www.php.net/array_search
Make sure u use the "===" for comparising.
It should look something like this :

if ($NewVersionFlag==0) 
{ 
   $i=0;
   foreach($array1 as $value)
   {
         if( array_search($value,$array2) !== 0  )
                  ++$i;
    }
    if( $i == count($array2) )
             echo 'The two arrays have the same values';
    else
             echo 'The arrays do not correspond'
}
use array_diff() function that gives you list of array not in both array's
<?php

$array1("red","Green","Blue");
$array2("Blue","white","red");
$available = array_diff($array1, $array2);
print_r($available);

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.