trq Posted January 31, 2007 Share Posted January 31, 2007 Its been a long time since Ive had to post a question on this forum, and this is one that could probably go in the [i]PHP Challenge[/i] thread but I'm gonna post it here and see how we go.I have a slightly unusual problem. I have three arrays...[code=php:0]$global = array('a','b','c','d');$local = array('b','-c','-d');$user = array('e','c','f');[/code]There three arrays are meant to represent a tree of user settings. The first ($global) are the defaults, these can then be overwritten by $local and $user while $global and $local can be overwritten by $user.. Notice the values starting with - (eg -c) in $local? This meens that the values c and d need to be removed from $global. Notice there is extra values (e and f) defined in $user? These need to be added to the final array.Its kind of hard to explain, but I hope someone uderstands. the final array I need to make from these three arrays would contain the values a b c e f.Here is one of my many attempts, but I'm just not getting the logic here. This code is also outputting warning about invalid array indexes on lines 28 and 29. I know what the problem is there, but really, the logic here just isn't working.Anyone care to have a crack at this puzzle?[code]#!/usr/bin/php<?php $global = array('a','b','c','d'); $local = array('b','-c','-d'); $user = array('e','c','f'); # expected results - a b c e f function cleanuse($g,$l,$u) { print_r($g); print_r($l); print_r($u); foreach ($u as $user) { if (substr($user,0,1) == '-') { unset($l[array_keys($l,$user)]); unset($l[array_keys($l,substr($user,1))]); unset($g[array_keys($g,$user)]); unset($g[array_keys($g,substr($user,1))]); } } foreach ($l as $local) { if (substr($local,0,1) == '-') { unset($g[array_keys($g,$local)]); # line 28 unset($g[array_keys($g,substr($local,1))]); # line 29 } } print_r($g); print_r($l); print_r($u); $tmp = array_merge($g,$l,$u); $return = array_unique($tmp); print_r($return); } cleanuse($global,$local,$user);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/36475-solved-an-unusual-array-puzzle/ Share on other sites More sharing options...
Orio Posted January 31, 2007 Share Posted January 31, 2007 The problem is, that array_keys returns an array.So you need to do something like this (will take the first loop as an example):[code]<?phpforeach ($u as $user){ if (substr($user,0,1) == '-') { $key = array_keys($l,$user); unset($l[key[0]]); $key = array_keys($l,substr($user,1)); unset($l[$key]); $key = array_keys($g,$user); unset($g[$key[0]]); $key = array_keys($g,substr($user,1)) unset($g[$key[0]]); }}?>[/code]I think this is a start :DOrio. Quote Link to comment https://forums.phpfreaks.com/topic/36475-solved-an-unusual-array-puzzle/#findComment-173602 Share on other sites More sharing options...
trq Posted January 31, 2007 Author Share Posted January 31, 2007 Damn... it does too! In all the heartache I guess I got a little flustered, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/36475-solved-an-unusual-array-puzzle/#findComment-173606 Share on other sites More sharing options...
HuggieBear Posted January 31, 2007 Share Posted January 31, 2007 Thorpe,I like the look of this one and I'm trying to come up with a solution myself. Can you post once you have one and I'll do the same.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/36475-solved-an-unusual-array-puzzle/#findComment-173609 Share on other sites More sharing options...
trq Posted January 31, 2007 Author Share Posted January 31, 2007 Ok... thats still not working. I think its because when I use unset, its obviously moving key positions around.I think I need to rethink the logic completely. [url=http://www.thorpesystems.com/cleanuse.phps]here[/url] is a link to my latest changes (Sorry about the indentation, must be a bug in th eway phps works).Anyway... I'm off to do some real work. I'll have a think about this and be back. Quote Link to comment https://forums.phpfreaks.com/topic/36475-solved-an-unusual-array-puzzle/#findComment-173610 Share on other sites More sharing options...
trq Posted January 31, 2007 Author Share Posted January 31, 2007 [quote]Can you post once you have one and I'll do the same.[/quote]I'll bet you beat me too it. Ive actually been on this for several hours. Quote Link to comment https://forums.phpfreaks.com/topic/36475-solved-an-unusual-array-puzzle/#findComment-173611 Share on other sites More sharing options...
trq Posted January 31, 2007 Author Share Posted January 31, 2007 I got it working but its ugly as sin, look at all those loops. There MUST be a better way. I'm think recursion maybe but one again, the logic eludes me.[code]#!/usr/bin/php<?php $global = array('a','b','c','d'); $local = array('b','-c','-d'); $user = array('e','c','f'); function cleanuse($g,$l,$u) { $rm = array(); foreach ($u as $user) { if (substr($user,0,1) == '-') { $rm[] = substr($user,1); } } foreach ($rm as $remove) { if (in_array($remove,$l)) { $key = array_keys($l,$remove); unset($l[$key[0]]); } if (in_array($remove,$g)) { $key = array_keys($g,$remove); unset($g[$key[0]]); } } foreach ($l as $local) { if (substr($local,0,1) == '-') { $rm[] = substr($local,1); } } foreach ($rm as $remove) { if (in_array($remove,$g)) { $key = array_keys($g,$remove); unset($g[$key[0]]); } } $tmp = array_merge($g,$l,$u); foreach ($tmp as $all) { if (substr($all,0,1) != '-') { $return[] = $all; } } return array_unique($return); } print_r(cleanuse($global,$local,$user));?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/36475-solved-an-unusual-array-puzzle/#findComment-173668 Share on other sites More sharing options...
Orio Posted January 31, 2007 Share Posted January 31, 2007 You can minimize the number of lines, but I think the way you are doing it is one of the fastest. Recursion with php is very slow...Here's a shorter version, basically doing the same thing, I don't know if it's more effective:[code]<?php$global = array('a','b','c','d');$local = array('b','-c','-d');$user = array('e','c','f');function cleanuse($g,$l,$u){ foreach ($u as $user) //user filter { if (substr($user,0,1) == '-') { $char = substr($user,1); if (count($key = array_keys($l,$char)) != 0) unset($l[$key[0]]); if (count($key = array_keys($g,$char)) != 0) unset($g[$key[0]]); } } foreach ($l as $local) //local filter { if (substr($local,0,1) == '-' && count($key = array_keys($g,substr($local,1))) != 0) unset($g[$key[0]]); } $tmp = array_merge($g,$l,$u); $return = array(); foreach ($tmp as $all) { if (substr($all,0,1) != '-') $return[] = $all; } return array_unique($return);}print_r(cleanuse($global,$local,$user));?>[/code]Orio.P.S- I tested it and it gave the same output Quote Link to comment https://forums.phpfreaks.com/topic/36475-solved-an-unusual-array-puzzle/#findComment-173708 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.