Jump to content

If specific sessions exist, don't run script


jds43
 Share

Recommended Posts

Hello, I need to run a script when specific sessions aren't active, but my code doesn't seem to be working. Are there any glaring errors

// Generate associative array of repeater items by title
$usgs_repeater = $usgs_data->explode('title');

// Get all session variables in an associative array
$sessions = $session->getAll();

// Check if titles of USGS Repeater items are active sessions to execute WireHttp for location data
if(!in_array($usgs_repeater, $sessions)) {
	// Do the thing here
}

Or should I check the array like this:

if(in_array($usgs_repeater, $sessions)) {
	return;
} else {
	// Do the thing here
}

 

Link to comment
Share on other sites

Something else that might stop a match is if there can be some kind type or formatting match in the titles.  So if your session say, has the key 'foo' but the title you are pulling in that explode gives you a 'Foo' then you won't match.

Hang on, just spotted that you have two arrays here - the result of the explode and the array of session items.  In that case, I think you doing an array intersection of some kind if you want it so any of the values in the set of exploded titles occurs in the session.

Link to comment
Share on other sites

Thanks @netcarver I tried a variety of approaches from array_intersect to array_diff. I think the following has worked. Not really sure how to confirm aside from clearing session data, but I'm not seeing any errors in Tracy.

if(array_keys($usgs_repeater) === array_keys($sessions))

 

Link to comment
Share on other sites

Comparing the array_keys wasn't working, so I changed to the following. Seems to be working.

// Generate associative array of repeater items by title
$usgs_repeater = $usgs_data->explode('title');
// Flip keys and values
$usgs_repeater = array_flip($usgs_repeater);
// Returns an associative array containing all the entries which have keys that are present in all arguments
$usgs_count = array_intersect_key($usgs_repeater, $sessions);
// Count the entries
$usgs_count = count($usgs_count);

if($usgs_count !== count($usgs_repeater)) {
	// Do the thing
}

 

Link to comment
Share on other sites

Hmm, are you sure it's working as you need it to? I would have thought you'd do an intersection and then just check if the intersection is empty or not - which makes me think I've not understood what you are trying to do here.

I'm imagining something like this: you have some set of titles like [0 => "Alpha", 1 => "Beta", 2 => "Gamma"] from the explode and you get the following session data ["Delta" => 'whatever']

To see if any of the values from the titles split were in the session keys I'd do something like this...

/**
 * $titles is the array of exploded titles. NB The title strings must appear as values in the array
 * $session_vars is the array of session variable names => session variable values
 **/

$intersection = array_intersect($titles, array_keys($session_vars));

if (empty($intersection)) {

    // Non of the $title values appeared as keys in the $session_vars.

} else {

    // At least one of the $title values appeared as keys in the $session_vars.

}

Selection_175.png.cd4452db0f43be0a2c0cd69c0221f002.png

If you're happy with the result though, all's good.

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...