我如何使用数组引用数组里面PHP? [英] How can I use array-references inside arrays in PHP?

查看:107
本文介绍了我如何使用数组引用数组里面PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够做到以下几点:

I want to be able to do the following:

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );

// Here I want to access the $normal_array reference **as a reference**,
// but that doesn't work obviously. How to do it?
end( $array_of_arrayrefs )["one"] = 1; // choking on this one

print $normal_array["one"]; // should output 1

问候

/ R

推荐答案

端()不返回的最后一个值的参考,而是最后的值本身。这里是一个解决方法:

end() doesn't return a reference of the last value, but rather the last value itself. Here is a workaround:

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );

$refArray = &end_byref( $array_of_arrayrefs );
$refArray["one"] = 1;

print $normal_array["one"]; // should output 1

function &end_byref( &$array ) {
    $lastKey = end(array_keys($array));
    end($array);
    return $array[$lastKey];
}

这篇关于我如何使用数组引用数组里面PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆