Dereferencing an array.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi.

Based upon ...

<?php
$name = Null;
$age = Null;
$boundParams = array('name' => &$name, 'age' => &$age);
$records = array();

$name = 'Richard';
$age  = 43;
$records[] = $boundParams;

$name = 'Sally';
$age  = 37;
$records[] = $boundParams;

print_r($records);
?>

outputs Sally twice.

Whilst that is the correct output based upon the code, it is undesired.

I want the boundParams to have the references (the actual data from my
mysqli_stmt::fetch() with bound results), but I want to be able to
copy the values and not maintain the references.


The best I've come up with is ...

<?php
$name = Null;
$age = Null;
$boundParams = array('name' => &$name, 'age' => &$age);
$records = array();


$columns = array_keys($boundParams);

$name = 'Richard';
$age  = 43;
//$records[] = $boundParams;
$records[] = array_combine($columns,
array_map(function($m_Value){return $m_Value;}, $boundParams));

$name = 'Sally';
$age  = 37;
//$records[] = $boundParams;
$records[] = array_combine($columns,
array_map(function($m_Value){return $m_Value;}, $boundParams));

print_r($records);
?>

Is there a more efficient way?
-- 
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux