Php 检索嵌套数组键
我快疯了。 假设我得到了这个数组:Php 检索嵌套数组键,php,arrays,wordpress,woocommerce,Php,Arrays,Wordpress,Woocommerce,我快疯了。 假设我得到了这个数组: Array ( [0] => Array ( [variation_name] => variation_1 [license_type] => Array ( [slug] => license_x [price] => price_x [dimensions] => dimensions_x ) ) [1] => Arra
Array
(
[0] => Array
(
[variation_name] => variation_1
[license_type] => Array
(
[slug] => license_x
[price] => price_x
[dimensions] => dimensions_x
)
)
[1] => Array
(
[variation_name] => variation_2
[license_type] => Array
(
[slug] => license_y
[price] => price_y
[dimensions] => dimensions_y
)
)
[2] => Array
(
[variation_name] => variation_3
[license_type] => Array
(
[slug] => license_solid_z
[price] => price_z
[dimensions] => dimensions_z
)
)
)
我想回显以“license_solid”开头的数组值以及包含它的数组的值。
要获得“许可证\实体”条目,我运行以下命令:
$attribute_pa_licenses = array_column($array, 'license_type');
$attribute_pa_license_slug = array_column($attribute_pa_licenses, 'slug');
foreach ($attribute_pa_license_slug as $value) {
if (0 === strpos($value, 'license_solid')) {
echo $value;
}
}
它确实可以工作,但我不知道如何同时回显“包含”$value的数组
在这个例子中,它应该给出变量3,在带有
$key
参数的整个数组上使用foreach:
foreach ($array as $key => $value) {
$slugValue = $value['license_type']['slug'];
if (...) { echo $slugValue; }
// $key contains current array index (0..2 for array in example)
// $value contains array with variation_name, license_type
}
重写foreach循环,如下所示:-
foreach ($attribute_pa_license_slug as $value) {
if(!empty($value['license_type']['slug'])){
$slug = $value['license_type']['slug'];
if (strpos($slug, 'license_solid') !== false) {
echo $slug; // echo your matched value
$data[] = $value; // store your array in data array
}
}
}
print_r($data); // print arrays who has valid slug values
传统的
foreach
适用于结构已知的数组,但适用于结构未知的大型数组
请看下面两种方法
<?php
$array = Array
(
'0' => Array
(
'variation_name' => 'variation_1',
'license_type' => Array
(
'slug' => 'license_x',
'price' => 'price_x',
'dimensions' => 'dimensions_x'
)
),
'1' => Array
(
'variation_name' => 'variation_2',
'license_type' => Array
(
'slug' => 'license_y',
'price' => 'price_y',
'dimensions' => 'dimensions_y'
)
),
'2' => Array
(
'variation_name' => 'variation_3',
'license_type' => Array
(
'slug' => 'license_solid_z',
'price' => 'price_z',
'dimensions' => 'dimensions_z'
)
)
);
//METHOD 1 - For known structured array
foreach($array AS $key => $val) {
$slug = $val['license_type']['slug'];
if (strpos($slug, 'license_solid') !== false) {
$data[] = $array[$key];
}
}
print_r($data);
//METHOD 2 - For unknown structured array (use iterator for unknow and large structured)
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
$data = array();
foreach ($it as $key => $val) {
$ar = $it->getSubIterator($it->getDepth() - 1);
if($key == 'slug' && strpos($val, 'license_solid') !== false){
$data[] = (array) $ar;
}
}
print_r($data);
只需在数组中循环。然后在循环内部,只需访问
slug
值,并检查其值是否以license\u solid
开头,如果是,您就得到了所需的子数组。我这样做了,但它仍然只给我license\u类型的slug
foreach($key=>$value的变体){$slugValue=$value['attributes']['attribute_pa_license'];if(0==strpos($slugValue,'license_solid'){echo';echo$slugValue;echo';}它给我:“警告:非法字符串偏移量”就好像$value它不是arrayDONE!它现在可以工作了!它直接打印:Array([0]=>7042)我怎么能只显示“7042”?我应该把$data[0]放在哪里
?在代码末尾,您得到了数组([0]=>7042)
Array
(
[0] => Array
(
[variation_name] => variation_3
[license_type] => Array
(
[slug] => license_solid_z
[price] => price_z
[dimensions] => dimensions_z
)
)
)