3

Is there a possibility to reference an element of an array in another element with the same array?

Let's say we want to make an array like this:

$a = array(
    'base_url' => 'https://rh.example.com',
    'URL_SLO_OpenAM_OIC' => 'https://openam.example.com/openam/UI/Logout?goto='.$this['base_url'],
);

Of course, it doesn't work because $this is intended for classes not for arrays. So is there an alternative?

6
  • why are you using $this? Are you using it in the scope of a class? Commented Apr 22, 2016 at 8:02
  • 1
    No, I wanted just to explain what I want to mean by reference. the array isn't in a scope of a class. Commented Apr 22, 2016 at 8:04
  • 1
    Still, you should not be using the keyword $this (because it represent an object), you can't represent it as an array. Commented Apr 22, 2016 at 8:06
  • Yes, I agree with you. See my update and tell me if my question is now more understandable? Commented Apr 22, 2016 at 8:08
  • 1
    I think that violates the concept of an array which is for objects capability Commented Apr 22, 2016 at 8:20

5 Answers 5

9

No it's not possible that way. You can't reference to the same array within its context. But here is a work around:

$a = array(
    'base_url' => ($base_url = 'https://rh.example.com'),
    'URL_SLO_OpenAM_OIC' => 'https://openam.example.com/openam/UI/Logout?goto='.$base_url,
);
Sign up to request clarification or add additional context in comments.

Comments

2

An alternative would be to add elements to array one by one.

$a['base_url'] = 'https://rh.example.com';
$a['URL_SLO_OpenAM_OIC'] = 'https://openam.example.com/openam/UI/Logout?goto='.$a['base_url'];

Comments

1

You can not reference an array element to another element. Array dose not have such functionality. It will gives you an undefined variable error if you are doing this. Answer to your question, you can store the value to an another variable and use that variable while initializing an array.

$base_url = 'https://rh.example.com';
$a = array(
'base_url' => $base_url,
'URL_SLO_OpenAM_OIC' => 'https://openam.example.com/openam/UI/Logout?goto='.$base_url,);

Comments

1

You can't do what you want with arrays because they are only data. But you can do this with an object:

$myCustomArray = new stdClass;
$myCustomArray->base_url = 'https://rh.example.com';
$myCustomArray->URL_SLO_OpenAM_OIC = function () { echo 'https://openam.example.com/openam/UI/Logout?goto='.$this->base_url; };

and then do : $myCustomArray->URL_SLO_OpenAM_OIC();

Comments

0

An alternative approach would be to substitute values after assignment, using tokens for simple cases.

<?php

function substitutor(array $array) {
    foreach ($array as $key => $value) {
        if(preg_match('/@(\w+)@/', $value, $match)) {
            $array[$key] = str_replace($match[0], $array[$match[1]], $value);
        } 
    };

    return $array;
}

$array = array(
    'foo' => 'bar',
    'baz' => 'some' . '@foo@'
);

var_dump($array);
$substituted = substitutor($array);
var_dump($substituted);

Output:

array(2) {
  ["foo"]=>
  string(3) "bar"
  ["baz"]=>
  string(9) "some@foo@"
}
array(2) {
  ["foo"]=>
  string(3) "bar"
  ["baz"]=>
  string(7) "somebar"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.