Viewed   106 times

When I use array_merge() with associative arrays I get what I want, but when I use them with numerical key arrays the keys get changed.

With + the keys are preserved but it doesn't work with associative arrays.

I don't understand how this works, can anybody explain it to me?



Because both arrays are numerically-indexed, only the values in the first array will be used.

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

array_merge() has slightly different behavior:

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

Tuesday, October 11, 2022

By assigning temporary keys, you can determine whether you are dealing with the first occurrence or not and then use the appropriate technique to either store the whole subarray or merely add value to the price element of the subarray.

Code: (Demo)

$to_account = [
  [ 'account' => 251234567890, 'price' => 83 ],
  [ 'account' => 251234567890, 'price' => 27 ],
  [ 'account' => 251234564526, 'price' => 180 ],
  [ 'account' => 251234567890, 'price' => 40 ]

foreach ($to_account as $row) {
    if (!isset($result[$row['account']])) {
        $result[$row['account']] = $row;
    } else {
        $result[$row['account']]['price'] += $row['price'];
        // imitate the above line if there was another column to sum


array (
  251234567890 => 
  array (
    'account' => 251234567890,
    'price' => 150,
  251234564526 => 
  array (
    'account' => 251234564526,
    'price' => 180,

This method does not bother to overwrite redundant account element values. To reindex the output array, merely call array_values() on it.

Sunday, October 16, 2022

The difference between ResolveUrl and ResolveClientUrl is that ResolveClientUrl returns a path relative to the current page, ResolveUrl returns a path relative to the site root:

I would recommend using absolute paths.

Edit: Rick Strahl posted a nice article about this

Edit2: Removed bit about caching. Does not add to the answer and may not necessarily be accurate.

Sunday, November 13, 2022

Here it is.

function array_merge_recursive_ex(array $array1, array $array2)
    $merged = $array1;

    foreach ($array2 as $key => & $value) {
        if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
            $merged[$key] = array_merge_recursive_ex($merged[$key], $value);
        } else if (is_numeric($key)) {
             if (!in_array($value, $merged)) {
                $merged[] = $value;
        } else {
            $merged[$key] = $value;

    return $merged;
Monday, December 12, 2022

The best way to explain the differences between these different items—that are related but different—is to break it down by example. Think of networks like a tree:

  • There is a trunk that separates into major branches.
  • Branches which in turn separate into smaller branches.
  • Smaller branches eventually leading to individual leaves.

The “trunk” and “branches” are “domains” and “subdomains” and the leaves are individual devices, like computers. So let’s start there.

  • A “computer name” is strictly a local convention: I have a computer named giacomo1968. This computer name is simply the name I have assigned my local machine. Nobody outside of my LAN will know this computer name; this is strictly a local setting.

  • A “hostname” (aka “nodename”) is a network identifier: If I wanted to publicly advertise my local computer to others, I would have to attach a “hostname” to the IP address of my computer. The “hostname” doesn’t really have to have anything to do with the computer name, but many times administrators like to use the same name to make things easier to understand. Also a “hostname” doesn’t always mean the computer is exposed to the Internet; it’s just an easy way to let others on your network where/what your computer is. As Wikipedia explains; emphasis is mine:

In computer networking, a hostname (archaically nodename) is a label that is assigned to a device connected to a computer network and that is used to identify the device in various forms of electronic communication such as the World Wide Web, e-mail or Usenet.

  • A fully qualified domain name (FQDN) is just that; a fully qualified domain name: Now that might seem confusing but you need to think of it this way: It just means in the great scheme of things, what is the actual “path” to get to a computer. So let’s say I have my computer named giacomo1968 and it has a hostname on my LAN that is giacomo1968. Within the context of my LAN, that giacomo1968 is my computer name, and is my “hostname” and can possibly be considered my “fully qualified domain name” depending on my LAN architecture. Meaning let’s say my office LAN has other nodes in it like, first_floor and second_floor and my “hostname” of giacomo1968 is on the second_floor network. Well, if that were the case, then my LAN-based “fully qualified domain name” would be giacomo1968.second_floor and that is it. But let’s say I worked at a big company named “” and my computer were somehow exposed to the world. Then in that case, my WAN-based “fully qualified domain name” could be or maybe even just if the network administrator didn’t want to be hassled with the “second_floor” designation. Again, as Wikipedia explains; emphasis is mine:

A fully qualified domain name (FQDN), sometimes also referred to as an absolute domain name, is a domain name that specifies its exact location in the tree hierarchy of the Domain Name System (DNS). It specifies all domain levels, including the top-level domain and the root zone. A fully qualified domain name is distinguished by its lack of ambiguity: it can be interpreted only in one way.

  • The “www” in some website names is a “hostname” designation that is historical in nature: Basically, back in the 1990s when the world wide web was still in it’s infancy, networks had many different services attached to them. And mainly in an academic context. So there would be a place like and that school would have FTP services on, email on and so on… So when the world wide web came along, they would just have placed the web server on Nowadays, everyone—and seemingly everything in the world—has a website. And many people just register domains just for the web service. So the historic convention of www is discarded in many cases. Many people have websites without www but will still have accommodations to redirect www traffic to the main, non-www hostname. But technically speaking, www can still be considered a hostname.

  • BONUS (Never Asked But Mentioned/Implied): What is a “subdomain” in the great scheme of things: A subdomain is basically just a child of a parent domain/hostname. So in my example of the giacomo1968 can be considered a subdomain of and big_company in that can be considered to be the “domain” with .com being the top-level domain (TLD). Yet again, as Wikipedia explains; emphasis is mine:

In the Domain Name System (DNS) hierarchy, a subdomain is a domain that is part of a larger domain.

Now after drafting all of this, it can be confusing. Many computer names are hostnames are fully-qualified domain names and in some cases they could be subdomains. It’s all a matter of context. And looking at articles online the words “domain,” “host” and “node” are used fairly loose and fast all over the place. So in my opinion, many uses of these terms are synonymous.

Which also helps to explain your confusion in item number 2:

If “mail” in and “developers” in are called subdomains why is en in called hostname? What is the difference between host name and subdomain?

The is a subdomain and a hostname. The en in the is a subdomain of the domain name And is in itself a hostname since has an IP address connected to it and thus a computer connected to that IP address as well. Meaning the en itself is semantically considered a subdomain, but it is also a hostname because there is a host (computer) connected to the IP address connected to So knowing that and are hostnames as well which are both subdomains of

To possibly make things clearer think of it like this; let’s use the non-existant subdomain as an example:

  • The org in is a top-level domain (TLD). As a top-level domain it does not resolve to an IP address. Thus it is not a hostname. It is simply a naming convention.
  • The in is a domain name and it has the potential to be a hostname as well if it is connected to an IP address and resolves to a computer when one goes to
  • The fakename in is a subdomain name since it is a child domain of in the context of the domain name itself. It can be a hostname if it connected to an IP address and resolves to a computer when one goes to

If you ping you are attempting to ping the fakename subdomain of If that ping dies — which it most likely will since it is fake — that means that the host is down or it does not exist. If it dies with an IP address connected to it, that would mean the hostname is down. If the ping dies with 100% no IP address connected to it, them it would mean that the subdomain is an invalid hostname.

Yes, this can all be confusing. But what it boils down to is the difference between what a domain/subdomain is in the context of DNS entries versus what domain/subdomain is in the context of having a host/computer connected to it.

So if this all started with your curiosity about www, that is a historic hostname/nodename/subdomain that is not really used as a convention as much anymore but is still so commonly used that many sites have some accommodations in place to “catch” requests to www and redirect them to the main hostname of a website.

Wednesday, August 31, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :