jds43 Posted July 13, 2021 Share Posted July 13, 2021 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 More sharing options...
netcarver Posted July 13, 2021 Share Posted July 13, 2021 Hi @jds43 I can't remember now, but I think the items in the session come back as key => value mappings. In that case you might want to use the array_key_exists() function instead of in_array(). Link to comment Share on other sites More sharing options...
netcarver Posted July 13, 2021 Share Posted July 13, 2021 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 More sharing options...
jds43 Posted July 14, 2021 Author Share Posted July 14, 2021 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 More sharing options...
jds43 Posted July 14, 2021 Author Share Posted July 14, 2021 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 More sharing options...
netcarver Posted July 14, 2021 Share Posted July 14, 2021 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. } If you're happy with the result though, all's good. 1 Link to comment Share on other sites More sharing options...
jds43 Posted July 14, 2021 Author Share Posted July 14, 2021 Thanks for the help @netcarver ?? Link to comment Share on other sites More sharing options...
jds43 Posted July 15, 2021 Author Share Posted July 15, 2021 I prefer your approach @netcarver It appears to be working quite well. Thank you for that option. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now