I would like to search key in multidimensional array and i would like to get corrosponding value associated with that key. For e.g. I would like to extract following texts from below array :
我想在多维数组中搜索key我想要得到与那个key相关的腐蚀值。例如,我想从下面的数组中提取以下文本:
SENT AT 12.08ms
And the text
和文本
sample id 41962
following is an array print_r() output :
下面是一个数组print_r()输出:
Array
(
[0] => Array
(
[VERSION] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
)
[value] => Array
(
[0] => Array
(
[0] => 3.0
)
)
)
)
[SAMPLE] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
)
[value] => Array
(
[0] => Array
(
[0] => sample id 41962
)
)
)
)
[TSAM] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
)
[value] => Array
(
[0] => Array
(
[0] => sample group 141
)
[1] => Array
(
[0] => ¯
)
[2] => Array
(
[0] => sample batch 81
)
[3] => Array
(
[0] =>
)
[4] => Array
(
[0] =>
)
)
)
)
[STATUS] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
[TYPE] => Array
(
[0] => CART
)
)
[value] => Array
(
[0] => Array
(
[0] => SENT AT 12.08ms
)
)
)
)
)
)
Can somebody provide me optimized code for above problem. The multidimensional array contains more than 5000 to 10000 arrays.
能不能给我提供以上问题的优化代码。多维数组包含5000到10000个数组。
3 个解决方案
#1
2
If all the array keys have the same structure the following code should work:
如果所有的数组键都具有相同的结构,下面的代码应该可以工作:
foreach($array as $item){
$sentat = $item['STATUS'][0]['value'][0][0];
$sample = $item['SAMPLE'][0]['value'][0][0];
}
More detailed information would help us to provide you more tips :)
更详细的信息将帮助我们提供更多的提示:)
#2
3
Please, see if my function works for you:
请看看我的功能是否适合您:
function get_value_by_key($array,$key)
{
foreach($array as $k=>$each)
{
if($k==$key)
{
return $each;
}
if(is_array($each))
{
if($return = get_value_by_key($each,$key))
{
return $return;
}
}
}
}
Use:
使用:
$array = array('array1'=>array('array2'=>array('find_some_key'=>'some_value')));
echo get_value_by_key($array,'find_some_key'); // outputs: some_value
#3
0
I had the same problem, so I developed this function to solve
我有同样的问题,所以我开发了这个函数来求解。
private function array_key_search($value, $key) {
$result = false;
if (is_array($value)) {
foreach ($value as $k => $v) {
$result = $k === $key ? $v : $this->array_key_search($v, $key);
if ($result) {
break;
}
}
}
return $result;
}
This function loops through all the keys of an array and retrieves the first occurrence with the name you assign the variable $key
此函数循环遍历数组的所有键,并使用变量$key分配的名称检索第一次出现
#1
2
If all the array keys have the same structure the following code should work:
如果所有的数组键都具有相同的结构,下面的代码应该可以工作:
foreach($array as $item){
$sentat = $item['STATUS'][0]['value'][0][0];
$sample = $item['SAMPLE'][0]['value'][0][0];
}
More detailed information would help us to provide you more tips :)
更详细的信息将帮助我们提供更多的提示:)
#2
3
Please, see if my function works for you:
请看看我的功能是否适合您:
function get_value_by_key($array,$key)
{
foreach($array as $k=>$each)
{
if($k==$key)
{
return $each;
}
if(is_array($each))
{
if($return = get_value_by_key($each,$key))
{
return $return;
}
}
}
}
Use:
使用:
$array = array('array1'=>array('array2'=>array('find_some_key'=>'some_value')));
echo get_value_by_key($array,'find_some_key'); // outputs: some_value
#3
0
I had the same problem, so I developed this function to solve
我有同样的问题,所以我开发了这个函数来求解。
private function array_key_search($value, $key) {
$result = false;
if (is_array($value)) {
foreach ($value as $k => $v) {
$result = $k === $key ? $v : $this->array_key_search($v, $key);
if ($result) {
break;
}
}
}
return $result;
}
This function loops through all the keys of an array and retrieves the first occurrence with the name you assign the variable $key
此函数循环遍历数组的所有键,并使用变量$key分配的名称检索第一次出现