[PHP] Removing duplicate in an array… really fast

Let’s imagine you have a huge array, with say couple ten thousands of elements and you want to remove any duplicates entries in this array. As PHP programmer you’ll be thinking of array_unique. For a “small” sized array and one dimensional array this one does the trick.

I have some nice snippets which cover multi dimensional and/or huge sized array.

First: you have a huge, one dimensional array.

1
array_flip(array_flip(array_reverse($input,true)))

This one works only on one dimensional array. The key of the last element will be preserved and the keys are not sorted

Second: for a huge, multi dimensional array

1
2
3
4
5
6
7
8
9
function super_unique($array) {
  $result = array_map("unserialize", array_unique(array_map("serialize", $array)));
  foreach ($result as $key => $value) {
    if ( is_array($value) ) {
      $result[$key] = super_unique($value);
    }
  }
  return $result;
}

sources:

  1. http://dk.php.net/manual/en/function.array-unique.php#97285
  2. http://www.puremango.co.uk/2010/06/fast-php-array_unique-for-removing-duplicates/

[TYPO3] Redirect to last page

Problem of the week:

you have a password protected pages on your TYPO3 site. If you spread the URL of the pages and other user will be shown the login page, if they call the URL. Problem will be, after logging in, the user is not redirected to the last page, which they call.

The felogin extension of TYPO3 supports 6 type of redirect after login:

  1. defined by usergroup record
  2. defined by user record
  3. TypoScript or Flexform of the felogin extension
  4. GET/POST-Parameter
  5. Referer
  6. Domain entries

The solution is the “referer” redirection. The question is how. This is where we use the fabulous TypoScript.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[loginUser = ]
page.headerData.666 = COA_INT
page.headerData.666 {
	10 = TEXT
	10 {
		typolink {
			parameter = xxx
			additionalParams {
				data = getIndpEnv:TYPO3_REQUEST_URL
				rawUrlEncode = 1
				wrap = &referer=|
			}
			returnLast = url
		}
		wrap = <meta http-equiv="refresh" content="0; URL=/|"/>
	}
}
[end]

This TypoScript creates a redirect per meta tag (see wrap), but to which URL. The magic happens in the typolink function. xxx is your login page. The GET parameter “referer” will have the value of the last page, which the user visited.

The magic does not end here. You have to set to “referer” redirect in the flexform of the felogin extension.

felogin

so that’s all.. hope this could be help.

Cheers

 

 

Rewrite robots.txt

Your CMS (read: TYPO3) can handle multiple subdomain in one installation. Problem: you can only put one robots.txt in the htdocs. The following rewrite rule will solve the problemby rewriting robots.txt to robots_(subdomain).txt

1
2
3
4
5
6
7
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.tld [NC]
RewriteRule robots\.txt$ robots_%1.txt [L]
 
# default robots.txt
RewriteCond %{REQUEST_FILENAME} robots(_.*).txt$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule robots(_.*).txt$ robots_def.txt [L]

it will do following rewrite:

  • www.domain.tld/robots.txt will load robots_www.txt
  • sub1.domain.tld/robots.txt will load robots_sub1.txt, if it’s not exist, then robots_def.txt will be loaded

New plugin

you know… in Germany, they have one thing that bugs me a lot… privacy on web… ok it will annoy me also, if my data are spread out there without me knowing (may be it has been happening the time I wrote this). but the privacy law has problem if the guest data are sent to some foreign server (read Google). So instead Google Analytics, Piwik is used, since you can host your own analytics software and you can anonymize it, so it’s ok. So Piwik is used here….

Second.. Akismet. Who don’t know akismet… spam comment detecting service, which automatically remove those viagra ads (read “spam”) from the comments… but akismet is also a foreign server and your guests data are sent to some foreign server… so need to find something else to replace it… Here comes Antispambee. It’s supposed not to sent any data out from your server, but promising some spam accuracy… so I give it a chance… let’s see how good this thing is… BUT it is NOT a challange for you guys to spam my blog :)

you’ve been warned… watch out for the bees and so long…

munich

Gruß Gott, si ivan ke bayern, tepatnya ke munich. refreshing, weekend travel. naek kreta kyk jaman student dulu :)
jln dr rumah jam 7 pagi, msh gelap n thermometer “cuma” minus 1 :) skrg dah ada matahari n kereta br berangkat dari Nuernberg. dr frankfurt ke wuerzburg, trs ke nuernberg, n skrg finish di muenchen :) msh ada 1,5jam lagi.
so, bsk dr muenchen ke zugspitze. snow? maybe. lets see. ivan’s out

wifi day

oh yeah. for me this weekend is wifi day.

now otw to dietzenbach (like 30min with train) helping a friend setting up home network, wifi, isdn phone with 3 lines. and maybe this evening in frankfurt also setting up wifi in combination with cable modem. wow totally interesting…. bazinga ;)

maybe for us (read nerd) is simple, but to those poor people, networking is like hell. which cable should i use, what this black box for, or why the lamps are blinking and no google. yeah dumb question, but its frustating for them.

when do i have time for myself…. :(

Hello world!!!

it’s perhaps a sample wordpress post’s title, but trust me, it’s me writing this. As a nerd (you know what a nerd is, don’t you?), it’s a standard output for a first application created or configured…

Hello world.. it’s a new blog about me, my daily life, what I like and don’t like.. just everything about me. This blog will be multilanguage. I will be writing in english, indonesian and german (deutsch). So don’t ever leave a comment asking me to translate…. :-)

Happy blogging to me and happy reading to you all

Cheers :-)