PHP:跳过一个级别检查数组元素是否存在


PHP: Check if array element exists skipping one level

我试图检查数组元素是否已经存在,如果不存在,我需要创建一个数组元素,只填充一个值,第二个值设置为null。增加的复杂性是,在检查数组时,我需要忽略第二级,而不必再次循环遍历数组,因为它可能是一个相当大的数组。

我的数组如下:

Array
(
    [2016-05-28] => Array
    (
        [0] => Array
        (
            [store] => 1
            [price] => 12
        )
        [1] => Array
        (
            [store] => 7
            [price] => 18
        )
        [2] => Array
        (
            [store] => 9
            [price] => 
        )
    )
)

我试图检查是否有一个存储值为x的现有元素,如果它不存在,我会创建一个新元素,如果它确实存在,我就会忽略它并继续前进。

在这个例子中,我对$day$store变量进行了硬编码,但这通常会在For循环中填充,然后在For循环内运行下面的代码段。

我的代码:

$day = '2016-05-28';
$store = 8;
if (!$history[$day][][$store]) {
    $history[$day][] = array(
        "store" => $store
        , "price" => null
    );
}

问题是检查元素是否存在if (!$history[$day][][$store]) {,是否可以忽略$day元素和$store元素之间的第二级,以便检查store元素是否存在,我可以使用通配符还是in_array可以工作?

这是我目前正在使用的全部代码。

$setPriceHistoryData = $daoObj->getSetPriceHistoryData($set['id']);
$chartDays = date('Y-m-d', strtotime('-30 days'));
$priceHistoryData = array();
$endDay = date('Y-m-d');
while ($chartDays <= $endDay) {
    for ($i = 0; $i < count($setPriceData["price_history_store_data"]); $i++) {
        for ($j = 0; $j < count($setPriceHistoryData); $j++) {
            if ($setPriceData["price_history_store_data"][$i]["id"] == $setPriceHistoryData[$j]["vph_store"]
                && $chartDays == $setPriceHistoryData[$j]["vph_date"]) {
                $priceHistoryData[$chartDays][] = array(
                    "store" => $setPriceHistoryData[$j]["vph_store"]
                    , "price" => $setPriceHistoryData[$j]["vph_price"]
                );
            } else {
                if (!$priceHistoryData[$chartDays][]["store"]) {
                    $priceHistoryData[$chartDays][] = array(
                        "store" => $setPriceHistoryData[$j]["vph_store"]
                        , "price" => null
                    );
                }
            }
        }
    }
    // Increment day
    $chartDays = date('Y-m-d', strtotime("+1 day", strtotime($chartDays)));
} 

我会循环浏览所有日期。每天,循环浏览您希望找到的所有门店编号。使用array_filter查找所需的存储。如果你找不到所需的商店,请添加它。

$required_stores = [1,2,3,4]; // stores you wish to add if missing    
$source = [
    '2016-06-15'=>[
        ['store'=>1,'price'=>10],['store'=>2,'price'=>10],
    ],
    '2016-06-16'=>[
        ['store'=>1,'price'=>10],['store'=>3,'price'=>10],
    ],
    '2016-06-17'=>[
        ['store'=>3,'price'=>10],['store'=>4,'price'=>10],
    ],
];    
//go through all dates. Notice we pass $stores as reference
//using "&"  This allows us to modify it in the forEach
foreach ($source as $date => &$stores):       
    foreach($required_stores as $lookfor):
        //$lookfor is the store number we want to add if it's missing
        //will hold the store we look for, or be empty if it's not there
        $found_store = array_filter(
            $stores,
            function($v) use ($lookfor){return $v['store']===$lookfor;}
        );
        //add the store to $stores if it was not found by array_filter
        if(empty($found_store)) $stores[] = ['store'=>$lookfor,'price'=>null];
    endforeach;
endforeach;
// here, $source is padded with all required stores

正如Rizier123所建议的,您可以使用array_column((。您可以编写一个简单的函数,接受存储编号、引用的历史数组和日期:

$history = [
    '2016-05-28' => [
        ['store' => 1, 'price' => 23],
        ['store' => 2, 'price' => 23],
        ['store' => 3, 'price' => 23]
    ]
];
$store   = 8;
$day     = '2016-05-28';
function storeHistory($store, &$history, $day)
{
    if ( ! isset($history[$day])) {
        return false;
    }
    $presentStores = array_column($history[$day], 'store');
    if ( ! in_array($store, $presentStores)) {
        $history[$day][] = ['store' => $store, 'price' => null];
    }
}
storeHistory($store, $history, $day);
var_dump($history);
array (size=1)
  '2016-05-28' => 
    array (size=4)
      0 => 
        array (size=2)
          'store' => int 1
          'price' => int 23
      1 => 
        array (size=2)
          'store' => int 2
          'price' => int 23
      2 => 
        array (size=2)
          'store' => int 3
          'price' => int 23
      3 => 
        array (size=2)
          'store' => int 8
          'price' => null

应该用几个嵌套循环来完成,我认为如果历史中也不存在日期元素,您可能需要创建一个新的日期元素。

代码注释:

<?php
$history = Array
(
    '2016-05-28' => Array
    (
        0 => Array
        (
            'store' => 1,
            'price' => 12
        ),
        1 => Array
        (
            'store' => 7,
            'price' => 18
        ),
        2 => Array
        (
            'store' => 9,
            'price' => null
        )
    )
);
print_r($history);
$day = '2016-05-28';
$store = 8;
// loop through dates
foreach ($history as $key=>&$date){
  // scan for date
  $found_date = false;
  if ($key != $day) continue;
  $found_date = true;
  // scan for store
  foreach ($date as $item){
    $found_store = false;
    if ($item['store'] != $store) continue;
    $found_store = true;
    // stop looping if store found
    break;
  }
  // create null element
  if (!$found_store) {
      $date []= array(
          "store" => $store
          , "price" => null
      );
  }
  // stop looping if date found
  break;
}
// if date not found, create all elements
if (!$found_date) {
  $history[$day]= array(
    0 => array(
      "store" => $store
      , "price" => null
    )
  );
}
print_r($history);

之前:

Array
(
    [2016-05-28] => Array
        (
            [0] => Array
                (
                    [store] => 1
                    [price] => 12
                )
            [1] => Array
                (
                    [store] => 7
                    [price] => 18
                )
            [2] => Array
                (
                    [store] => 9
                    [price] => 
                )
        )
)

之后:

Array
(
    [2016-05-28] => Array
        (
            [0] => Array
                (
                    [store] => 1
                    [price] => 12
                )
            [1] => Array
                (
                    [store] => 7
                    [price] => 18
                )
            [2] => Array
                (
                    [store] => 9
                    [price] => 
                )
            [3] => Array
                (
                    [store] => 8
                    [price] => 
                )
        )
)

感谢@trincot评论的帮助,我使用存储id作为数组中的键,完成了我想要做的事情,工作代码如下。

$setPriceHistoryData = $daoObj->getSetPriceHistoryData($set['id']);
$chartDays = date('Y-m-d', strtotime('-30 days'));
$endDay = date('Y-m-d');
$priceHistoryData = array();
while ($chartDays <= $endDay) {
    for ($i = 0; $i < count($setPriceData["price_history_store_data"]); $i++) {
        $store = $setPriceData["price_history_store_data"][$i]["id"];
        for ($j = 0; $j < count($setPriceHistoryData); $j++) {                    
            if ($store == $setPriceHistoryData[$j]["vph_store"]
                && $chartDays == $setPriceHistoryData[$j]["vph_date"]
                && !isset($priceHistoryData[$chartDays][$store])) {
                $priceHistoryData[$chartDays][$store] =  $setPriceHistoryData[$j]["vph_price"];
            } else {
                if (!isset($priceHistoryData[$chartDays][$store])) {
                    $priceHistoryData[$chartDays][$store] = null;
                }
            }
        }
    }
    // Increment day
    $chartDays = date('Y-m-d', strtotime("+1 day", strtotime($chartDays)));
}
<?php
$history = array();    // Assuming that's array's identifier.
$history['2016-05-28'] = array (
    array('store' => 1, 'price' => 12),
    array('store' => 7, 'price' => 18),
    array('store' => 9, 'price' => 20)
);
// variables for the if condition
$day = '2016-05-28';
$store = 8;
$match_found = FALSE;
foreach($history[$day] as $element) {
    if ($element['store'] == $store) {
        $match_found = TRUE;
    }
    else {
        continue;
    }
}
if ($match_found == TRUE) {
    // I included a break statement here. break works only in iterations, not conditionals. 
} else {
    array_push($history[$day], array('store' => $store, 'price' => null));
    // I was pushing to $history[$date] instead of $history[$day] since the variable I created was $day, NOT $date
}

我重写了PHP代码段,只是因为键值声明中出现了一些错误。例如,根据PHP规范,2016-05-28键元素应该是字符串或整数(http://php.net/manual/en/language.types.array.php)
所以您的代码片段会像上面的代码一样出现

我编辑了代码,将数据附加到主日期元素中,而不是根

由于您愿意更改数据结构,一种不同的、非常酷的方法只需几行简单的代码就可以完成这项工作。首先将数据源更改为新形状,其中商店ID是关键,价格是值

$history = [
    '2016-06-15'=>[
        1=>10, 2=>10, //On June 15, store 1 had price 10 
    ],
    '2016-06-16'=>[
        1=>10, 3=>10,
    ],
    '2016-06-17'=>[
        3=>10, 4=>10,
    ],
];

现在,您所要做的就是在源代码中循环使用天数,并在数组上使用+运算符添加任何丢失的存储(它会添加丢失的密钥(

$required_stores = [1,2,3,4]; // stores you wish to add if missing
//will be [1=>null, 2=>null, 3=>null, 4=>null]
$required_keys = array_combine(
                $required_stores,
                array_fill(0,count($required_stores),null)
    );
//go through each day and add required keys if they're missing
foreach ($history as &$stores):
    $stores += $required_keys
endforeach;

CCD_ 11通过使用第一参数作为键并且使用第二参数作为值来返回数组。