Jump to content

[SOLVED] Remove things from arrays ...


QUACK

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 :)
Link to comment
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]
Link to comment
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.
Link to comment
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]
Link to comment
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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.