Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the elements of a function's return array?

I need to return multiple values from a function, therefore I have added them to an array and returned the array.

<? function data(){     $a = "abc";     $b = "def";     $c = "ghi";      return array($a, $b, $c); } ?> 

How can I receive the values of $a, $b, $c by calling the above function?

like image 856
Ajay Avatar asked Apr 17 '11 08:04

Ajay


People also ask

How do you access elements in an array of arrays?

Access Array Elements You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

How do you access the elements of an array?

Array elements are accessed by using an integer index. Array index starts with 0 and goes till the size of the array minus 1. The name of the array is also a pointer to the first element of the array.

How do you retrieve the number of elements in an array A?

Just divide the number of allocated bytes by the number of bytes of the array's data type using sizeof() . int numArrElements = sizeof(myArray) / sizeof(int);

How do you return an array element in C++?

Return Array from Functions in C++ C++ does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

How to access array elements in C++?

Different ways of accessing array elements in C++ 1 Using pointer 2 (arr+1) 3 Using a little manipulation i [arr] for using arrays and the reason behind that. More ...

How to return an array from a function in C++?

3. Using std::array For std::array in C++, returning the array name from a function actually translates into the the whole array being returned to the site of the function call. Hence it is clear from the output, that the array return by the function func () was successful.

What is the return type of array in keypoint?

Keypoint 1: Method returning the array must have the return type as an array of the same data type as that of the array being returned. The return type may be the usual Integer, Double, Character, String, or user-defined class objects as well.

What is an array in programming?

An array is a data structure that consists of a group of elements of the same data type such that each element of the array can be identified by a single array index or key.


1 Answers

You can add array keys to your return values and then use these keys to print the array values, as shown here:

function data() {     $out['a'] = "abc";     $out['b'] = "def";     $out['c'] = "ghi";     return $out; }  $data = data(); echo $data['a']; echo $data['b']; echo $data['c']; 
like image 101
Kristoffer Bohmann Avatar answered Sep 24 '22 10:09

Kristoffer Bohmann