Jump to content


Photo

[SOLVED] Remove things from arrays ...


  • Please log in to reply
12 replies to this topic

#1 QUACK

QUACK
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 August 2006 - 08:03 PM

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

$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' ) );

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

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

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 :)

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 15 August 2006 - 08:50 PM

try

<?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>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 QUACK

QUACK
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 August 2006 - 11:15 PM

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

Thanks.

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 15 August 2006 - 11:19 PM

Post the results of var_export() for the arrays then I can use those to set up the same arrays on my server
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 QUACK

QUACK
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 August 2006 - 11:24 PM

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

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


#6 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 15 August 2006 - 11:26 PM

Use array_intersect and/or array_diff

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

#7 QUACK

QUACK
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 August 2006 - 11:31 PM

No luck with either thoes two functions.

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 15 August 2006 - 11:35 PM

What happens if you swap £moo and $cow around in my code for that 2nd example
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 QUACK

QUACK
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 August 2006 - 11:36 PM

No change just says Array ( )

#10 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 15 August 2006 - 11:39 PM

<?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);
?>

Results in:

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
)

Which appears to work to me.

#11 QUACK

QUACK
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 August 2006 - 11:45 PM

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

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


#12 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 15 August 2006 - 11:48 PM

it work for me
<?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);
?>


#13 QUACK

QUACK
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 15 August 2006 - 11:56 PM

Hmm that works wounderfully for some reason :D thank you all for your help :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users