Jump to content

Archived

This topic is now archived and is closed to further replies.

QUACK

[SOLVED] Remove things from arrays ...

Recommended Posts

Basicly, I have an array called say, $moo ... and another $cow.

[code]$cow = array ( '0' => 'Hey', '2' => 'Woo', '3' => 'Poop', '4' => 'Cool!', 'YAY' => Array ( '0' => 'Hi', '2' => 'Boo' ) );
$moo = array ( '0' => 'Woo', '2' => 'Hey', '3' => 'Nice!', 'YAY' => Array ( '0' => 'Hi' ) );[/code]

How can I run though $moo and remove all the values that $cow has from $moo ...
So in the end, $moo becomes:

[code]$moo = array ( '3' => 'Poop', '4' => 'Cool!', 'YAY!' => Array ( '2' => 'Boo' ) );[/code]

So $cow and $moo both have Hey and Woo so we remove that from $moo ... they both have YAY => Hi so remove that $moo too.

Thanks for the help :)

Share this post


Link to post
Share on other sites
try

[code]<?php
$cow = array ( '0' => 'Hey', '2' => 'Woo', '3' => 'Poop', '4' => 'Cool!', 'YAY' => Array ( '0' => 'Hi', '2' => 'Boo' ) );
$moo = array ( '0' => 'Woo', '2' => 'Hey', '3' => 'Nice!', 'YAY' => Array ( '0' => 'Hi' ) );

$res = array();
foreach ($cow as $k =>$val) {
if (is_array($val)) {
$tmp = array_diff($cow[$k], $moo[$k]);
if ($tmp) $res[$k] = $tmp;
}
else {
if (!in_array($val, $moo))
$res[$k] = $val;
}
}


echo '<pre>', print_r($res, true), '</pre>';
?>[/code]

Share this post


Link to post
Share on other sites
Okay that worked for that example array but it dident for some reason on this:
I had $cow set to [code]Array ( [0] => logo.gif [WOW] => Array ( [0] => cool.gif [1] => Saved Game ) )[/code] and $moo set to [code]Array ( [0] => hey.txt [1] => logo.gif [WOW] => Array ( [0] => cool.gif [1] => Saved Game [2] => OK! ) )[/code] I SHOULD have been left with [code]$res = Array ( '0' => 'hey.txt', 'WOW' => Array ( '0' => 'OK!' ) ); [/code]but it returned nothing :/ why ?

Thanks.

Share this post


Link to post
Share on other sites
Post the results of var_export() for the arrays then I can use those to set up the same arrays on my server

Share this post


Link to post
Share on other sites
From $moo
[code]array ( 0 => 'hey.txt', 1 => 'logo.gif', 'Wow' => array ( 0 => 'cool.gif', 1 => 'Saved Game', 2 => 'OK!' ,), )[/code]

From $cow
[code]array ( 0 => 'logo.gif', 'Wow' => array ( 0 => 'cool.gif', 1 => 'Saved Game' ), )[/code]

Share this post


Link to post
Share on other sites
Use array_intersect and/or array_diff

http://www.php.net/array_intersect
http://www.php.net/array_diff

Share this post


Link to post
Share on other sites
What happens if you swap £moo and $cow around in my code for that 2nd example

Share this post


Link to post
Share on other sites
[code]<?php

$moo = array ( 0 => 'hey.txt', 1 => 'logo.gif', 'Wow' => array ( 0 => 'cool.gif', 1 => 'Saved Game', 2 => 'OK!' ,), );
$cow = array ( 0 => 'logo.gif', 'Wow' => array ( 0 => 'cool.gif', 1 => 'Saved Game' ), );

$diff = array_intersect($cow, $moo);
$iff = array_diff($moo, $cow);

echo '<pre>';
print_r($moo);
echo '<br />';
print_r($cow);
echo '<br />';
print_r($diff);
echo '<br />';
print_r($iff);
?>[/code]

Results in:

[code]Array
(
    [0] => hey.txt
    [1] => logo.gif
    [Wow] => Array
        (
            [0] => cool.gif
            [1] => Saved Game
            [2] => OK!
        )

)

Array
(
    [0] => logo.gif
    [Wow] => Array
        (
            [0] => cool.gif
            [1] => Saved Game
        )

)

Array
(
    [0] => logo.gif
    [Wow] => Array
        (
            [0] => cool.gif
            [1] => Saved Game
        )

)

Array
(
    [0] => hey.txt
)[/code]

Which appears to work to me.

Share this post


Link to post
Share on other sites
But intersect removes hey.txt and diff doesent include Wow => 'OK!' ... Im trying to get the end result of the the array to be

[code]Array ( [0] => hey.txt [Wow] => Array ( [0] => OK! ) );[/code]

Share this post


Link to post
Share on other sites
it work for me[code]
<?php
$a=array ( 0 => 'hey.txt', 1 => 'logo.gif', 'Wow' => array ( 0 => 'cool.gif', 1 => 'Saved Game', 2 => 'OK!' ,), );
$b= array ( 0 => 'logo.gif', 'Wow' => array ( 0 => 'cool.gif', 1 => 'Saved Game' ), );
// a -b
function rem ($a, $b) {
foreach ($a as $k => $v) {
if (is_array($v)) {
if (key_exists($k, $b)) {
$c= rem($v, $b[$k]);
if (count($c)) $out[$k] = $c;
}
} elseif (!in_array($v, $b)) $out[$k] = $v; //or $out[] = $v
}
return $out;
}

$c = rem($a,$b);
print_r($c);
?>[/code]

Share this post


Link to post
Share on other sites
Hmm that works wounderfully for some reason :D thank you all for your help :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.