Home > Backend Development > PHP Tutorial > PHP数组比较 求解解决思路

PHP数组比较 求解解决思路

WBOY
Release: 2016-06-13 13:13:17
Original
816 people have browsed it

PHP数组比较 求解
这是第一个数组
[code=PHP]
array
    0   =>  
        array
            0   =>   string   '1000061 '   (length=7)
            1   =>   string   '1000031 '   (length=7)
            2   =>   string   '1000056 '   (length=7)
    1   =>  
        array
            0   =>   string   '1000056 '   (length=7)
            1   =>   string   '1000049 '   (length=7)
    3   =>  
        array
            0   =>   string   '1000056 '   (length=7)
            1   =>   string   '1000048 '   (length=7)
[/code]
这是第二个数组
[code=PHP]
array
    0   =>   string   '1000047 '   (length=7)
    1   =>   string   '1000056 '   (length=7)
    2   =>   string   '1000061 '   (length=7)
[/code]

第一个数组   和第二个数组   比较   去除不相同的数组元素   还要保留第一个数组的结构

想要的效果是这样的
[code=PHP]
array
    0   =>  
        array
            0   =>   string   '1000061 '   (length=7)
            1   =>   string   '1000056 '   (length=7)
    1   =>  
        array
            0   =>   string   '1000056 '   (length=7)
    3   =>  
        array
            0   =>   string   '1000056 '   (length=7)

[/code]

哪位大侠帮我看下  


------解决方案--------------------
整理数据的时间比写代码的时间要长得多!

PHP code
$a = array(
  0 => array(
    0 => '1000061', 
    1 => '1000031', 
    2 => '1000056', 
    ),
  1 => array(
    0 => '1000056', 
    1 => '1000049', 
    ),
  3 => array(
    0 => '1000056', 
    1 => '1000048',
    ),
);
 
$b = array( 
  0 => '1000047', 
  1 => '1000056',
  2 => '1000061', 
);

foreach($a as &$v) $v = array_intersect($v, $b);

print_r($a); <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template