Hi there, i'm currently having trouble re-sorting an array based on a letter that a user has submitted.
this is the associative array I want to sort.
$people = array(
0 => array(
'id' => 12345,
'name' => 'abi',
),
1 => array(
'id' => 12346,
'name' => 'ben',
),
2 => array(
'id' => 12347,
'name' => 'carly',
)
);
I've been trying for quite sometime now but to no avail
this is the code i have created so far.
function sort_array($array,$keyID,$letter)
{
$match = array();
$other = array();
$flag = false;
foreach ($array as $key => $value)
{
//print $key; print 0 1 2
//print $value; prints array array array
//
//loop through each value array
foreach($value as $key2 => $value2)
{
//print $value; will print array 6 times
//print $key2; will print id name 3 times
//print $value2; will print 12345abi 12346ben 12347carly ,the values of each key
if($key2 == $keyID)
{
//print $key; prints 0 1 2
//print $value2; prints abi ben carly
if($letter == $value2[0])
{
$flag = true;
}
if($flag == true)
{
$match[$key] = $value2;
}
else
{
$other[$key] = $value2;
}
$sortable[$key] = array_merge($match,$other);
}
}
}
foreach ($sortable as $key => $value)
{
$new_array[$key] = $array[$key];
}
print_r($new_array);
}
//call function
sort_array($people,'name','c');
any help would be apreciated