0

I am trying create an array in a foreach loop, and then sort it by a key.

The for each loop creating the array looks like this:

public function index(){

    $query=$this->My_model->get_data();
    foreach ($query as $row)
    {
           $data=array(
           Array('Points'=>$points,'Name'=>$row['Name'], 'Phone'=>$row['phone']),
            );

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

        usort($data, "cmp");

        print_r($data);


        }
    }

But this only returns the first the first item in the array.

However when I input some array items directly such as the below, it works fine and sorts all the array items.

public function index(){

    $query=$this->My_model->get_data();
    foreach ($query as $row)
    {
         $data = array (
    Array ( 'Points' => 500, 'Name' => 'James Lion' ) ,
    Array ( 'Points' => 1200, 'Name' => 'John Smith' ), 
    Array ( 'Points' => 700, 'Name' => 'Jason Smithsonian' ) );

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

        usort($data, "cmp");

        print_r($data);

        }
    }

How do I fix this so that the code in the first snippet, so that works as it does in the second snippet?

2
  • have you tried using your custom sort after the foreach operation? (meaning after and outside the foreach) Commented Jun 18, 2014 at 4:06
  • @kevinabelita I have tried that earlier, but it just returns the last item. The second code snippet above works fine, with the array and sort code in that position, but when I try to create the array with the dynamic code int the first snippet doesn't work, so I think it's something with the way I'm construction the array in the loop in the 1st code snippet that is. Commented Jun 18, 2014 at 4:10

3 Answers 3

1

You have to change the code piece like this

$data[]=array('Points'=>$points,'Name'=>$row['Name'], 'Phone'=>$row['phone']));

The problem with your code is , you are not creating a multidimensional array and instead overwriting the $row values in $data which eventually has the last data since all the other data is overwritten

Also move your function cmp outside of the foreach loop

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

Comments

1

Have you tried using your custom sort on the outside, (after building your array on the loop). Consider this example:

public function index()
{
    $query = $this->My_model->get_data();
    foreach ($query as $row) {
        $data[] = array('Points' => $points,' Name' => $row['Name'], 'Phone' => $row['phone']),);
    }

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

    usort($data, "cmp");
    print_r($data);
}

1 Comment

Tried this, this gives an error, where the index "Points" in the cmp function is not identified/recognized, so I get undefined index errors
0

Store your values into array format lock like this $data[]

foreach ($query as $row)
    {
           $data[]=array(
           Array('Points'=>$points,'Name'=>$row['Name'], 'Phone'=>$row['phone']),
            );
   }

Then you print the data outside of foreach loop

print_r($data);

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.