php - Get Records From Array Based On Value -
bottom line, have huge multidimensional array returned ldap_get_entries trying parse different groups based on location attribute.
in powershell like:
$location1 = get-aduser -filter * | ? {?_.l -eq "test_location"}
what doing within php (as far brain let me go) like:
foreach ($records $record) { if (isset($record['l'])) { switch ($record['l'][0]) { case "test_location1": $location1[] = $record; continue 2; case "test_location2": $location2[] = $record; continue 2; } } }
i use foreach loop (alternative syntax) against array of location variables ($location1, $location2), used above method, sort records appropriate location variable. in way, build html table grouping each locations records together. build new table each location html code in between each group, don't believe sorting ldap results location work, output 1 large table.
is there way specify clause in php? can assign records in array matching key value variable1 , records in array different matching key value variable2.
i assuming tackling in amateur scripting way.. if there easier way accomplish task (maybe skipping assign records variables part), or sort of "best practice" missing here, learning it.
thanks in advance!!
as far understood question want this:
$recordssorted = array(); foreach ($records $record) { if (! isset($record['l'])) { continue; } $recordssorted[$records['l'][0]][] = $record; } ksort($recordssorted); foreach($recordssorted $location => $records) { usort($records, function($a, $b){ return strnatcasecmp($a['uid'][0], $b['uid'][0]); }); echo '<h1>$location</h1><ul>'; foreach ($records $record) { echo '<li>' . $record['dn'] . '</li>'; } echo '</ul>'; }
this first put first entry location-attribute of entry key of array. key can used sort array.
to output content iterates on new array , sorts content - in case using first value of uid-attribute (change uid
whatever attribute need). sorted content output html.
the first array $recordssorted
might this:
array( 'london' => array( array(<entry ldap>), array(<another entry ldap>), ), 'paris' => array( array(<a further entry ldap>), ), 'berlin' => array( array(<and entry ldap>), ), );
the result this:
<h1>berlin</h1> <ul> <li>[uid of , entry ldap]</li> </ul> <h1>london</h1> <ul> <li>[uid of entry ldap]</li> <li>[uid of entry ldap]</li> </ul> <h1>paris</h1> <ul> <li>[uid of further entry ldap]</li> </ul>
does you?
Comments
Post a Comment