【问题标题】:How to fetch quote_path value in associative array where the key may vary in a dynamic loop?如何在动态循环中键可能不同的关联数组中获取quote_path值?
【发布时间】:2020-01-13 08:51:10
【问题描述】:

我正在尝试根据特定键获取值。密钥在循环中出现,但密钥的索引会有所不同。一种情况可以是 48,另一种情况可以是 50。共享下面的数组-

 [info_buyRequest] => Array
            (
                [uenc] => aHR0cDovL2xvY2FsaG9zdC9TdGlja2VyaXR3ZWIvY3VzdG9tLXN0aWNrZXJzL3RyYW5zZmVyLXN0aWNrZXJzLmh0bWw,
                [product] => 12
                [selected_configurable_option] => 
                [related_product] => 
                [qty] => 1
                [custom_quantity] => 500
                [custom_price1] => 303
                [options] => Array
                    (
                        [47] => 
                        [48] => Black
                        [49] => 8x10 cm
                        [51] => 
                        [76] => 500 pcs
                        [50] => Array
                            (
                                [type] => image/png
                                [title] => project_it_Side_Logo.png
                                [quote_path] => custom_options/quote/S/t/056de63143470f3907e354cd38f2dd3a.png
                                [order_path] => custom_options/order/S/t/056de63143470f3907e354cd38f2dd3a.png
                                [fullpath] => /var/www/html/project/pub/media/custom_options/quote/S/t/056de63143470f3907e354cd38f2dd3a.png
                                [size] => 57385
                                [width] => 1294
                                [height] => 353
                                [secret_key] => 056de63143470f3907e3
                            )

                    )

                [skipbutton] => 0
            )

【问题讨论】:

  • $yourObjectName["info_buyRequest"]["options"][50]["quote_path"] 应该这样做。
  • 数组'options'中是否总是有一个数组?
  • @ADyson 我猜他的意思是这个职位将是动态的..
  • @DanyalSandeelo 嗯,我想这是可能的。但如果是这个意思,那就太糟糕了……
  • 但是 Option[50] 的值发生了变化,然后我如何找到这个值

标签: php arrays loops foreach


【解决方案1】:

试试下面的代码。它对你有用。

$options = $objectName["info_buyRequest"]["options"];
$quotePaths = [];

foreach($options as $option){
   if( is_array ($option) && isset($option['quote_path'])){
     $quotePaths[] = $option['quote_path'];
   }
}

【讨论】:

    【解决方案2】:

    遍历options 数组,并使用is_array 找到该数组,如下所示:

    $array = $yourObjectName["info_buyRequest"]["options"];
    $innerArray = array();
    foreach($array as $value){
       if( is_array ($value)){
         $innerArray = $value;
         break;
       }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 2010-11-04
      相关资源
      最近更新 更多