hi guys i have a multidimensional associative array and i want to search in the array.
public static function userAccessArray()
{
return array(
array(
'title' => 'General',
'access' => array(
array('header' => 'Configuration' ,'link' => 'configuration', 'id' => 'configuration'),
array('header' => 'Colleges' ,'link' => 'college', 'id' => 'college'),
array('header' => 'Periods' ,'link' => 'period', 'id' => 'period'),
array('header' => 'School' ,'link' => 'school', 'id' => 'school'),
array('header' => 'User Accounts' ,'link' => 'user', 'id' => 'user'),
array('header' => 'Audit Log' ,'link' => 'configuration/auditlog', 'id' => 'auditlog')
)
),
array(
'title' => 'Offerings',
'access' => array(
array('header' => 'Programs' ,'link' => 'program', 'id' => 'program'),
array('header' => 'Departments' ,'link' => 'department', 'id' => 'department'),
array('header' => 'Curriculum' ,'link' => 'curriculum', 'id' => 'curriculum'),
array('header' => 'Instructors' ,'link' => 'instructor', 'id' => 'instructor'),
array('header' => 'Rooms' ,'link' => 'room', 'id' => 'room'),
array('header' => 'Sections' ,'link' => 'section', 'id' => 'section'),
array('header' => 'Subjects' ,'link' => 'subject', 'id' => 'subject')
)
)
) )
i want to search in LINK column if it exists i want to return the TITLE and the rest of access values in the array.
public static function searchUSerAccess($array, $val, $column)
{
$arr = [];
foreach ($array as $key => $value) {
foreach ($value['access'] as $k => $v) {
$keyres = array_search($val, array_column($v, $column));
}
}
}
return Helpers::searchUSerAccess(Helpers::userAccessArray(), 'college', 'link');
CodePudding user response:
Using array_search()
isn't really suitable for your use case since according to the documentation, it
Searches the array for a given value and returns the first corresponding key if successful
array_search()
is used more for a list of values discreet values in an array but we are interested in the value for a particular key that we know about, so this would not give back any interesting information. Instead we can iterate over the array and sub-arrays and check for equalities:
public static function searchUSerAccess($array, $val, $column)
{
// Loop through the top level array.
foreach ($array as $value) {
// Each iteration gives us
// $value = array('title' => '…', 'access' => array(…))
// Go through the 'access' element arrays, for a particular column
// and value.
foreach ($value['access'] as $access) {
// Each iteration gives us
// $access = array('header' => '…', 'link' => '…', 'id' => '…')
// Check the value of the column we are interested in.
if ($access[$column] === $val) {
// If found, give back a merge of array members we
// are interested in.
return [
'title' => $value['title'],
...$access,
];
}
}
}
}
This would give the following for your example:
var_dump(Helpers::searchUSerAccess(Helpers::userAccessArray(), 'college', 'link'));
// array(4) {
// ["title"]=>
// string(7) "General"
// ["header"]=>
// string(8) "Colleges"
// ["link"]=>
// string(7) "college"
// ["id"]=>
// string(7) "college"
// }