任何人都可以帮助我了解多维数组吗?我有想随机阵列里面的阵列,因此,因此它的层次结构或存在的索引值将被从阵列的原始布置改变和不同的,如:随机php多维数组并获取原始数组索引
这是原始阵列
Array(
[0]=>Array(
[title] => 'Title 1'
[description] => 'description here'
)
[1]=>Array(
[title] => 'Title 2'
[description] => 'another description here'
)
[2]=>Array(
[title] => 'Title Here Again'
[description] => 'description here again'
)
)
这将是上述阵列的原始结构,如果你随便吧,让我们说,这将是
这是随机编列结果
Array(
[0]=>Array(
[title] => 'Title 2'
[description] => 'another description here'
)
[1]=>Array(
[title] => 'Title 3'
[description] => 'another description again'
)
[2]=>Array(
[title] => 'Title 1'
[description] => 'description here'
)
)
正如你所看到的,数组内部的值在不同位置被随机化,现在的问题是我无法获得如何从随机数组中获得像这样的原始数组索引的确切逻辑。就像'标题1'的值一样,它的原始索引是[0],并且在它被随机化后它变成了[2],但我仍然希望'标题1'被分配给索引[0]。下面是我如何随机阵列短的PHP代码:
foreach (shuffleThis($rss->getItems()) as $item) {
foreach($item as $key=>$value){
if($key=='title'){
$title=$value;
}
if($key=='description'){
$description=$value;
}
}
}
function shuffleThis($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key) {
$random[] = $list[$key];
}
return $random;
}
只想得到他们被随机编key
的原始数组索引。
谢谢!
哇!这是一个很好的答案,当我尝试它时,我可以看到元素正在改变位置,但不是数组键。但我怎么能得到像这样[0]的关键索引号,里面的零? – 2012-04-27 08:06:11
不确定你在问什么。如何获得哪个密钥,并给出什么信息? – Jon 2012-04-27 08:15:00