Asked  7 Months ago    Answers:  5   Viewed   26 times

Consider these 2 examples...

$key = 'jim';

// example 1
if (isset($array[$key])) {
    // ...
}

// example 2    
if (array_key_exists($key, $array)) {
    // ...
}

I'm interested in knowing if either of these are better. I've always used the first, but have seen a lot of people use the second example on this site.

So, which is better? Faster? Clearer intent?

 Answers

46

isset() is faster, but it's not the same as array_key_exists().

array_key_exists() purely checks if the key exists, even if the value is NULL.

Whereas isset() will return false if the key exist and value is NULL.

Wednesday, March 31, 2021
 
BetaRide
answered 7 Months ago
63

$arr2 = $arr creates a deep copy. But the actual copying only happens when $arr2 is modified -- PHP utilizes copy-on-write.

If you want a "pointer" instead of a copy, use $arr2 =& $arr, which makes $arr2 a reference to $arr.

Saturday, May 29, 2021
 
BrunoRamalho
answered 5 Months ago
13

If your columns are already defined, what about remove the column iterator?

Try something like this:

foreach ($activeSheet->getRowIterator() as $rkey => $row) {
    $rowIndex = $row->getRowIndex ();
    $parsedData[$rowIndex]['order'] = $activeSheet->getCell('A' . $rowIndex);
    $parsedData[$rowIndex]['ts']    = $activeSheet->getCell('B' . $rowIndex);
    $parsedData[$rowIndex]['summ']  = $activeSheet->getCell('C' . $rowIndex);
    .
    .
    .
}
Saturday, May 29, 2021
 
Raef
answered 5 Months ago
53

Don't bother with figuring out the differences between timezones. That's not necessary.

Whenever the user accesses a page, update a field in their record of the Users table last-updated-time. Then do a query for all users having a last-updated-time within the last 5 minutes. Anything more than this, and they are considered "offline."

If you use your server-time, via the NOW() function in MySQL, you'll side-step calculating differences between timezones.

This is the standard way of tracking how many users are presently online (Meaning, active within the last couple of minutes).

Constantly Updated

If you would like to know they are still active even when they're not jumping from page to page, include a bit of javascript to ping your server every 60 seconds or so to let you know they are still alive. It'll work the same way as my original suggestion, but it will update your records without requiring them to be frantically browsing your site at least once every five minutes.

var stillAlive = setInterval(function () {
    /* XHR back to server
       Example uses jQuery */
    $.get("stillAlive.php");
}, 60000);
Sunday, June 27, 2021
 
FWH
answered 4 Months ago
FWH
35

You should take position of the view with View.getX() and View.getY() to get x and y of the upper left corner and also assuming You know the radius (or able to obtain width/height of the view to determine radius). After that, obtain xTouch and yTouch using MotionEvent.getX() and MotionEvent.getY() and check if:

double centerX = x + radius;
double centerY = y + radius;
double distanceX = xTouch - centerX;
double distanceY = yTouch - centerY;

boolean isInside() {
    return (distanceX * distanceX) + (distanceY * distanceY) <= radius * radius;
}

The formula is just interpretation of schools geometry for determining if dot is inside circle area or not. Refer to circle equation for Cartesian coordinates for more details.

Values explanation is:

(x + radius) and (y + radius) is the center of circle.

(xTouch - (x + radius)) is distance from touch point to center by X.

(yTouch - (y + radius)) is distance from touch point to center by Y.

Tuesday, September 7, 2021
 
Paul Stanley
answered 1 Month ago
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 :