error while appending data into an array using array_push in php

时间:2016-03-02 10:57:42

标签: php arrays codeigniter

I want to filter my data_array and create a new array $arr with new data.I tried using following function.it is not appending data. but it is replacing the data.I know that I have to use array_push(). when I did that data is appending to the array as single elements.

 foreach ($data_array as $value) {

                try {
                    $rep = $value->rep;
                    $idposition_rep = $value->idposition_rep;
                    $max_idinvoice = $value->max_idinvoice;
                    $idsession = $value->idsession;
                    $i_date = $value->i_date;
                    $last_i_time_string = $value->last_i_time;
                    $delayTime = $value->delayTime;
                    if ($delayTime > 30) {
                        $arr[] = array(
                            'rep' => $rep,
                            'idposition_rep' => $idposition_rep,
                            'max_idinvoice' => $max_idinvoice,
                            'idsession' => $idsession,
                            'i_date' => $i_date,
                            'last_i_time_string' => $last_i_time_string,
                            'curr_time' => date("H:i:s"),
                            'delayTime' => $delayTime);
                    }
                } catch (PDOException $ex) {
                    echo $ex->getMessage();
                    die;
                }
            }

1 个答案:

答案 0 :(得分:0)

我根据您的要求创建了示例代码。根据您的要求替换变量和赋值。 $ data_array是示例输入数组。

$ data_array = array(' 1' =>' abc',' 2' =>' das',&# 39; 3' =>' def');

$ arr = array(); $ I = 0;

foreach ($data_array as $value)  {

try {
    $rep = $value;
    $aval = strpos($rep,'a');  // Put Your condition for display Time > 30
    if ($aval !== false) {
    $arr[$i] = array(
        'rep' => $rep // assign values get from input array.
      );
    $i++;
    }
} catch (PDOException $ex) {
    echo $ex->getMessage();
    die;
} }

的print_r($ ARR);

<强>输出:

阵 (     [0] =&gt;排列         (             [rep] =&gt; ABC         )     [1] =&gt;排列         (             [rep] =&gt; DAS         ) )

希望这会对你有所帮助。