繁体   English   中英

在PHP中使用array_push将数据附加到数组中时出错

[英]error while appending data into an array using array_push in php

我想过滤我的data_array并使用新数据创建一个新数组$arr 。我尝试使用以下函数。它不附加数据。 但是它正在替换数据。我知道我必须使用array_push() 当我这样做时,数据将作为单个 元素追加到数组。

 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;
                }
            }

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

$ data_array = array('1'=>'abc','2'=>'das','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] =>数组([rep] => abc)[1] =>数组([rep] => das))

希望这会帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM