【问题标题】:Efficient way to search array with substring使用子字符串搜索数组的有效方法
【发布时间】:2021-08-20 08:39:13
【问题描述】:

这是我的数组。

array (
  0 => 
  (object) array(
     'per_unit_price' => NULL,
     'code' => 'dynamic_labour_cost',
     'text_value' => '1000',
  ),
  1 => 
  (object) array(
     'per_unit_price' => NULL,
     'code' => 'dynamic_metal_weight',
     'text_value' => '10',
  ),
  2 => 
  (object) array(
     'per_unit_price' => NULL,
     'code' => 'dynamic_stone_carat',
     'text_value' => '10',
  ),
  3 => 
  (object) array(
     'per_unit_price' => NULL,
     'code' => 'dynamic_stone2_carat',
     'text_value' => '10',
  ),
  4 => 
  (object) array(
     'per_unit_price' => '10.00',
     'code' => 'dynamic_metal',
     'text_value' => NULL,
  ),
  5 => 
  (object) array(
     'per_unit_price' => '20.00',
     'code' => 'dynamic_stone',
     'text_value' => NULL,
  ),
  6 => 
  (object) array(
     'per_unit_price' => '50.00',
     'code' => 'dynamic_stone2',
     'text_value' => NULL,
  ),
)

总之,

举个例子=

我想相乘 => array[1]['text_value'] * array[4]['per_unit_price']

因为 array[1]['code'] = dynamic_metal_weightarray[4]['code'] = dynamic_metal 的子字符串

由于所有代码都是动态的,所以我不能硬编码条件。

我尝试使用 for each [扫描所有数组],但传统循环会花费很多时间。

请指导我。

【问题讨论】:

  • 请在问题中粘贴var_export($array);,并向我们展示您的最佳尝试。这就是为什么你应该not post pictures 的代码。阅读How to Ask
  • 你能告诉我们any code you already have吗?
  • @AakashThoriya 你所说的循环是指嵌套循环吗?您还没有分享您的尝试和预期输出。
  • @AakashThoriya 你能告诉我,如果让我们说 dynamic_metal 每个单价数组元素相乘,其他代码总是有前缀 dynamic_metal 然后你希望它乘以那个。
  • 如果你坚持这样并且你不能在数据库中这样做,创建一个使用code作为键索引的新数组,值是一个对象数组用那个代码。您可以这样做一次,然后不是一个巨大的循环,而是一个键查找,然后是一个较小的循环。

标签: php arrays laravel search substring


【解决方案1】:

正如@Chris Haas 所建议的,此解决方案基于相同的逻辑。根据我在下面的问题描述中看到的内容,可以为您提供帮助。还提供了即兴创作的建议..

<?php
echo "<pre>";
// array of data received from database
$code_values = [
    [
        'per_unit_price' => NULL,
        'code' => 'dynamic_labour_cost',
        'text_value' => '1000'
    ],
    [
        'per_unit_price' => NULL,
        'code' => 'dynamic_metal_weight',
        'text_value' => '10'
    ],
    [
        'per_unit_price' => NULL,
        'code' => 'dynamic_stone_carat',
        'text_value' => '10'
    ],
    [
        'per_unit_price' => NULL,
        'code' => 'dynamic_stone2_carat',
        'text_value' => '10'
    ],
    [
        'per_unit_price' => '10.00',
        'code' => 'dynamic_metal',
        'text_value' => NULL
    ],
    [
        'per_unit_price' => '20.00',
        'code' => 'dynamic_stone',
        'text_value' => NULL
    ],
    [
        'per_unit_price' => '50.00',
        'code' => 'dynamic_stone2',
        'text_value' => NULL
    ]
];

// filter $code_values to extract an array of elements where "per_unit_price" is set i.e. text_value is NULL
// Suggestion to improvise: If from database, you can fetch codes where only per_unit_price are set then this array_filter will not be required
$per_unit_prices = array_filter($code_values, function($array_element) {
    return is_null($array_element['text_value']);
});
// create array of per unit prices where code will be key and per_unit_price will be value
$per_unit_prices = array_column($per_unit_prices, 'per_unit_price', 'code');
echo "<br>per_unit_prices<br>";
print_r($per_unit_prices);

// filter $code_values to extract an array of elements where "text_value is set i.e. per_unit_price is NULL
// Suggestion to improvise: If you can fetch codes where only text_values are set then this array_filter will not be required
$text_values = array_filter($code_values, function($array_element) {
    return is_null($array_element['per_unit_price']);
});
// create array of text_values where code will be key and text_value will be value
$text_values = array_column($text_values, 'text_value', 'code');
echo "<br>text_values<br>";
print_r($text_values);

$output = []; // array to store multiplication of per_unit_price and text_value of matching elements
foreach($text_values as $code => $value) {
    // create key of per_unit_price by removing last string part starting with _
    $per_unit_prices_key = implode('_', array_slice(explode('_', $code), 0, -1));

    // check if per_unit_prices_key exits in $per_unit_prices array we created. If so, create output value
    if(isset($per_unit_prices[$per_unit_prices_key])) {
        $output[$code] = (float) $value * (float)$per_unit_prices[$per_unit_prices_key];
    }
}

echo "<br>output<br>";
print_r($output);

这是上面代码的输出。

per_unit_prices
Array
(
    [dynamic_metal] => 10.00
    [dynamic_stone] => 20.00
    [dynamic_stone2] => 50.00
)

text_values
Array
(
    [dynamic_labour_cost] => 1000
    [dynamic_metal_weight] => 10
    [dynamic_stone_carat] => 10
    [dynamic_stone2_carat] => 10
)

output
Array
(
    [dynamic_metal_weight] => 100
    [dynamic_stone_carat] => 200
    [dynamic_stone2_carat] => 500
)

【讨论】:

    猜你喜欢
    • 2012-12-18
    • 1970-01-01
    • 2011-11-02
    • 2012-03-23
    • 2014-04-02
    • 2014-05-24
    • 2012-01-29
    • 2010-10-25
    • 1970-01-01
    相关资源
    最近更新 更多