问题
Lets say I have an array like:
$thing = Array ( [test1] => something [test2] => something [info] => yes [array] => Array ( [test1] => something else [test2] => something else [info] => maybe [array] => Array ( [test1] => something [info] => yes ) ) )
How can I write a function that goes through $thing
and adds a key to each part called 'valid' with a value of TRUE
if the value of 'info' is 'yes' and FALSE
otherwise?
I basically want the final array to look like:
$final = Array ( [test1] => something [test2] => something [info] => yes [valid] => TRUE //add this [array] => Array ( [test1] => something else [test2] => something else [info] => maybe [valid] => FALSE //add this [array] => Array ( [test1] => something [info] => yes [valid] => TRUE //add this ) ) )
I'm writing a library for codeigniter that manages a site map and provides easy ways to get the breadcrumb and navigation tree for a page but am stuck at a part where I need to do something like this.
The main question that I can't figure out is: How do I go through the array and add a key to each part? Also there can be an infinite amount of arrays in arrays.
Thanks!
回答1:
You can use this
$thing = Array
(
"test1" => "something",
"test2" => "something",
"info" => "yes",
"array" => Array
(
"test1" => "something else",
"test2" => "something else",
"info" => "maybe",
"array" => Array
(
"test1" => "something",
"info" => "yes"
)
)
);
function validPush($array)
{
foreach ($array as $key => $value)
{
if(is_array($value))
$array[$key] = validPush($value);
if($key == "info")
$array['valid'] = ($value == "yes") ? true : false ;
}
return $array;
}
var_dump(validPush($thing));
Output
array
'test1' => string 'something' (length=9)
'test2' => string 'something' (length=9)
'info' => string 'yes' (length=3)
'array' =>
array
'test1' => string 'something else' (length=14)
'test2' => string 'something else' (length=14)
'info' => string 'maybe' (length=5)
'array' =>
array
'test1' => string 'something' (length=9)
'info' => string 'yes' (length=3)
'valid' => boolean true
'valid' => boolean false
'valid' => boolean true
回答2:
This feels like you're adding redundant information. Why can't you just re-use the data in the array - i.e.
[info] => yes
回答3:
function setInfoTrueFalse($array){
if(isset($array['info']) && $array['info']=='yes'){
$array['valid']=TRUE;
}else{
$array['valid']=FALSE;
}
foreach($array as $key => $val){ # if we don't know the name of possible arrays, loop though
if(is_array($val)){
# loop one layer deeper if we have another array, could combine with the 'info' check if yes to that means this array exists and is an array
$array[$key]=setInfoTrueFalse($array[$key]);
}
}
return $array;
}
$final=setInfoTrueFalse($thing);
回答4:
why not jus add the value to the first array and consider it valid for the rest.
edit: do a recursive call, passing $array depending on how many dimensional array you got, here 3.
function add_array( $array) {
$array['info'] = 'yes';
return $arr;
}
call:
$final = add_array(add_array(add_array($final)));
来源:https://stackoverflow.com/questions/12516874/how-do-i-add-a-key-to-each-array-in-a-multidimensional-array-if-it-meets-certain