如何在多维数组中的每个数组添加一个键,如果它符合在PHP中的某些标准? [英] How do I add a key to each array in a multidimensional array if it meets certain criteria in php?
问题描述
$ thing = Array
(
[test1] = > something
[test2] =>某事
[info] =>是
[array] =>数组
(
[test1] =>别的
$ [$] $>
[数组] $>
[数组] $>
[数组] $>
[数组] ] =>是
)
)
)
如何写一个函数,通过 $ thing
,并添加一个键到每个有效的部分,值为 TRUE
,如果'info'的值是'yes'和 FALSE
否则?
我基本上希望最终数组看起来像:
$ final = Array
(
[test1] => something
[test2] = > something
[info] =>是
[valid] => TRUE //添加这个$数组
[test1] =>其他
[test2] =>其他
[info] =>也许
[valid] => FALSE // add this
[array] => Array
(
[test1] => something
[info] =>是
[有效] => TRUE //添加此
)
)
)
我正在为codeigniter编写一个库,它管理一个站点地图,并提供简单的方法来获取页面的导航树和导航树,但是却被困在需要做这种事情的地方。
我无法弄清的主要问题是:
如何通过数组并为每个部分添加一个键?也可以在数组中有无数个数组。
谢谢!
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' test1=>something,
test2=>something,
info=>yes,
array=> Array
$ btest1=>别的东西,
test2=>别的东西,
info=>也许,
array=> Array
(
test1=>something,
info=>yes
)
)
);
函数validPush($ array)
{
foreach($ array as $ key => $ value)
{
if(is_array($ value))
$ array [$ key] = validPush($ value);
if($ key ==info)
$ array ['valid'] =($ value ==yes)?真假 ;
}
return $ array;
}
var_dump(validPush($ thing));
输出
数组
'test1'=>字符串'东西'(长度= 9)
'test2'=>字符串'东西'(长度= 9)
'info'=>字符串'是'(长度= 3)
'数组'=>
数组
'test1'=>字符串'别的东西'(长度= 14)
'test2'=>字符串别的东西(长度= 14)
'info'=>字符串'maybe'(长度= 5)
'array'=>
数组
'test1'=>字符串'东西'(长度= 9)
'info'=>字符串'是'(长度= 3)
'有效'=>布尔值true
'valid'=> boolean false
'valid'=>布尔值true
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!
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
这篇关于如何在多维数组中的每个数组添加一个键,如果它符合在PHP中的某些标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!