Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62678 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 8744 invoked from network); 2 Sep 2012 18:05:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Sep 2012 18:05:00 -0000 Authentication-Results: pb1.pair.com smtp.mail=theanomaly.is@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=theanomaly.is@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.170 as permitted sender) X-PHP-List-Original-Sender: theanomaly.is@gmail.com X-Host-Fingerprint: 209.85.217.170 mail-lb0-f170.google.com Received: from [209.85.217.170] ([209.85.217.170:49448] helo=mail-lb0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A3/A2-17065-ACF93405 for ; Sun, 02 Sep 2012 14:04:59 -0400 Received: by lbbgp3 with SMTP id gp3so2166323lbb.29 for ; Sun, 02 Sep 2012 11:04:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=pkcgWI//s0VyVO7z+XSas0xOQpD8WcYKVMXECOiPwJw=; b=Fz4CnSRGEtYbf2C8dm/p1ckA/mN/XCvx8Lk3Z1YLNFgEwc5BOMBP1+LDDmhyrC2A/L hHHthqa3FnJ+iP07Iut9YU/Px4WOoANIDKXgh1oPNqStFPYRnHhjGcy9Np6dRAVtRpTV Bmb6wYcLAkUnz3RBoOFlTnsJrRBu53fgmOBS3c7vxuUcChpqUh94cPQJwxlZimAgNBD+ O0FAAFL76MuHpGX/AzKLQJ8Ks9ZDW0vO0M668X2P4zPcgbOFCbZEjb/Uxqdzr3LuZvxV +ROpOc/vD04lmRHP3Ycm1VaVCin1jPh8bp94uREXWMRlE1Ic2ecfK0CJuAkTPjVsQM8Q KMzA== MIME-Version: 1.0 Received: by 10.112.88.2 with SMTP id bc2mr2849834lbb.61.1346609094833; Sun, 02 Sep 2012 11:04:54 -0700 (PDT) Received: by 10.112.12.178 with HTTP; Sun, 2 Sep 2012 11:04:54 -0700 (PDT) In-Reply-To: References: Date: Sun, 2 Sep 2012 14:04:54 -0400 Message-ID: To: Amaury Bouchard Cc: PHP Internals Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Question about hashtables implementation From: theanomaly.is@gmail.com (Sherif Ramadan) > Hi all, > > I have a question about the internal implementation of PHP's hashtables. I > did some researches, but I didn't find the answer. > > Here is an example of what I would like to understand. > Start by creating an array: > $a = array(); > > Fill it, using implicit and explicit keys: > $a[] = 'cc'; > $a[1] = 'dd'; > > If we look at what's inside, we get: > Array > ( > [0] => cc > [1] => dd > ) > > OK, everything is obvious. Now, I add a value at the beginning of the array: > array_unshift($a, 'ee'); > > Do a print_r() again: > Array > ( > [0] => ee > [1] => cc > [2] => dd > ) > > As you can see, the keys for 'cc' and 'dd' have been recalculated. It works > as espected. > My question is how does it work? Are all numeric keys computed when the > array_shift() is done? Or is the iterator calculating them on-the-fly? > > Thanks. > > Amaury To clarify, this particular functionality you're using as an example "array_unshift" really isn't specific to the internal implementation of hashtables in PHP. That is to say that this side-effect you're describing is specific to that function and not necessarily hashtables' internal implementation. Essentially, array_unshift() just rebuilds the array. From http://php.net/array-unshift "All numerical array keys will be modified to start counting from zero while literal keys won't be touched" If we were to write a PHP user-land implementation of array_unshift() it would probably look something like this... function array_unshift(&$array) { $new_array = array(); foreach (array_slice(func_get_args(),1) as $e) { $new_array[] = $e; } foreach ($array as $k => $v) { if (is_numeric($k)) { $new_array[] = $v; } else { $new_array[$k] = $v; } } $array = $new_array; return count($array); } If you're interested in seeing the actual code for array_unshift's internal implementation you can take a look here: http://lxr.php.net/xref/PHP_5_4/ext/standard/array.c#2049 As for how the internal implementation of hashtables in PHP, that can be quite an extensive topic. PHP uses hashtables for pretty much everything internally. The hashtable struct can be seen here http://lxr.php.net/xref/PHP_5_4/Zend/zend_hash.h#66 If you're interested in a more detailed explanation of the implementation of PHP's Array type you might find this article by Nikic useful: http://nikic.github.com/2012/03/28/Understanding-PHPs-internal-array-implementation.html