免责声明:所以,这可能是一个重复,但我已经花了一个小时的大部分时间来解决这个问题,我对这不应该如此困难感到沮丧。所以(PHP操作多维数组值)看起来和我想要做的事情很相似,但是还不太清楚决议是什么。
我有一个通过SQL查询检索的多维值数组。我遍历主数组来访问子数组,然后遍历每个子数组来访问密钥对值。当遇到子数组中的某个键时,我对该值执行按位操作,并将一个值赋值给一个变量,供以后使用。我的问题在于如何将这些新值分配回当前子数组的键。这是我的密码:
public function getPublicList()
{
$this->sql = "SELECT id, name, difficulty, conditions, details FROM {$this->table} WHERE enabled='y'";
$this->execSql("Get Legs List", SELECT_MULTI);
$conditions = '';
$val = '';
// break down this data's array into pieces
foreach($this->data as $key => $value)
{
// iterate through the subarrays and find conditions
foreach($value as $key => $val)
{
// var_dump($key . " => " . print_r($val, true));
if($key === 'conditions')
{
// run bitwise operations to determine what conditions are set
if($val & 1)
$conditions .= "no-shade,";
if($val & 2)
$conditions .= "quiet,";
if($val & 4)
$conditions .= "elevation,";
if($val & 8)
$conditions .= "gravel";
$val = rtrim($conditions, ",");
$conditions = '';
}
$value['conditions'] = $val;
}
}
}在我的各种尝试中,我设法将值返回到子数组的各个部分,但不是在我需要它们的地方。我需要为$conditions中的每个数组分配$value['conditions']。$value['conditions'] = $val;是我实现所需的最新尝试,但是子数组的其他值最终也被分配给$val。
我对这个很困惑,我认为这是我在搜索中找不到答案的原因。如果你能告诉我一个答案,或者是解释如何实现这一目标的文档,我会非常感激的。蒂娅!
更新:
这是var_dump(print_r($value));的结果
Array (
[id] => 4
[name] => Leg 1
[difficulty] => vh
[conditions] => 4
[details] => And you're off! Exchange 1 is where you begin the relay, starting at Timberline Lodge on Mt. Hood, and proceeding for 5.44 miles down some steep, windy terrain. Make sure you to have your required safety gear, and be mindful of other runners. This leg will test what you're made of!
)
bool(true)
Array (
[id] => 4
[name] => Leg 1
[difficulty] => vh
[conditions] => Leg 1
[details] => And you're off! Exchange 1 is where you begin the relay, starting at Timberline Lodge on Mt. Hood, and proceeding for 5.44 miles down some steep, windy terrain. Make sure you to have your required safety gear, and be mindful of other runners. This leg will test what you're made of!
)
bool(true)
Array (
[id] => 4
[name] => Leg 1
[difficulty] => vh
[conditions] => vh
[details] => And you're off! Exchange 1 is where you begin the relay, starting at Timberline Lodge on Mt. Hood, and proceeding for 5.44 miles down some steep, windy terrain. Make sure you to have your required safety gear, and be mindful of other runners. This leg will test what you're made of!
)
bool(true)
Array (
[id] => 4
[name] => Leg 1
[difficulty] => vh
[conditions] => no-shade,quiet,elevation,gravel
[details] => And you're off! Exchange 1 is where you begin the relay, starting at Timberline Lodge on Mt. Hood, and proceeding for 5.44 miles down some steep, windy terrain. Make sure you to have your required safety gear, and be mindful of other runners. This leg will test what you're made of!
) bool(true)
Array (
[id] => 4
[name] => Leg 1
[difficulty] => vh
[conditions] => And you're off! Exchange 1 is where you begin the relay, starting at Timberline Lodge on Mt. Hood, and proceeding for 5.44 miles down some steep, windy terrain. Make sure you to have your required safety gear, and be mindful of other runners. This leg will test what you're made of!
[details] => And you're off! Exchange 1 is where you begin the relay, starting at Timberline Lodge on Mt. Hood, and proceeding for 5.44 miles down some steep, windy terrain. Make sure you to have your required safety gear, and be mindful of other runners. This leg will test what you're made of! ) bool(true)
Array (
[id] => 5
[name] => Leg 2
[difficulty] => h
[conditions] => 5 [details] =>
Placeholder Content
).... for thirty something more "legs"发布于 2017-01-11 01:03:59
你的循环看起来比需要的要复杂一些。我觉得这应该能行
public function getPublicList($data)
{
$this->sql = "SELECT id, name, difficulty, conditions, details FROM {$this->table} WHERE enabled='y'";
$this->execSql("Get Legs List", SELECT_MULTI);
// just loop through the data, if it has a "conditions" key, adjust it
foreach($this->data as $key => $value)
{
if (array_key_exists("conditions", $value) {
$this->data[$key]["conditions"] = $this->do_condition_adjustment($value);
}
}
}
// separate function to apply the flags to a single conditions value
private function do_condition_adjustment($value) {
$conds = array();
if ($value & 1) {
$conds[] = "no-shade";
}
if ($value & 2) {
$conds[] = "quiet";
}
if ($value & 4) {
$conds[] = "elevation";
}
if ($value & 8) {
$conds[] = "gravel";
}
return implode(",", $conds);
}编辑:删除未使用的vars,修复错误键入的键
https://stackoverflow.com/questions/41580883
复制相似问题