从条件php的数组中获取过滤数组

时间:2016-05-28 07:24:12

标签: php arrays codeigniter

此代码使用Codeigniter框架,

我有以下数组$ csv_array

Array ( [0] => Array ( [location] => X33 [usernumber] => 1 [order] => XX [part_number] => Hi ) [1] => Array ( [location] => X33 [usernumber] => 1 [order] => XX [part_number] => 68730 ) [2] => Array ( [location] => W33 [usernumber] => 2 [order] => YY [part_number] => 68741) [3] => Array ( [location] => W33 [usernumber] => 2 [order] => YY [part_number] => Hello )

我的要求,

为每个用户数量(此处为2个不同的用户)创建一个数组,其中包含location,part_number,order,然后只发送2次电子邮件,每个用户1次,并为用户编号生成数组信息,

我尝试了下面的步骤代码,但我在循环中丢失了!

foreach ($csv_array as $row)
            {
                $user = $this->admin_model->get_usershipment($row['usernumber']);
                $order_data = array();

                for ($i=0; $i < count($csv_array); $i++)
                {
                    if($row['usernumber'] == $user->usernumber)
                    {
                        //Create some array to send????
                        $order_data = array();

                       //Should be something like below, but should contain for usernumber 1 only, and then in 2nd email it should be for usernumber 2 only
                        //$order_data = array('location' => $row['location'], 'part_number' => $row['part_number'], 'order' => $row['order']);
                    }
                }

                $data = array(
                    'user_firstname'    =>  $user->user_firstname,
                    'user_email'        =>  $user->user_email,
                   'order_data' => $order_data);

                $subject = 'Hello Update! ';
                $message = $this->parser->parse('templates/notification', $data, TRUE);

                $this->emailnotification($user->user_email, $subject, $message);
            }

这里还有一个问题是,电子邮件通知功能将运行4次,我只需要2次,意味着只有2个用户,

任何指导如何实现这一目标?

提前致谢!

1 个答案:

答案 0 :(得分:0)

如果我理解正确,可以有很多方法。一个解决方案可以是(这只是演示使用你自己的感觉来修改它): -

foreach($csv_array as $row)
{
    $csv_array1[$row['usernumber']][] = $row
}
foreach ($csv_array1 as $key=>$details)
{

    $user = $this->admin_model->get_usershipment($key);
    $data = array(
            'user_firstname'    =>  $user->user_firstname,
            'user_email'        =>  $user->user_email,
           'order_data' => $details);//$details has 2 arrays for each user 
    $subject = 'Hello Update! ';
    $message = $this->parser->parse('templates/notification', $data, TRUE);
    $this->emailnotification($user->user_email, $subject, $message);
}