1

I am trying to create an application which find near by restaurants, I have used a database to retrieve near by zipcodes

so I now have an array like

$sortLike=array(60007,60001,60003,60002);

and database return array like

$array1= array( array ('ID' => 138,'zip_code' => 60007,'approved' => 1),
                array('ID' => 103,'zip_code' => 60007,'approved' => 0),
                array('ID' => 124,'zip_code' => 60002,'approved' => 0),
                array('ID' => 104,'zip_code' => 60002,'approved' => 1),
                array('ID' => 105,'zip_code' => 60003,'approved' => 0),
                array('ID' => 106,'zip_code' => 60001,'approved' => 0),
                array('ID' => 114,'zip_code' => 60007,'approved' => 0)
);

So I need to show all restaurants that are approved but in order of $sortLike array i.e returned array should be

   $array1= array(  
                    array ('ID' => 138,'zip_code' => 60007,'approved' => 1),
                    array('ID' => 103,'zip_code' => 60007,'approved' => 0),
                    array('ID' => 114,'zip_code' => 60007,'approved' => 0)
                    array('ID' => 106,'zip_code' => 60001,'approved' => 0),
                    array('ID' => 105,'zip_code' => 60003,'approved' => 0),
                    array('ID' => 104,'zip_code' => 60002,'approved' => 1),
                    array('ID' => 124,'zip_code' => 60002,'approved' => 0),
    );

Here is what I tried

function sortarray1($mylist,$mainZipSearch){
    $newList=$newList2=$newList3 = array();
    $j=$k=$l=0;
    if(count($mylist))
    {
        foreach($mylist as $k=>$v) {
            if( $v['approved']==1 && $v['zip_code']==$mainZipSearch){
                    $newList[$j]    =   $mylist[$k];
            } 
            else if( $v['approved']==0 && $v['zip_code']==$mainZipSearch){
                    $newList2[$k]   =   $mylist[$k];    
            }
            else{
                    $newList3[$l]   =   $mylist[$k]; 
            }
            $j++;$k++;$l++;
        }
        $newList=array_values($newList);
        $newList2=array_values($newList2);
        $newList3=array_values($newList3);
        $arr=array_merge($newList,$newList2,$newList3 );
    }
    return

}

3 Answers 3

2

You're going way too far !

Using usort makes such problems easier to handle.

function sort_results ($a, $b) {
   $sortLike=array(60007,60001,60003,60002);
   // Push unknown values at the end of the array
   if (!in_array ($a['zip_code'], $sortLike)) { return 1; } 
   return array_search($a['zip_code'], $sortLike) > array_search($b['zip_code'], $sortLike);
}

usort ($arr, 'sort_results');

print_r ($arr);

Array ( 
    [0] => Array ( [ID] => 114 [zip_code] => 60007 [approved] => 0 ) 
    [1] => Array ( [ID] => 103 [zip_code] => 60007 [approved] => 0 ) 
    [2] => Array ( [ID] => 138 [zip_code] => 60007 [approved] => 1 ) 
    [3] => Array ( [ID] => 106 [zip_code] => 60001 [approved] => 0 ) 
    [4] => Array ( [ID] => 105 [zip_code] => 60003 [approved] => 0 ) 
    [5] => Array ( [ID] => 124 [zip_code] => 60002 [approved] => 0 ) 
    [6] => Array ( [ID] => 104 [zip_code] => 60002 [approved] => 1 ) 
    [7] => Array ( [ID] => 184 [zip_code] => 60022 [approved] => 0 )
) 
Sign up to request clarification or add additional context in comments.

6 Comments

if the database array have an element which is not not in the sortLike array it places right after 60007 please try this input $array1= array(array ('ID' => 138,'zip_code' => 60007,'approved' => 1),array('ID' => 103,'zip_code' => 60007,'approved' => 0), array('ID' => 124,'zip_code' => 60002,'approved' => 0), array('ID' => 104,'zip_code' => 60002,'approved' => 1), array('ID' => 105,'zip_code' => 60003,'approved' => 0), array('ID' => 106,'zip_code' => 60001,'approved' => 0), array('ID' => 114,'zip_code' => 60007,'approved' => 0),array('ID' => 184,'zip_code' => 60022,'approved' => 0) );
@June I don't really get your point, and what you would like to do in that case ? You can easily change the sort_results function to check if in_array ($sortLike, $a) and handle this case to place it in the end for example.
function should place such a thing in the end so that if it doesnt exist it should go in the end
@June Edited my answer, it's working as you want now.
this works but if you could somehow check if 60022(not in sortlike array) is 2 times once with approved 0 and than with approved 1 , in this scenario 60022 with approved should come first
|
0

This is why we have relational databases. If you have a query which returns the sorted list of Zipcodes, then simply join the query which returns the approved restaurants.

3 Comments

lol :D "order by whatever_it_needs_ordered_by_probably_distance;"
Yeah but the data is further in different tables so I had to get the data in 1 query and sort on compile time
I am struggling to come up with a civil response to this comment. That you are using the term tables implies you are using a relational database (although even most non-relational databases support some sort of JOIN capability). No, you do not to run one query for each table. This is very rudimentary database programming.
0

You could try something like this:

<?
$sortLike=array(60007,60001,60003,60002);
$array1= array( array ('ID' => 138,'zip_code' => 60007,'approved' => 1),
                array('ID' => 103,'zip_code' => 60007,'approved' => 0),
                array('ID' => 124,'zip_code' => 60002,'approved' => 0),
                array('ID' => 104,'zip_code' => 60002,'approved' => 1),
                array('ID' => 105,'zip_code' => 60003,'approved' => 0),
                array('ID' => 106,'zip_code' => 60001,'approved' => 0),
                array('ID' => 114,'zip_code' => 60007,'approved' => 0)
);

function removeNonApproved($var) {
    return $var['approved'];
}

function cmp($a,$b) {
    global $sortLike;
    return (array_search($a['zip_code'],$sortLike) < array_search($b['zip_code'],$sortLike) ) ? -1 : 1;
}

// Remove non approved items
$array1 = array_filter($array1,"removeNonApproved");

// Sort your array
usort($array1, "cmp");

var_dump($array1);

?>

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.