0

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

2
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by benjaminFowl87
0

I'm still strugling with this, the multi sort function provided only sorts arrays by ASC, DESC, STRING etc,

I would like to sort the array starting from a specified letter such as c

this is what i currently have

$people = array(
    0 => array(
        'id' => 12345,
        'name' => 'Joe',

    ),
    1 => array(
        'id' => 12346,
        'name' => 'Adam',

    ),
    2 => array(
        'id' => 12347,
        'name' => 'Amy',
    )
);

// Obtain a list of columns
foreach ($people as $key => $row) 
{
    $name[$key]  = $row['name'];
}

array_multisort($name, SORT_DESC, $people);

print_r($people);

With my previous code i was simply adding all the values that didn't start with the letter b for example to the end of an array thus giving me a sorted by letter array.

i cant seem to achieve this with an associative array as when i merge them the array comes out in ASC order again.

0

I've solved the problem, heres the final code to sort an associative array by its first letter. Hope it helps anyone hoping to achieve something similar.

class sortArray 
{
	/* Will get the first letter of each value and compare it
	   to the user input */
	public function getFirstChar($char)
	{
		return substr($char, 0,1);
	}
	
	/* Verify the user input and convert it */
	public function verifyVariable($var)
	{
		if(isset($var) && !is_numeric($var))
		{
			$string = strtoupper($var);
		}
		else
		{
			$string = '';
		}
		
		return $string;
	}
	
	public function sort_array($array,$keyID,$letter)
	{
		$new = array();
		$sortable = array();
		$match = array();
		$other = array();
		$letter = $this->verifyVariable($letter);
		$flag = false;
		
		foreach ($array as $key => $value) 
		{
			foreach ($value as $key2 => $value2)
			{
				if($key2 == $keyID)
				{
					if($letter == $this->verifyVariable($this->getFirstChar($value2)))
					{
						$flag = true;
					}
					
					if($flag == true)
					{
						$match[$key] = $value2;
					}
					else
					{
						$other[$key] = $value2;
					}
					
					$sortable = $match + $other;
				}
			}	
		}
		
		foreach ($sortable as $key => $value) 
		{
			$new[$key] = $array[$key];
		}
		
		return $new;
	}
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.