开发者

php Getting the 'new' values in an array

开发者 https://www.devze.com 2022-12-23 06:01 出处:网络
Trying to learn about php\'s arrays today. I have a set of arrays like this: $a = array ( 0 => array ( \'value\' => \'America\', ),

Trying to learn about php's arrays today.

I have a set of arrays like this:

$a = array ( 
    0 => array ( 'value' => 'America', ), 
    1 => array ( 'value' => 'England', ),  
    2 => array ( 'value' => 'Australia', ), 
)
$b = array ( 
    0 => array ( 'value' => 'America', ), 
    1 => arra开发者_Python百科y ( 'value' => 'England', ), 
    2 => array ( 'value' => 'Canada', ), 
)

I need to get the 'new' subarrays that array b brings to the table.

ie, I need to return array ( 'value' => 'Canada', )

I thought I could first merge $a+$b and then compare the result to $a.

$merge = array_merge($a,$b);
$result = array_diff($merge,$a);

But somehow that does not work. It returns array()

How come? And thanks!


This should do the trick:

$new = array();
foreach($b as $elem){
    if(!in_array($elem, $a)){
        $new[] = $elem;
    }
}

Returns Canada.


I'm not a hundred per cent sure on what you are asking. If you want to return the Canada part of array $b in a function you can use return $b[2]. That will return an array containing value=>Canada. If you want to display the content of the array you can use the var_dump function like this.

var_dump($VaribleToDisplay);

var_dump can also be used for other variable types.


If I run your code until array_merge($a, $b) and do a var_dump($merge), I get this:

array(6) {
  [0]=>
  array(1) {
    ["value"]=>
    string(7) "America"
  }
  [1]=>
  array(1) {
    ["value"]=>
    string(7) "England"
  }
  [2]=>
  array(1) {
    ["value"]=>
    string(9) "Australia"
  }
  [3]=>
  array(1) {
    ["value"]=>
    string(7) "America"
  }
  [4]=>
  array(1) {
    ["value"]=>
    string(7) "England"
  }
  [5]=>
  array(1) {
    ["value"]=>
    string(6) "Canada"
  }
}

That's not, what you had in mind.

--

Moreover, array_diff's documentation on php.net reads:

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.
Note: This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);. 


Since your array contains subarrays indexed by common keys, you can't use array_merge, array_diff, etc because those deal with arrays indexed by keys (no sub arrays).

You can use array_walk to do what you want, but will take a lot more code. Consider something similar to:

$newVals = array();

function walker($sub, $key, $otherArray)
{
    $found = false;
    foreach($otherArray as $idx => $sub2)
    {  
        if ($sub['value'] == $sub2['value'])
        {
            $found = true;
            break;
        }
    }
    if (!$found)
        $newVals[] = $sub;
}

array_walk($a, 'walker', $b);

This isn't particularly fast. If you can change your input data to use key=>value directly in the array (rather than sub arrays) then array_diff and array_merge will work and will be much faster.

i.e. you would really want to restructure your data like this:

 $a = array( 'America', 'Australia', 'Canada' );

Or something similar.


why would you set up your array like that is this for a select or something?

$a = array('countries'=> array('America', 'England', 'Australia'));
$b = array('countries'=> array('America', 'England', 'Canada'));
array_diff($b['countries'], $a['countries']);
0

精彩评论

暂无评论...
验证码 换一张
取 消