我正在运行这个DB调用来获取多维数组我试图获取每个键的键但是当我尝试它时出现空白或作为数组.
$root_array = array();
$sites = $this->sites($member_id);
foreach ($sites as $site){
$records = $this->db->select('p.name as place_name, p.id as place_id,p.active as place_status')
->from('places p')
->join('members_permissions pm','pm.sites_id = p.sites_id and pm.members_id ='.$member_id)
->where('p.active', 0)
->get();
$places = $records->result_array();
$places['name'] = $site['name'];
foreach($places as $place){
$root_array[$site['name']][] = $place;
}
}
return $root_array;
Run Code Online (Sandbox Code Playgroud)
这是我的php循环:
<?php foreach($places as $site): ?>
<h5><?=key($site)?></h5>
<?php foreach($site as $place): ?>
<h6><?=$place['place_name']?></h6>
<?php endforeach?>
<?php endforeach ?>
Run Code Online (Sandbox Code Playgroud)
此外,当我运行一个只吐出数组的测试时,这就是结果,我想渲染的是[费城]
[Philadelphia] => Array
(
[0] => Array
(
[place_name] => XYX …Run Code Online (Sandbox Code Playgroud) print_r($ samplearr)为包含3个项目的数组打印以下内容:
Array ( [4722] => Array ( [value1] => 52 [value2] => 46 )
Array ( [4922] => Array ( [value1] => 22 [value2] => 47 )
Array ( [7522] => Array ( [value1] => 47 [value2] => 85 )
Run Code Online (Sandbox Code Playgroud)
我想把这些放到一个HTML表中,所以我正在做一个foreach,但它没有做我所期望的:
foreach($samplearr as $item){
print "<tr><td>" . key($item) . "</td><td>" . $samplearr['value1'] . "</td><td>" . $samplearr['value2'] . "</td></tr>";
}
Run Code Online (Sandbox Code Playgroud)
哪个回来了:
<tr><td>value1</td><td>52</td><td>46</td></tr>
Run Code Online (Sandbox Code Playgroud)
这将是我想要的第一个输出:
<tr><td>4722</td><td>52</td><td>46</td></tr>
Run Code Online (Sandbox Code Playgroud)
我需要使用什么功能代替键($ item)才能获得4722?
在php中,可以使用关联数组处理状态名称列表及其缩写,如下所示:
<?php
$stateArray = array(
"ALABAMA"=>"AL",
"ALASKA"=>"AK",
// etc...
"WYOMING"=>"WY"
);
foreach ($stateArray as $stateName => $stateAbbreviation){
print "The abbreviation for $stateName is $stateAbbreviation.\n\n";
}
?>
Run Code Online (Sandbox Code Playgroud)
输出(保留密钥顺序):
The abbreviation for ALABAMA is AL.
The abbreviation for ALASKA is AK.
The abbreviation for WYOMING is WY.
Run Code Online (Sandbox Code Playgroud)
编辑:请注意,数组元素的顺序保留在php版本的输出中.使用HashMap的Java实现不保证元素的顺序.Python中的字典也不是.
这是如何在java和python中完成的?我只找到提供价值的方法,给定密钥,如python:
stateDict = {
"ALASKA": "AK",
"WYOMING": "WY",
}
for key in stateDict:
value = stateDict[key]
Run Code Online (Sandbox Code Playgroud)
编辑:基于答案,这是我在python中的解决方案,
# a list of two-tuples
stateList = [
('ALABAMA', 'AL'),
('ALASKA', 'AK'),
('WISCONSIN', 'WI'),
('WYOMING', 'WY'),
] …Run Code Online (Sandbox Code Playgroud) 我需要遍历一个动态数组.该数组看起来如下所示:
Array
(
[2010091907] => Array
(
[home] => Array
(
[score] => Array
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
[away] => Array
(
[score] => Array
(
[1] => 0
[2] => 7
[3] => 0
[4] => 0
[5] => 0
[T] => 7
)
[abbr] => ARZ
[to] => 2
)
[weather] =>
[media] => Array …Run Code Online (Sandbox Code Playgroud) 以下php片段将显示关联数组$ ar中每个元素的键和值.但是,不能以更优雅的方式完成,这样键和值指针就可以通过相同的机制来提升吗?我仍然希望它在foreach循环中执行(或者其他一些不依赖于元素数量的知识).
<?php
$ar = array("A" => "one", "B" => "two", "C" => "three");
foreach($ar as $a){
echo "key: ".key($ar)." value: ".$a."\n";
$ignore = next($ar);//separate key advancement - undesirable
}
?>
Run Code Online (Sandbox Code Playgroud) 我用
foreach($terms as $one)
{
print $one."<br>";
}
Run Code Online (Sandbox Code Playgroud)
但这只会打印出值.钥匙怎么样?