QUACK Posted August 15, 2006 Share Posted August 15, 2006 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/17670-solved-remove-things-from-arrays/ Share on other sites More sharing options...
Barand Posted August 15, 2006 Share Posted August 15, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/17670-solved-remove-things-from-arrays/#findComment-75325 Share on other sites More sharing options...
QUACK Posted August 15, 2006 Author Share Posted August 15, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/17670-solved-remove-things-from-arrays/#findComment-75392 Share on other sites More sharing options...
Barand Posted August 15, 2006 Share Posted August 15, 2006 Post the results of var_export() for the arrays then I can use those to set up the same arrays on my server Quote Link to comment https://forums.phpfreaks.com/topic/17670-solved-remove-things-from-arrays/#findComment-75395 Share on other sites More sharing options...
QUACK Posted August 15, 2006 Author Share Posted August 15, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/17670-solved-remove-things-from-arrays/#findComment-75400 Share on other sites More sharing options...
hitman6003 Posted August 15, 2006 Share Posted August 15, 2006 Use array_intersect and/or array_diffhttp://www.php.net/array_intersecthttp://www.php.net/array_diff Quote Link to comment https://forums.phpfreaks.com/topic/17670-solved-remove-things-from-arrays/#findComment-75402 Share on other sites More sharing options...
QUACK Posted August 15, 2006 Author Share Posted August 15, 2006 No luck with either thoes two functions. Quote Link to comment https://forums.phpfreaks.com/topic/17670-solved-remove-things-from-arrays/#findComment-75405 Share on other sites More sharing options...
Barand Posted August 15, 2006 Share Posted August 15, 2006 What happens if you swap £moo and $cow around in my code for that 2nd example Quote Link to comment https://forums.phpfreaks.com/topic/17670-solved-remove-things-from-arrays/#findComment-75410 Share on other sites More sharing options...
QUACK Posted August 15, 2006 Author Share Posted August 15, 2006 No change just says Array ( ) Quote Link to comment https://forums.phpfreaks.com/topic/17670-solved-remove-things-from-arrays/#findComment-75412 Share on other sites More sharing options...
hitman6003 Posted August 15, 2006 Share Posted August 15, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/17670-solved-remove-things-from-arrays/#findComment-75414 Share on other sites More sharing options...
QUACK Posted August 15, 2006 Author Share Posted August 15, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/17670-solved-remove-things-from-arrays/#findComment-75416 Share on other sites More sharing options...
sasa Posted August 15, 2006 Share Posted August 15, 2006 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 -bfunction 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] Quote Link to comment https://forums.phpfreaks.com/topic/17670-solved-remove-things-from-arrays/#findComment-75419 Share on other sites More sharing options...
QUACK Posted August 15, 2006 Author Share Posted August 15, 2006 Hmm that works wounderfully for some reason :D thank you all for your help :) Quote Link to comment https://forums.phpfreaks.com/topic/17670-solved-remove-things-from-arrays/#findComment-75427 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.