php新手,求教各位大神,关于二维数组按照一定条件合并的问题

kangaa 2015-04-25 11:45:08
有这样类型的三个二维数组:
$arr = Array
(
"0" => Array(
"hzlx" => '买断',
"yfs" => '400',

),

"1" => Array(
"hzlx" => '关店',
"yfs" => '3',

),

"2" => Array(
"hzlx" => '关门',
"yfs" => '5',

),

"3" => Array(
"hzlx" => '待开发-未开门',
"yfs" => '1',

),

"4" => Array(
"hzlx" => '待开发-未谈',
"yfs" => '1',

),

"5" => Array(
"yfs" => '889',
"hzlx" => '独家',
),
"6" => Array(
"hzlx" => '独家-配货',
"yfs" => '2',

),
);
$arr1 = Array(
"0" => Array(
"hzlx" => '买断',
"shul" => '48555',
"cb" => '59633.90000000002',
"glcb" => '0',

),

"1" => Array(
"hzlx" => '独家',
"shul" => '82980',
"cb" => '97979.40000000002',
"glcb" => '0',

),

"2" => Array(
"hzlx" => '独家-配货',
"shul" => '17',
"cb" => '7.85',
"glcb" => '0',

),
);
$arr2 = Array(
"0" => Array(
"hzlx" => '买断',
"fkje" => '450',
"lzs" => '10',

),

"1" => Array(
"hzlx" => '独家',
"fkje" => '450',
"lzs" => '9',

),
);
按照相同的hzlx进行合并成这样的一个二维数组:
$arr3 = Array
(
[0] => Array(
[hzlx] => 买断,
[yfs] => 400,
[lzs] => 10,
[fkje] => 450,
[shul] => 48555,
[cb] => 59633.90000000002,
[glcb] => 0,
),

[1] => Array
(
[hzlx] => 关店,
[yfs] => '',
[lzs] => '',
[fkje] =>'',
[shul] => '',
[cb] => '',
[glcb] => '',
),

[2] => Array
(
[hzlx] => 关门,
[yfs] => '',
[lzs] => '',
[fkje] => '',
[shul] => '',
[cb] => '',
[glcb] => '',
),

[3] => Array
(
[hzlx] => 待开发-未开门,
[yfs] => '',
[lzs] => '',
[fkje] => '',
[shul] => '',
[cb] => '',
[glcb] => '',
),

[4] => Array
(
[hzlx] => 待开发-未谈,
[yfs] => '',
[lzs] => '',
[fkje] => '',
[shul] => '',
[cb] => '',
[glcb] => '',
),

[5] => Array
(
[hzlx] => 独家,
[yfs] => '889',
"fkje" => '450',
"lzs" => '9',
"shul" => '82980',
"cb" => '97979.40000000002',
"glcb" => '0',
),


[6] => Array
(
[hzlx] => 独家-配货,
[yfs] => '2',
[lzs] =>'',
[fkje] =>'',
"shul" => '17',
"cb" => '7.85',
"glcb" => '0',
),
);
以第一个数组为基准进行合并,没有的为空或0
...全文
141 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2015-04-25
  • 打赏
  • 举报
回复
$arr = Array(
  "0" => Array(
    "hzlx" => '买断',
    "yfs" => '400',
    ),
  "1" => Array(
    "hzlx" => '关店',
    "yfs" => '3',
    ),
  "2" => Array(
    "hzlx" => '关门',
    "yfs" => '5',
    ),

  "3" => Array(
    "hzlx" => '待开发-未开门',
    "yfs" => '1',
    ),
  "4" => Array(
    "hzlx" => '待开发-未谈',
    "yfs" => '1',
    ),
  "5" => Array(
    "yfs" => '889',
    "hzlx" => '独家',
    ),
  "6" => Array(
    "hzlx" => '独家-配货',
    "yfs" => '2',
    ),
);

$arr1 = Array(
  "0" => Array(
    "hzlx" => '买断',
    "shul" => '48555',
    "cb" => '59633.90000000002',
    "glcb" => '0',
    ),
  "1" => Array(
    "hzlx" => '独家',
    "shul" => '82980',
    "cb" => '97979.40000000002',
    "glcb" => '0',
    ),
  "2" => Array(
    "hzlx" => '独家-配货',
    "shul" => '17',
    "cb" => '7.85',
    "glcb" => '0',
    ),
);

$arr2 = Array(
  "0" => Array(
    "hzlx" => '买断',
    "fkje" => '450',
    "lzs" => '10',
  ),
  "1" => Array(
    "hzlx" => '独家',
    "fkje" => '450',
    "lzs" => '9',
    ),
);

foreach($arr as $r) $t[$r['hzlx']] = $r;
foreach($arr1 as $r) $t[$r['hzlx']] = array_merge($t[$r['hzlx']], $r);
foreach($arr2 as $r) $t[$r['hzlx']] = array_merge($t[$r['hzlx']], $r);

$fmt = array(
  'hzlx' => '',
  'yfs' => '',
  'lzs' => '',
  'fkje' =>'',
  'shul' => '',
  'cb' => '',
  'glcb' => '',
);
foreach($t as $r) $arr3[] = array_merge($fmt, $r);
 
print_r($arr3);
Array
(
    [0] => Array
        (
            [hzlx] => 买断
            [yfs] => 400
            [lzs] => 10
            [fkje] => 450
            [shul] => 48555
            [cb] => 59633.90000000002
            [glcb] => 0
        )

    [1] => Array
        (
            [hzlx] => 关店
            [yfs] => 3
            [lzs] => 
            [fkje] => 
            [shul] => 
            [cb] => 
            [glcb] => 
        )

    [2] => Array
        (
            [hzlx] => 关门
            [yfs] => 5
            [lzs] => 
            [fkje] => 
            [shul] => 
            [cb] => 
            [glcb] => 
        )

    [3] => Array
        (
            [hzlx] => 待开发-未开门
            [yfs] => 1
            [lzs] => 
            [fkje] => 
            [shul] => 
            [cb] => 
            [glcb] => 
        )

    [4] => Array
        (
            [hzlx] => 待开发-未谈
            [yfs] => 1
            [lzs] => 
            [fkje] => 
            [shul] => 
            [cb] => 
            [glcb] => 
        )

    [5] => Array
        (
            [hzlx] => 独家
            [yfs] => 889
            [lzs] => 9
            [fkje] => 450
            [shul] => 82980
            [cb] => 97979.40000000002
            [glcb] => 0
        )

    [6] => Array
        (
            [hzlx] => 独家-配货
            [yfs] => 2
            [lzs] => 
            [fkje] => 
            [shul] => 17
            [cb] => 7.85
            [glcb] => 0
        )

)

kangaa 2015-04-25
  • 打赏
  • 举报
回复
万分感谢大神 谢谢!

21,888

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧