Asked  1 Year ago    Answers:  5   Viewed   12 times

Issue: Draw checkboxes and assign ID from query1 to elementId and values assigned to checkbox from query2. But the issue is query 2 not always returns full 9 values as compared to array1. And I want to replace missing values in query 2 with zero's after comparing the array1 values.

My Approach:

Get result set of two queries and

query 1 resultset = $tresult;
query 1 resultset = $assignedTiles;


 $a1 = array();
        while ($row = mysql_fetch_row($tresult)){
            $a1[] = $row;
        }

    $a2 = array();
        if (mysql_num_rows($assignedTiles)==0){
            //echo " inside if row null";
    $a2 = array ('0' => array ('0'),'1' => array ('0'),'2' => array 
         ('0'),'3' => array ('0'),'4' => array ('0'),'5' => array ('0'),
        '6' => array ('0'),'7' => array ('0'),'8' => array ('0'));      
        }else{
            while($row = mysql_fetch_row($assignedTiles)){
                $a2[] = $row;   
            }
        }               


    foreach($a1 as $aV){
        $aTmp1[] = $aV['0']; //setting array key 
    }
    foreach($a2 as $aV){
        $aTmp2[] = $aV['0'];
    }

    $resultArr = array_diff($aTmp1,$aTmp2); 
    // getting the difference in    both arrays

    if($resultArr !=NULL){
        foreach($resultArr as $v){
            $v = str_replace($resultArr, "0", $aTmp1);
        }
    }else { echo "did not match"; }


    $countVal = count($v);
    $i=0;
    $i<$countVal;
    $i++;

    foreach ($v as $tileId => $value){

echo "<td align=center >
<input type='checkbox' id='checkBox$tileId ' value='$value' >
 </td>";
    }

 I'm pretty sure there is a better way to do this. Any ideas or suggestion 
 would be really appreciated.

 Answers

1

Try this, for example:

$a1 = [ 1 => 'one', 2 => 'two', 3 => 'three',5=>'fayve', 6=>'six'];
$a2 = [ 2 => 'two', 5=>'five'];

print_r( array_diff($a1, $a2) );
$keys = array_keys($a1);
foreach ($keys as $k)
{
    if (!isset($a2[$k])) $a2[$k] = '0';
}
print_r($a2);

Output:

Array
(
    [1] => one
    [3] => three
    [5] => fayve
    [6] => six
)
Array
(
    [2] => two
    [5] => five
    [1] => 0
    [3] => 0
    [6] => 0
)
Saturday, May 29, 2021
 
daiscog
 
3

I would just code it like:

$c = $a;
foreach ($b as $removeKey) {
    unset($c[$removeKey]);
}
Saturday, May 29, 2021
 
Dail
 
2

Instead of a loop, why don't you use the numeric day and calculate:

$day = 'Thursday';
$day_names = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$day_num = array_search($day, $day_names);
$o_date = new DateTime("2012-09-12 20:56:43 +18 hours");
$o_day_num = $o_date->format('w');
$day_diff = ($day_num - $o_day_num) % 7;
if ($day_diff == 0) { $day_diff = 7; }
$order_day = clone $o_date;
$order_day->add(new DateInterval("P".$day_diff."D"));
Saturday, May 29, 2021
 
5

Use array_diff_key, that is what it is for. As you said, it returns an empty array; that is what it is supposed to do.

Given array_diff_key($array1, $array2), it will return an empty array if all of array1's keys exist in array2. To make sure that the arrays are equal, you then need to make sure all of array2's keys exist in array1. If either call returns a non-empty array, you know your array keys aren't equal:

function keys_are_equal($array1, $array2) {
  return !array_diff_key($array1, $array2) && !array_diff_key($array2, $array1);
}
Wednesday, August 18, 2021
 
lena
 
5

You can use iconv's transliterate feature:

iconv('utf8', 'ASCII//TRANSLIT', 'Æther') == 'Aether';

Some Windows systems may required the use of 'utf-8' instead of 'utf8'.

Sunday, December 12, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :