Get offset neighbors of an array of Objects

231
August 05, 2017, at 08:55 AM

In this thread it was raised a question about how to get N-neighbors of an array having user-defined starting offset and limit for the search. E.g:

Considering a Collection of 5 stdClass Objects to simplify (see below):

  • If defined a limit of 2 and a starting offset of 3, the routine should return the Object at offset 3, the two other Objects before it and only one after it, otherwise it would go out of range.

  • If defined a limit of 3 and a starting offset of 0, the routine should return the first Object and the three other Objects after it. Nothing else before the first because, obviously, otherwise it would go out of range as well.

OK so far? Good!

Both solutions presented by @mickmackusa and @Andreas solved the issue but I ended up voting for @mickmackusa's simply because it's faster and because I'm not that versed in the sorcery @Andreas did there (sorry buddy :p)

Well, the question was hard to explain to me so I simplified it with simple arrays. And even though it was still kind of hard to understand, that's why I decided to split this in a different thread, so:

How can I retrieve the same kind of information having an array filled with mixed content, specifically Objects

I tried to apply both most updated solutions with this (fake) data and neither of them worked:

$std1 = new\stdClass;
$std1 -> name = 'Name #1';
$std2 = new\stdClass;
$std2 -> name = 'Name #2';
$std3 = new\stdClass;
$std3 -> name = 'Name #3';
$std4 = new\stdClass;
$std4 -> name = 'Name #4';
$std5 = new\stdClass;
$std5 -> name = 'Name #5';
$collection = [ $std1, $std2, $std3, $std4, $std5 ];
$offset     = rand( 0, 5 );
$limit      = rand( 1, 5 );
printf( 'Value at offset #%d: %s<br />', $offset, $collection[ $offset ] -> name );
printf( 'Random limit: %d<br />', $limit );
var_dump( $collection );
var_dump( getNeighborsMickVersion( $collection, $offset, $limit ) );
var_dump( getNeighborsAndreasVersion( $collection, $offset, $limit ) );
function getNeighborsMickVersion( array $collection, $offset = 0, $limit = 1 ) {
    return array_intersect_key(
        $collection,
        array_flip(
            range( ( $offset - $limit ), ( $offset + $limit ) )
        )
    );
}
function getNeighborsAndreasVersion( array $collection, $offset = 0, $limit = 1 ) {
    Preg_match("/.{0," . $limit ."}" . chr(65 +$offset) . ".{0," . $limit ."}/", implode("", $collection), $match);
    return str_split($match[0] );
}

Guess that woth to mention that in production these Object won't be simple stdClass, may not have a specific interface implemented nor anything like that.

READ ALSO
Session not creating causing no login

Session not creating causing no login

I am creating a login script and am having trouble getting the user to login and direct to the correct pageCurrently the user submits the login form which directs to the 'secure' page which checks the session matches as seen below however it seems to keep kicking...

165
How to redirect a request, if PHP Module is turned-OFF in Appache

How to redirect a request, if PHP Module is turned-OFF in Appache

My web pages are written in PHPIf accidentally the PHP Module is turned OFF, then the requested page will simply display the source code

170
How to make sure my PHP app wont be distibuted?

How to make sure my PHP app wont be distibuted?

I have made an appointment management php app for a department of an institutionThey insist to keep the app in their server, because the database is holding sensitive informations, and they don`t want to hand it for a 3rd party

145