PHP - 访问多维数组的数据
我想访问多维数组中的数据。 我需要获取这些数据的值(EC000001, EG000017, EN, EF007220)
,但我在PHP中处于foreach循环的困境,特别是当需要嵌套的foreach时能否请你帮助我,给我解决方案来访问想要的数据?PHP - 访问多维数组的数据
我需要遍历所有的$ c,然后循环内部以获取所有需要的数据。这是我之前收集EC000001值的方法,但我相信有更好的解决方案。阵列的
foreach ($c as $classCodes => $value) { $classCode = key($c[$classCodes]); //classCode -> EC000001
}
结构:
Array (
**[EC000001]** => Array
(
[0] => **EG000017**
[1] => Array
(
[0] => Array
(
[0] => **EN**
[1] => Busbar terminal
)
[1] => Array
(
[0] => **nl-NL**
[1] => Aansluitklem stroomrail
)
)
[2] => Array
(
[0] => Array
(
[0] => **EF007220**
[1] => EU570448
[2] => Array
(
)
)
[1] => Array
(
[0] => EF007219
[1] => EU570448
[2] => Array
(
)
)
[2] => Array
(
[0] => EF000073
[1] =>
[2] => Array
(
[0] => EV009241
[1] => EV009472
)
)
[3] => Array
(
[0] => EF007092
[1] => EU570448
)
[4] => Array
(
[0] => EF004969
[1] => EU570126
)
)
)
)
回答:
我无法测试它,但你可以用这个尝试的循环中:
$value[0]; // -> 1 $value[1][0][0]; // -> 2
$value[1][1][1]; // -> 3
回答:
$EG000002Array[0][EG000001][0] $EG000002Array[0][EG000001][1][0]
$EG000002Array[0][EG000001][1][1][1]
回答:
有多种途径获得来自多维阵列的阵列值
例如使用的foreach():
$flavors = array('Japanese' => array('hot' => 'wasabi', 'salty' => 'soy sauce'),
'Chinese' => array('hot' => 'mustard',
'pepper-salty' => 'prickly ash'));
// $culture is the key and $culture_flavors is the value (an array)
foreach ($flavors as $culture => $culture_flavors) {
// $flavor is the key and $example is the value
foreach ($culture_flavors as $flavor => $example) {
print "A $culture $flavor flavor is $example.\n";
}
}
或使用用于():
$specials = array(array('Chestnut Bun', 'Walnut Bun', 'Peanut Bun'), array('Chestnut Salad','Walnut Salad', 'Peanut Salad'));
// $num_specials is 2: the number of elements in the first dimension of $specials
for ($i = 0, $num_specials = count($specials); $i < $num_specials; $i++) {
// $num_sub is 3: the number of elements in each sub-array
for ($m = 0, $num_sub = count($specials[$i]); $m < $num_sub; $m++) {
print "Element [$i][$m] is " . $specials[$i][$m] . "\n";
}
}
输出应该是这样的:
Element [0][0] is Chestnut Bun Element [0][1] is Walnut Bun
Element [0][2] is Peanut Bun
Element [1][0] is Chestnut Salad
Element [1][1] is Walnut Salad
Element [1][2] is Peanut Salad
回答:
可以使用递归和正则表达式检查它是否粗体:)
通知我们把
&
到$classCodes
通过参考通过它和值没有 。
功能:
function get_bolded_data($c, &$classCodes = array()){ foreach($c as $k1 => $v1){
if(is_array($v1)){
//If $v1 is an array we call get_bolded_data() again and pass
//$v1 and $classCode
get_bolded_data($v1,$classCodes);
}else if(preg_match("/(\*\*).*(\*\*)/", $v1)){
$classCodes[] = $v1;
}
}
}
用法:
$classCodes = array(); $c = array(
0 => array(
'**EC000001**' => array(
0 => '**EG000017**',
1 => array(
0 => array(
0 => '**EN**',
1 => 'Busbar terminal'
) ,
1 => array(
0 => '**nl-NL**',
1 => 'Aansluitklem stroomrail'
)
) ,
2 => array(
0 => array(
0 => '**EF007220**',
1 => 'EU570448',
2 => array()
) ,
1 => array(
0 => 'EF007219',
1 => 'EU570448',
2 => array()
) ,
2 => array(
0 => 'EF000073',
1 => '',
2 => array(
0 => 'EV009241',
1 => 'EV009472'
)
) ,
3 => array(
0 => 'EF007092',
1 => 'EU570448'
) ,
4 => array(
0 => 'EF004969',
1 => 'EU570126'
)
)
)
)
);
//Call our function
get_bolded_data($c, $classCodes);
下面是从的var_dump结果:
array(4) { [0]=>
string(12) "**EG000017**"
[1]=>
string(6) "**EN**"
[2]=>
string(9) "**nl-NL**"
[3]=>
string(12) "**EF007220**"
}
以上是 PHP - 访问多维数组的数据 的全部内容, 来源链接: utcz.com/qa/265917.html