请教:PHP中,如何获取提交的二维数组中某几列键值不为空的结果

WBOY
Lepaskan: 2016-06-23 13:52:51
asal
721 orang telah melayarinya

所提交的结果如下:

Array(    [name] => Array        (            [0] => 地砖            [1] =>             [2] => 空心砖            [3] => 地砖        )    [cunit] => Array        (            [0] => 块            [1] =>             [2] => 块            [3] => 块        )    [unitprice] => Array        (            [0] => 35            [1] =>             [2] => 3.5            [3] => 35        )    [pronum] => Array        (            [0] => 2            [1] =>             [2] => 2            [3] =>         )    [summoney] => Array        (            [0] =>             [1] =>             [2] =>             [3] =>         ))
Salin selepas log masuk


			$data=$_POST['sale'];			//print_r($data);			foreach ($data as $k1 => $v1) {				foreach ($v1 as $k2 => $v2) {					$new_data[$k2][$k1] = $v2;					$new_data[$k2]['username']='admin';				}			}
Salin selepas log masuk

我想得到的最终结果是。name中数值不为空,且pronum数值不为空的结果。试了下if( $v2!=' ) 不对。特请教各位。谢谢了


回复讨论(解决方案)

$arr=array(    'name' => array        (            0 =>'地砖',            1 =>'',            2 => '空心砖',            3 => '地砖',        ),    'cunit' => array        (            0 => '块',            1 => '',            2 => '块',            3 => '块'        ),     'unitprice' => array        (            0 => 35,            1 => '',            2 => 3.5,            3 => 35        ),   'pronum' => array       (            0 => 2,            1 => '',            2 => 2,            3 =>''        ),   'summoney' => array        (            0 =>'',             1 =>'',             2 =>'',             3 =>''         )    );$new_data=array();foreach($arr as $k=>$v){    if($k=='name' || $k=='pronum'){        foreach($v as $newk=>$newv){            if($newv!=''){                $new_data[$k][]=$newv;            }                    }    }}print_r($new_data);[code=php]Array(    [name] => Array        (            [0] => 地砖            [1] => 空心砖            [2] => 地砖        )    [pronum] => Array        (            [0] => 2            [1] => 2        ))
Salin selepas log masuk




[/code]

其实处理成这样才便于后续处理

Array(    [0] => Array        (            [name] => 地砖            [cunit] => 块            [unitprice] => 35            [pronum] => 2            [summoney] =>         )    [2] => Array        (            [name] => 空心砖            [cunit] => 块            [unitprice] => 3.5            [pronum] => 2            [summoney] =>         ))
Salin selepas log masuk
$k = array_keys($data);foreach(call_user_func_array('array_map', array_merge(array(null), $data)) as $i=>$r) {  $r = array_combine($k, $r);  if($r['name'] && $r['pronum']) $res[$i] = $r;}
Salin selepas log masuk

谢谢xuzuning版主。这样处理周全点,同时谢谢luo19880415

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan