dongyupen6269
2014-06-18 04:04
浏览 124
已采纳

在Foreach循环中动态创建数组

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?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在foreach循环中创建一个数组,然后用一个键对它进行排序。 \ n

创建数组的每个循环如下所示:

  public function index(){
 
 $ query = $ this-&gt; My_model  - &gt; get_data(); 
 foreach($ query as $ row)
 {
 $ data = array(
 Array('Points'=&gt; $ points,'Name'=&gt; $ row [' 名称'],'电话'=&gt; $ row ['电话']),
); 
 
函数cmp($ a,$ b){
返回$ a ['Points']&lt;  $ b ['积分']?  1:-1; 
} 
 
 usort($ data,“cmp”); 
 
 print_r($ data); 
 
 
} 
} 
   
 
 

但这只返回数组中第一项的第一项。

但是当我直接输入一些数组项时,如下所示,它工作正常, 对所有数组项进行排序。

  public function index(){
 
 $ query = $ this-&gt; My_model-&gt; get_data(); 
 foreach(  $ query为$ row)
 {
 $ data = array(
 Array('Points'=&gt; 500,'Name'=&gt;'James Lion'),
 Array('Points'=&gt;  1200,'Name'=&gt;'John Smith'),
数组('Points'=&gt; 700,'Name'=&gt;'Jason Smithsonian')); 
 
函数cmp($ a,$  b){
返回$ a ['Points']&lt;  $ b ['积分']?  1:-1; 
} 
 
 usort($ data,“cmp”); 
 
 print_r($ data); 
 
} 
} 
    
 
 

如何修复此问题以便第一个代码段中的代码与第二个代码段中的代码一样?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongya3627 2014-06-18 04:12
    已采纳

    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

    已采纳该答案
    打赏 评论
  • dqd82461 2014-06-18 04:12

    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);
    
    打赏 评论
  • douken1726 2014-06-18 04:13

    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);
    }
    
    打赏 评论

相关推荐 更多相似问题