DISCLAIMER: So, this is a probably duplicate but I've spent the better part of an hour trying to figure this out, and I'm frustrated with how this shouldn't be so difficult. This SO (PHP manipulating multidimensional array values) looks like it's similar to what I'm trying to do,but it's not quite clear what the resolution was.
I have a multidimenionsal array of values retrieved via an SQL query. I iterate over the main array to access the subarrays, which I then iterate over each subarray to access the key pair values. When a certain key in a subarray is encountered, I perform a bitwise operation on that value, and assign a value to a variable for later use. My issue lies in getting these new values assigned back to the key of the present subarray. Here's my code:
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;
}
}
}
In my various attempts, I have managed to get the values back into various parts of the subarrays, but not where I need them. I need $conditions to be assigned to $value['conditions'] for each array in $this->data. $value['conditions'] = $val; is my latest attempt at achieving what I need, but other values of the subarray wind up being assigned $val as well.
I'm pretty stumped with this one, and I think that's contributing to my lack of finding an answer in searches. If you can point me towards an answer, or towards docs that will explain how to achieve this, I will greatly appreciate it. TIA!
UPDATE:
Here's the result of 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"
$valinstead of$value?