how to concatenate array index with your previous one?

Asked

Viewed 132 times

1

I’m trying, inside a classe, create a property that will be populated by an array.

The problem is that the second index of this array depends on the value of the first one and would like to concatenate.

private $site = array (
    "dominio"           => "site.com.br",
    "www"               => "www.".->dominio,                

How to do this?

  • Like the $site is being populated? Through a set?

  • no! Via the array itself. I edited the question.

  • How about $site = ["dominio" => "site.com.br"]; $site["www"] = "www." . $site["dominio"];? You can also use the function end($site) to return the last element of the array, just be careful that the internal pointer will be modified.

  • "www" => $site["domain"],, did not roll;

  • even because the variable (array) $site is not yet complete on this line!

  • Something like this, https://ideone.com/TaNLrQ?

Show 1 more comment

1 answer

1

There is no way. What you can do is set the properties directly:

<?php

class Site 
{
    private $site = [
        'domain' => 'site.com.br',
        'www' => 'www.site.com.br'
    ];
}
  • that’s what I wouldn’t like to do. kkk. wanted to do automatic!

  • the way you want it doesn’t. What you can do is create it in the builder, but I don’t see an apparent benefit.

  • I don’t see it either!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.