Rectangle 27 1

PHP function return array?


function data() {
    $out['a'] = "abc";
    $out['b'] = "def";
    $out['c'] = "ghi";
    return $out;
}

$data = data();
echo $data['a'];
echo $data['b'];
echo $data['c'];

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

Note
Rectangle 27 1

PHP function return array?


list($a, $b, $c) = data();

print "$a $b $c"; // "abc def ghi"

@GiacomoTecyaPigani list is not a function it's a language construct as stated in the docs.

Note
Rectangle 27 1

PHP function return array?


extract($my_array)
function give_array(){

    $a = "abc";
    $b = "def";
    $c = "ghi";

    return compact('a','b','c');
}


$my_array = give_array();

This is cool... but not very commonly used in my experience.

Note
Rectangle 27 0

PHP function return array?


<?

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"
?>

Caution: you may not want to evaluate the function 3 times !

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

Note
Rectangle 27 0

PHP function return array?


<?php
...
$result = data();

$a = $result[0];
$b = $result[1];
$c = $result[2];

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

The data function is returning an array, so you can access the result of the function in the same way as you would normally access elements of an array:

Note
Rectangle 27 0

PHP function return array?


<?php $consultation = $this->servicesQuery("consultation"); ?> ?>
      <?php foreach($consultation as $consul): ?>
             <span class="text-1"><?php echo $consul['content']; ?></span>
       <?php endforeach;?>
public function servicesQuery($section){
        $data = Yii::app()->db->createCommand()
                ->select('*')
                ->from('services')
                ->where("section='$section'")
                ->queryAll();   
        return $data;
    }

This is what I did inside the yii framewok:

What I am doing grabbing a cretin part of the table i have selected. should work for just php minus the "Yii" way for the db

Note
Rectangle 27 0

PHP function return array?


$array  = data();

print_r($array);
Note
Rectangle 27 0

PHP function return array?


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;
}
$data = cart_stats($_GET['id']); 
<?=$data['total_bids']?>

and you get the array data like this

here is the best way in a similar function

Note
Rectangle 27 0

PHP function return array?


<?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"];
?>
Note