0

i am fetching some data from multiple tables on the basis of particular id, instead of using joins i used nested queries, now the data needs to be to sorted as i am using nested queries it is difficult to use orderby now below is the array i am generating!

Array
(
    [0] => Array
        (
            [id] => 54
            [name] => Array
                (
                    [0] => Array
                        (
                            [name] => Jumbo Green Raisins Chinese
                        )

                )

            [path] => Array
                (
                    [0] => Array
                        (
                            [path] => Jumbo_Green_Raisins_Chinese2.jpg
                        )

                )

            [sort] => Array
                (
                    [0] => Array
                        (
                            [sort] => 2
                        )

                )

        )

    [1] => Array
        (
            [id] => 55
            [name] => Array
                (
                    [0] => Array
                        (
                            [name] => Regular Green Raisins Chinese
                        )

                )

            [path] => Array
                (
                    [0] => Array
                        (
                            [path] => Regular_Green_Raisins_Chinese.jpg
                        )

                )

            [sort] => Array
                (
                    [0] => Array
                        (
                            [sort] => 4
                        )

                )

        )

    [2] => Array
        (
            [id] => 56
            [name] => Array
                (
                    [0] => Array
                        (
                            [name] => Green Sunderkhani Raisins
                        )

                )

            [path] => Array
                (
                    [0] => Array
                        (
                            [path] => Green_Sunderkhani_Raisins.jpg
                        )

                )

            [sort] => Array
                (
                    [0] => Array
                        (
                            [sort] => 3
                        )

                )

        )

    [3] => Array
        (
            [id] => 57
            [name] => Array
                (
                    [0] => Array
                        (
                            [name] => Jumbo Black Raisins
                        )

                )

            [path] => Array
                (
                    [0] => Array
                        (
                            [path] => Jumbo_Black_Raisins.jpg
                        )

                )

            [sort] => Array
                (
                    [0] => Array
                        (
                            [sort] => 1
                        )

                )

        )
)

basically i am using a loop to display the information retrieved in the above data, what i want to do is inside the loop display the sorted data the index sort is what i need to see while sorting, i could do it with db too but i have to mess up code in a lot of places it would be alot better if i do i this way! any help?

uneeb meer
  • 682
  • 2
  • 7
  • 27
  • Why the switch to nested queries over joins? As a general rule joins are faster. – GordonM Dec 04 '17 at 13:10
  • actually i made this project a long long time ago 1.5 years from now! now some work came up so i cant just change the logic of everything, back then i wasn't that professional i loved to try new logics and stuff now it got me hanging.:/ – uneeb meer Dec 04 '17 at 13:12
  • can you give actual json array? – Abhinav Dec 04 '17 at 13:45

1 Answers1

1

Basic php usort

function sorted_nested($a, $b) {
    if($a['sort'][0]['sort'] == $b['sort'][0]['sort']) {
        return 0;
    }

    if($a['sort'][0]['sort'] < $b['sort'][0]['sort']) {
        return -1;
    } else {
        return 1;
    }
}

usort($array, 'sorted_nested');
Shibi
  • 1,373
  • 2
  • 9
  • 8
  • wow, there are very few times when i don't get the logic by seeing it! hats off brother keep up the awesome work! – uneeb meer Dec 04 '17 at 13:44