The returned array of PHP functions

I need to return multiple values ​​from a function, so I added them to the array and returned the array.

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

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

+63
function arrays php
Apr 17 2018-11-11T00:
source share
13 answers

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

 function data() { $out['a'] = "abc"; $out['b'] = "def"; $out['c'] = "ghi"; return $out; } $data = data(); echo $data['a']; echo $data['b']; echo $data['c']; 
+85
May 27 '11 at 15:49
source share

You can do it:

 list($a, $b, $c) = data(); print "$a $b $c"; // "abc def ghi" 
+48
Apr 17 2018-11-11T00:
source share
 function give_array(){ $a = "abc"; $b = "def"; $c = "ghi"; return compact('a','b','c'); } $my_array = give_array(); 

http://php.net/manual/en/function.compact.php

+20
Nov 09 '12 at 19:29
source share

The data function returns an array, so you can access the result of the function in the same way as if you normally accessed the elements of the array:

 <?php ... $result = data(); $a = $result[0]; $b = $result[1]; $c = $result[2]; 

Or you can use the list() function, as @fredrik recommends, to do the same in a line.

+13
Apr 17 2018-11-11T00:
source share
 $array = data(); print_r($array); 
+6
Apr 17 2018-11-11T00:
source share

From PHP 5.4, you can take advantage of dereferencing arrays and do something like this:

 <? function data() { $retr_arr["a"] = "abc"; $retr_arr["b"] = "def"; $retr_arr["c"] = "ghi"; return $retr_arr; } $a = data()["a"]; //$a = "abc" $b = data()["b"]; //$b = "def" $c = data()["c"]; //$c = "ghi" ?> 
+5
Feb 27 '16 at 13:23
source share
 <?php function demo($val,$val1){ return $arr=array("value"=>$val,"value1"=>$val1); } $arr_rec=demo(25,30); echo $arr_rec["value"]; echo $arr_rec["value1"]; ?> 
+4
Jun 15 '17 at 15:46
source share

here is the best way in a similar function

  function cart_stats($cart_id){ $sql = "select sum(price) sum_bids, count(*) total_bids from carts_bids where cart_id = '$cart_id'"; $rs = mysql_query($sql); $row = mysql_fetch_object($rs); $total_bids = $row->total_bids; $sum_bids = $row->sum_bids; $avarage = $sum_bids/$total_bids; $array["total_bids"] = "$total_bids"; $array["avarage"] = " $avarage"; return $array; } 

and you get array data like this

 $data = cart_stats($_GET['id']); <?=$data['total_bids']?> 
+2
Feb 27 2018-12-12T00:
source share

This is what I did inside yii framewok:

 public function servicesQuery($section){ $data = Yii::app()->db->createCommand() ->select('*') ->from('services') ->where("section='$section'") ->queryAll(); return $data; } 

then inside my view file:

  <?php $consultation = $this->servicesQuery("consultation"); ?> ?> <?php foreach($consultation as $consul): ?> <span class="text-1"><?php echo $consul['content']; ?></span> <?php endforeach;?> 

What I am doing is capturing the part of the cretin table that I selected. should work only for php minus the path "Yii" for db

+1
Nov 02 '12 at 19:25
source share

The main problem is accessing the data in the array, as Felix Kling points out in the first answer.

In the following code, I got access to array values ​​with print and echo constructs.

 function data() { $a = "abc"; $b = "def"; $c = "ghi"; $array = array($a, $b, $c); print_r($array);//outputs the key/value pair echo "<br>"; echo $array[0].$array[1].$array[2];//outputs a concatenation of the values } data(); 
+1
Nov 13 '14 at 18:09
source share

To get the values ​​of each variable, you need to treat the function as an array:

 function data() { $a = "abc"; $b = "def"; $c = "ghi"; return array($a, $b, $c); } // Assign a variable to the array; // I selected $dataArray (could be any name). $dataArray = data(); list($a, $b, $c) = $dataArray; echo $a . " ". $b . " " . $c; //if you just need 1 variable out of 3; list(, $b, ) = $dataArray; echo $b; 
+1
Oct 10 '18 at 22:29
source share

I think the best way to do this is to create a global var array. Then do whatever you want inside the function data, passing it as a link. No need to return anything.

 $array = array("white", "black", "yellow"); echo $array[0]; //this echo white data($array); function data(&$passArray){ //<<notice & $passArray[0] = "orange"; } echo $array[0]; //this now echo orange 
0
Oct 02
source share

Maybe this is what you were looking for:

 function data() { // your code return $array; } $var = data(); foreach($var as $value) { echo $value; } 
0
Jan 12 '19 at 2:42
source share



All Articles