【问题标题】:Retrieve nested array key检索嵌套数组键
【发布时间】:2017-07-17 21:53:37
【问题描述】:

我要疯了。 所以,假设我得到了这个数组:

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”开头的数组值和包含它的数组的值。 要获得“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 的数组 在这个例子中,它应该给出variation_3

【问题讨论】:

  • 循环遍历你的数组。然后在循环内简单地访问slug 值并检查它的值是否以license_solid 开头,如果是,你得到了你想要的子数组。

标签: php arrays wordpress woocommerce


【解决方案1】:

重写你的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

【讨论】:

  • 它给了我:“警告:非法字符串偏移”好像 $value 它不是一个数组
  • 完成!现在可以使用了!
  • 它 coorectly 打印:Array ( [0] => 7042 ) 我怎么能只显示“7042”?
  • 我应该把$data[0]放在哪里?
  • 在你的代码末尾你得到 Array ( [0] => 7042 )
【解决方案2】:

传统的foreach适用于结构已知的数组,但对于结构未知的大型数组array iterator比较好。

看看下面两种方法

<?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);

输出:

Array
(
    [0] => Array
        (
            [variation_name] => variation_3
            [license_type] => Array
                (
                    [slug] => license_solid_z
                    [price] => price_z
                    [dimensions] => dimensions_z
                )

        )

)

【讨论】:

    【解决方案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
    }
    

    【讨论】:

    • 我做到了,但它仍然给了我license_type的蛞蝓
    • foreach ($variations as $key =&gt; $value) { $slugValue = $value['attributes']['attribute_pa_license']; if (0 === strpos($slugValue, 'license_solid')) { echo '&lt;span class="ss_product_resolution_title"&gt;'; echo $slugValue; echo '&lt;/span&gt;'; }}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 2017-09-27
    • 2018-12-15
    • 2017-11-19
    • 2020-10-01
    • 2017-08-28
    • 1970-01-01
    相关资源
    最近更新 更多