In PHP, how can I obtain the results of a few columns in the submitted two-dimensional array whose key values are not empty?

Source: Internet
Author: User
In PHP, how to obtain the submitted results of a few columns of the two-dimensional array whose key values are not empty is as follows:
Array ([name] => Array ([0] => floor tile [1] => [2] => hollow brick [3] => floor tile) [cunit] => Array ([0] => block [1] => [2] => block [3] => block) [unitprice] = & gt; Array ([0] = & gt; 35 [1] = & gt; [2] = & gt; 3.5 [3] = & gt; 35) [pronum] => Array ([0] => 2 [1] => [2] => 2 [3] =>) [summoney] => Array ([0] => [1] => [2] => [3] => ))


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

The final result I want is. Result where the value of name is not empty and the value of pronum is not empty. Tried if ($ v2! = ') Incorrect. I would like to ask you. Thank you.


Reply to discussion (solution)

$ Arr = array ('name' => array (0 => 'Tile ', 1 => '', 2 => 'hollow block ', 3 => 'barri',), 'cunit '=> array (0 => 'block', 1 => '', 2 => 'block ', 3 => 'block'), '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 $ new K => $ newv) {if ($ newv! = '') {$ New_data [$ k] [] = $ newv ;}}} print_r ($ new_data ); [code = php] Array ([name] => Array ([0] => floor tile [1] => hollow brick [2] => floor tile) [pronum] => Array ([0] => 2 [1] => 2 ))




[/Code]

In fact, this is easy for subsequent processing.

Array ([0] => Array ([name] => tile [cunit] => block [unitprice] => 35 [pronum] => 2 [summoney] =>) [2] => Array ([name] => hollow brick [cunit] => block [unitprice] => 3.5 [pronum] => 2 [summoney] => ))
$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;}

Thank you for the xuzuning moderator. Thanks to luo19880415.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.