在多维数组的数组值 [英] array values in multidimensional array

查看:86
本文介绍了在多维数组的数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组

他们看起来像

  $ A1 =阵列(
  阵列('NUM'=大于1,'名'=>一),
  阵列('NUM'=大于2,'名'=>两节),
  阵列('NUM'= GT; 3,'名'=>'三'),
  阵列('NUM'=> 4,'名'= GT;四),
  阵列('NUM'=> 5,'名'= GT;十二五)
)$ A2 =阵列(3,4,5,6,7,8);

我想一个数组,看起来像

要结束了

  $ A3 =阵列(3,4,5);

所以基本上,其中 $ A1 [$ i] ['NUM'] $ A2

我知道我能做到

  $ A3 =阵列();的foreach($ A1为$ NUM)
 如果(array_search($ NUM ['NUM'],$ A2))
   $ A3 [] = $ NUM ['NUM'];

但是,好像很多非所需的迭代。

有没有更好的办法?



嗯...对齐

我才意识到我问这个问题的错误的方式的时候,我想用一个数组,看起来像

要结束了

  $ A3阵列(
  阵列('NUM'= GT; 3,'名'=>'三'),
  阵列('NUM'=> 4,'名'= GT;四),
  阵列('NUM'=> 5,'名'= GT;十二五)


解决方案

您可以提取相关信息的(即NUM项目)的从 $ A1

  $ a1_bis =阵列();
的foreach($ A1为$ a){
    $ a1_bis [] = $ A ['NUM'];
}

,然后,使用 array_intersect() 找一下既是 $ a1_bis $ A2

  $结果= array_intersect($ a1_bis,$ A2);
后续代码var_dump($结果);

这将让你:

 阵列
  2 => INT 3
  3 => INT 4
  4 => INT 5

结果
有了这个解决方案:


  • 您是通过 $ A1 将只有一次

  • 您信任的PHP使用好的算法来找到两个阵列之间的交集的(和/或考虑在C开发的功能可能比你能code在纯PHP任何同等速度更快)

结果


注释后编辑:好,考虑到你想要的结果,现在,我会去用另一种方法

首先,我会用 array_flip() 到的翻转的的 $ A2 阵列,允许更快地访问其元素的(按键访问是方式比找到一个值更快)的:

  $ a2_hash = array_flip($ A2); //为了加快速度:
                            //通过访问关键的不是寻找更快的方法
                            //由值数组中的项

然后,我会用 array_filter() 到过滤器应用于 $ A1 ,保持物品而 NUM 是在 $ a2_hash 翻转阵列:

  $结果= array_filter($ A1,功能($项目)使用($ a2_hash){
    如果(使用isset($ a2_hash [$项目['NUM'])){
        返回true;
    }
    返回false;
});
后续代码var_dump($结果);

注:我用了一个匿名函数,只用PHP存在> = 5.3;如果你正在使用PHP和LT; 5.3,本code可以重新合作,共进晚餐preSS关闭。

就这样,我得到你想要的数组:

 阵列
  2 =>
    排列
      'NUM'=> INT 3
      '名'=>字符串'三'(长度= 5)
  3 =>
    排列
      'NUM'=> INT 4
      '名'=>串四(长度= 4)
  4 =>
    排列
      'NUM'=> INT 5
      '名'=>字符串十二五(长度= 4)

请注意该键不对应任何有用的东西 - 如果你想他们的删除的,只是使用的 array_values​​() 功能 $结果

  $ final_result = array_values​​($结果);

但是,这可能不是必要的: - )

I have two arrays

they look like

$a1 = array(
  array('num' => 1, 'name' => 'one'),
  array('num' => 2, 'name' => 'two'),
  array('num' => 3, 'name' => 'three'),
  array('num' => 4, 'name' => 'four'),
  array('num' => 5, 'name' => 'five')
)

$a2 = array(3,4,5,6,7,8);

I want to end up with an array that looks like

$a3 = array(3,4,5);

so basically where $a1[$i]['num'] is in $a2

I know I could do

$a3 = array();

foreach($a1 as $num)
 if(array_search($num['num'], $a2))
   $a3[] = $num['num'];

But that seems like a lot of un-needed iterations.

Is there a better way?


Ah Snap...
I just realized I asked this question the wrong way around, I want to end up with an array that looks like

$a3 array(
  array('num' => 3, 'name' => 'three'),
  array('num' => 4, 'name' => 'four'),
  array('num' => 5, 'name' => 'five')
)

解决方案

You could extract the relevant informations (the 'num' items) from $a1 :

$a1_bis = array();
foreach ($a1 as $a) {
    $a1_bis[] = $a['num'];
}

And, then, use array_intersect() to find what is both in $a1_bis and $a2 :

$result = array_intersect($a1_bis, $a2);
var_dump($result);

Which would get you :

array
  2 => int 3
  3 => int 4
  4 => int 5


With this solution :

  • you are going through $a1 only once
  • you trust PHP on using a good algorithm to find the intersection between the two arrays (and/or consider that a function developed in C will probably be faster than any equivalent you could code in pure-PHP)



EDIT after the comment : well, considering the result you want, now, I would go with another approach.

First, I would use array_flip() to flip the $a2 array, to allow faster access to its elements (accessing by key is way faster than finding a value) :

$a2_hash = array_flip($a2); // To speed things up : 
                            // accessing by key is way faster than finding 
                            // an item in an array by value

Then, I would use array_filter() to apply a filter to $a1, keeping the items for which num is in the $a2_hash flipped-array :

$result = array_filter($a1, function ($item) use ($a2_hash) {
    if (isset($a2_hash[ $item['num'] ])) {
        return true;
    }
    return false;
});
var_dump($result);

Note : I used an anonymous function, which only exist with PHP >= 5.3 ; if you are using PHP < 5.3, this code can be re-worked to suppress the closure.

With that, I get the array you want :

array
  2 => 
    array
      'num' => int 3
      'name' => string 'three' (length=5)
  3 => 
    array
      'num' => int 4
      'name' => string 'four' (length=4)
  4 => 
    array
      'num' => int 5
      'name' => string 'five' (length=4)

Note the keys are not corresponding to anything useful -- if you want them removed, just use the array_values() function on that $result :

$final_result = array_values($result);

But that's probably not necessary :-)

这篇关于在多维数组的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆