Most of the time, when there is a function named func1 and it returns an array, I use this method to access a specific index of that returned array:
$myarray=func1();
echo $myarray['AssIndex'];
Is there a way to access it in a single line? Something like this?
// the brackets are not working. this is only to make my meaning clear.
echo {func1}['AssIndex'];
As of PHP 5.4, you can do this:
echo func1()['AssIndex'];
This is called Function Array Dereferencing.
Related
I have a function asdf() that returns an array ["key" => "value"]. I would like to print out value with one line, but reset() function suggested in similar questions does not work for me because reset only takes variable as a argument and doesent accept function. So if i try reset(asdf()) i get an exception: "Only variables should be passed by reference".
So my question is how can i print "value" from asdf() in a single line only using php native functions.
Actually reset function used to move the array's internal pointer to the first element, I think you must use current function that is return the current element in an array
Try like this
print current(asdf());
Try this:
current(array_values(asdf()));
It uses current to avoid the pass by reference error and array_values to ensure the array passed to current has the first element as it's current element.
Though unless you have a very good reason assigning the array to a variable and then using reset would be better.
Could someone please explain what is going on here in this code?
I can see it is an array called b accessing an element with the key 'c', but the stuff in the brackets? I don't know what is going on here.
$a=$b['c']($d,$e,$f);
$b['c'] must be a function name.
try to print it, you'll see.
$a=$b['c']($d,$e,$f);
calls that function passing $d, $e and $f arguments to it.
Try :
<?php
$func = 'var_dump';
$foo = array(1,2,3);
$func($foo)
It looks like you are looking at a variable function.
The expression above first evaluates the associative array $a=$b['c'] and then calls the function with that name, passing the arguments $d,$e,$f.
From the description:
PHP supports the concept of variable functions. This means that if a
variable name has parentheses appended to it, PHP will look for a
function with the same name as whatever the variable evaluates to, and
will attempt to execute it. Among other things, this can be used to
implement callbacks, function tables, and so forth.
It seems element at key c is expected to be a function.
In PHP5, you can use this kind of short notations. The function will be called with parameters d e and f.
I'm calling this WordPress function:
get_user_meta($user->ID, "user_address");
And it returns array, I don't want to put this into variable but simply echo it out.
But this doesn't work:
get_user_meta($user->ID, "user_address")[0];
Why? any way to do this as one liner?
As per function reference you have to pass third argument true so that it will return you single value.
get_user_meta($user->ID, "user_address",true);
Dereferencing an array immediately when it is returned by a function is a relatively new feature in PHP 5.4. You are most likely using 5.3 or older, in which case you cannot immediately access an element of an array returned by a function call.
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.
http://docs.php.net/manual/en/language.types.array.php
If it's the first element you need:
echo array_shift(get_user_meta($user->ID, "user_address"));
I am trying to reference something inside an array i get from a function.
Lets say I have a collection named $myArrays and when i call $myArrays->first(); I will get an array.
Now when I try to get the first element in my array with $myArrays->first()[0] this doesn't work. Why is that so and is there a way to use it in a similar way?
Regards, Senad
Because you are not using PHP 5.4:
Function array dereferencing has been added, e.g. foo()[0]
You need a temporary variable:
$first = $myArrays->first();
$first[0]
And no, you cannot "trick" PHP this way either:
($myArray->first())[0]
$youtubes = array("lNT4H39G2rw","pF2_qvdm8DQ","_8ytwhhJwco","K16ZRFWR2Mc","9WuPxe7zc6Q","rXZIIclPnd0","J8ZwyN6E3_Q","OEWJbsh0z-4","o62-X0stdFM","aIIiww2Neq0","5TJc-VbNYg0","MYQa1Tgw_z8","alxzFm-bqug","UmI7oyllrlY","RGKFXDHFmn4");
function randomFromArray($data) {
global $$data;
echo $$data[rand(0,count($youtubes)-1)];
}
randomFromArray("youtubes");
I am trying to get this to work as a function, so I can enter the array name as a parameter. It is then supposed to echo a random entry from the array. The bit where it gets the random entry from array works on its own if I substitute it straight in, but I can't seem to get it working as a function.
Any help?
You're using the variable name $youtubes in your randomFromArray function in the call to count (but the variable is not available under that name there).
Btw., why don't you pass in (a reference to) the array instead of its name? Would be much tidier than using global $$data; The following code uses a reference to avoid copying the array (but remember that then, the outside array could be changed from inside the method):
$youtubes = // ...
function randomFromArray(&$data) {
echo $data[rand(0,count($data)-1)];
}
randomFromArray($youtubes);
dont call the function randomFromArray("youtubes"); in this way you call the function with youtubes parametar like a string. and inside the function itself you dont have a $youtubes variable. call the function like this randomFromArray($youtubes);
hope this would help
You are passing the string 'youtubes' into the function, not the array. You want to pass in the name of the array:
randomFromArray($youtubes);
$youtubes = array("lNT4H39G2rw","pF2_qvdm8DQ","_8ytwhhJwco","K16ZRFWR2Mc","9WuPxe7zc6Q","rXZIIclPnd0","J8ZwyN6E3_Q","OEWJbsh0z-4","o62-X0stdFM","aIIiww2Neq0","5TJc-VbNYg0","MYQa1Tgw_z8","alxzFm-bqug","UmI7oyllrlY","RGKFXDHFmn4");
function randomFromArray($data) {
echo $data[rand(0,count($data)-1)];
}
randomFromArray($youtubes);
There are some things fundamentally wrong here.
global $$data
Why is there a double $ sign? And where would this data be coming from?
echo $$data[rand(0,count($youtubes)-1)];
Your function doesn't know the variable $youtubes, since you have never defined it inside of the function. All your function knows is the $data variable that you are passing to it.
randomFromArray("youtubes"); You are passing a string to your function, rather then your array. You probably want this instead:
randomFromArray($youtubes); // Pointing to your actual array, rather then a string
Try reading up on PHP functions first before attempting to use them as you're lacking a lot of basic knowledge about them.
After you put a ; after global $$data, you can try this: $f = $$data; and then echo $f[rand(..)]; if you really want to use names instead variables as others suggested. And if you do that you can use the string (or $f) further in the function code.