0

I have the following two arrays:

array1

[0] => Array
    (
        [date] => 2014-02-01
        [course_id] => 1
        [id] => 24     [reg] => 1

    )

[1] => Array
    (
        [date] => 2014-02-01
        [course_id] => 2
        [id] => 25
   [reg] => 2

    )

array2

[0] => Array
    (
        [date] => 2014-02-01
        [course_id] => 1
        [id] => 24
    )

[1] => Array
    (
        [date] => 2014-02-01
        [course_id] => 1
        [id] => 24

    )

[2] => Array
    (
        [date] => 2014-02-01
        [course_id] => 2
        [id] => 25

    )

[3] => Array
    (
        [date] => 2014-02-01
        [course_id] => 2
        [id] => 25
    )

I have merged these two arrays using array_merge and the new array is array3

I have sorted array3 using the following function:

function cmp($a, $b) {
    return strnatcasecmp( $a[course_id], $b[course_id] );
}
usort($array3, "cmp");

However the results I am looking to get do not show as I want below, where the course_id=1 from array 1 is always at the top, followed by a list of matching Course_ids from array two, then course_id = 2 from array 1, followed by a list of matching Course_ids from array 2. :

[0] => Array
    (
        [date] => 2014-02-01
        [course_id] => 1
        [id] => 24
   [reg] => 1
    )

[1] => Array
    (
        [date] => 2014-02-01
        [course_id] => 1
        [id] => 24

    )

[2] => Array
    (
        [date] => 2014-02-01
        [course_id] => 1
        [id] => 24

    )

[3] => Array
    (
        [date] => 2014-02-01
        [course_id] => 2
        [id] => 25
   [reg] => 2
    )
[4] => Array
    (
        [date] => 2014-02-01
        [course_id] => 2
        [id] => 25

    )

[5] => Array
    (
        [date] => 2014-02-01
        [course_id] => 2
        [id] => 25

    )

The function that I have used to sort array 3 doesnt all the time provide the desired results. Please could you help with this query.

1
  • Quote from manual (php.net/manual/en/function.usort.php): The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. Commented Mar 19, 2014 at 13:55

2 Answers 2

0

Append the ids in the first array by a and those in the second array by b and while sorting the merged array when you find the same id, use the differentiating character in the end to determine the originating array. While sorting the merged array, sort it on intval(id).

PS: This is just one way to differentiate between the two arrays. You could also add another element in the array to identify the originating array.

Another possible way is to first sort the arrays individually and then merge them. This is going to be faster but would require more coding than the first approach.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for the suggestion, but Im still clueless as I cant get the desired output. I have changed strnatcasecmp to intval and this didnt make a difference, I wasnt sure how to do this part you suggested, "when you find the same id, use the differentiating character in the end to determine the originating array. ". Would you be able to provide an example please?. I have been researching all different possibilities but somethings are just beyond my grasp.
@user3437807 So when you get two elements with the same intval(id), then get the last character of id and compare that, and based on that place the elements in the merged array. Comment back if you need assistance with the code.
0

I managed to find an answer by following a very informative guide from the following link:

http://agichevski.com/2013/06/02/sorting-a-php-multidimensional-array-by-value/

foreach ($data as $val)
 $tmp_age[] = $val['age'];

foreach ($data as $val)
 $tmp_name[] = $val['name'];

array_multisort($tmp_age, SORT_DESC, $tmp_name, $data);

I adjusted it to my own data and it produced the outcome I wanted.

Hope this helps others with similar scenarios.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.