使用逐位运算符操作多维数组



免责声明:所以,这可能是重复的,但我花了更好的钱花了一个小时的时间试图弄清楚这一点,但我对如何这应该没那么难。此SO(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;
}
}
}

在我的各种尝试中,我成功地将值返回到子数组的各个部分,但没有返回到我需要它们的地方。对于$this->data中的每个阵列,我需要将$conditions分配给$value['conditions']$value['conditions'] = $val;是我实现所需内容的最新尝试,但子数组的其他值最终也被分配了$val

我被这个问题难住了,我认为这是我在搜索中找不到答案的原因。如果你能给我一个答案,或者向医生解释如何实现这一点,我将不胜感激。TIA!

更新:

以下是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"

您的循环看起来比需要的要复杂一些

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);
}

编辑:删除了未使用的变量,修复了键入错误的密钥

来自foreach上的PHP文档:

为了能够直接修改循环中$value前面带有&的数组元素;。在这种情况下,该值将通过引用进行分配1

因此,在迭代器中的值前面加上一个与号:

foreach($this->data as $dataKey => &$value)    {
// iterate through the subarrays and find conditions
foreach($value as $key => &$val) {
//update $val accordingly
//e.g. $val .= 'quiet';

否则,您将需要直接更新引用数组(即$this->data):

foreach($this->data as $dataKey => $value)    {
// iterate through the subarrays and find conditions
foreach($value as $key => $val) {
//update $val accordingly
//e.g. $this->data[$dataKey][$key] .= 'quiet';

最新更新