Reputation: 54
I want to call array variable inside the current array. Something like this:
$conv= array (
"hello" => array("Hello", "Hi", "Hello, how are you?"),
"helo" => array_merge($conv['hello'])
);
print_r($conv['helo']);
How can I do this. thanks.
Upvotes: 1
Views: 175
Reputation: 3495
If you want to define your function directly when you create the array, then you need to declare helo
value as anonymous function, refer current array as global variable and return something you need
$conv = array(
"hello" => array("Hello", "Hi", "Hello, how are you?"),
"helo" => function() {
global $conv;
return array_merge($conv['hello'], array("Bye", "See you"));
}
);
print_r($conv['helo']());
Note that when you need the value returned from that function you need to call it as a function like above $conv['helo']()
while calling just $conv['helo']
will return a Closure
object.
UPDATE
According to what you're asking in comments, it seems that you just want to get a copy of hello
value through a function
$conv = array(
"hello" => array("Hello", "Hi", "Hello, how are you?"),
"helo" => function() {
global $conv;
return $conv["hello"];
}
);
print_r($conv['helo']());
Upvotes: 2
Reputation: 23958
The way you try to build the array is probably impossible.
Because the array is not made untill the end of the function ;
when you try to set 'helo', 'hello' is undefined.
You need to set 'hello' first then copy it to 'helo'.
$conv = array("hello" => array("Hello", "Hi", "Hello, how are you?"));
$conv["helo"] = $conv['hello'];
print_r($conv['helo']);
Upvotes: 0