foreach 相关问题

foreach是一个循环结构,它为列表/集合/数组中的每个元素执行给定的代码片段。与for循环相比,foreach循环不需要编码器维护计数器变量以避免一对一(fencepost)错误。建议在需要对整个数组/列表/集合进行简单迭代时使用。

当鼠标悬停在循环元素上时,有没有办法在循环元素上使用replaceChildren()?

const addToCartButtons = document.getElementsByClassName('添加到购物车'); const products = document.getElementsByClassName('product'); const addToCartButtonsArray = Array.from(addToCartButtons); c...

回答 1 投票 0

如何使用

我有2个数据集——主表和子表——它们是相关的。我想导出合并后的数据,但主行包含多个子行。 我尝试了 for 循环和 foreach datarow1 (插入数据...

回答 1 投票 0

如何使用 C# 将关系数据集导出到单个 Excel 工作表

我有2个数据集——主表和子表——它们是相关的。我想导出合并后的数据,但主行包含多个子行。 我尝试了 for 循环和 foreach datarow1 (插入数据...

回答 1 投票 0

如何从 foreach 循环 php laravel 获取自定义数据量

所以在 Eloquent 中有一个 take() 和skip() 函数,其工作原理如下: $users = DB::table('users')->skip(10)->take(5)->get(); 但现在我正在从 json 文件读取数据: $path = storage_pat...

回答 2 投票 0

如何在 VBA 中结束嵌套在“For Each”循环中的“If”语句?

我正在尝试将预算超支分摊到支出不足的预算中。目前,我的方法是循环遍历每一行,并将负值拟合为正值。 我的代码给了我一个“...

回答 1 投票 0

尝试找到一个可以正常工作的Wordpress(PHP代码)

我需要帮助来解决这个问题。我需要循环显示不同的帖子和页面,但我在嵌套它们时遇到了挑战。我不确定这是否是正确的词,但我正在粘贴我的代码...

回答 1 投票 0

以数组形式访问 div 的按钮

这是我遇到的问题: 我正在尝试访问 div 内部的按钮,并使每个按钮在单击时记录自己的文本内容。但它们都显示所有文本内容。而且它...

回答 1 投票 0

在单个资源中 Terraform 多个 for_each

我正在尝试创建多个 Github 存储库,并使用 JSON 文件将文件添加到新创建的存储库中。 # 地形当地人 当地人{ 绑定对 = toset([ 对于 setproduct 中的对( 键(...

回答 1 投票 0

我有两个不同的数组,我想删除第二个数组中具有与 array1 相同属性值的项目

代码中的 $response['results'] 用于 Google API。 $da 是我的数据库记录。我正在根据两个数据记录的纬度使用 foreach 来比较这两者。 这是我的代码: $setar...

回答 1 投票 0

JMeter:如何递减 Foreach 控制器中为 End 索引字段提供的变量?

我正在制定 JMeter 测试计划,从 JSON 响应中提取各种值。该计划要求我执行依赖于提供给 ForEach 的动态值的删除操作

回答 1 投票 0

如何使用 foreach 循环在我的代码中回显

这可能是基本的,但是,我找到了这段代码 函数 get_sorted_categories( $order_by = 'id' ){ 全局 $wpdb; $类别 = get_categories(); $订单= [ 'id' => '帖子ID', ...

回答 1 投票 0

使用 foreach() 循环文件;打印前需要删除重复的结果

我正在编写一些代码来自动化 fontfaceobserver.js。对于目录中的每个字体文件,我需要获取字体系列名称,以便可以在 javascript 中打印它。有些字体具有相同的家族...

回答 1 投票 0

为 joomla php 中的 foreach() 提供的参数无效

我收到以下错误: PHP 警告:为 E:\home cesseecom1\Web\libraries\joomla ccess ccess.php 第 409 行中的 foreach() 提供的参数无效 代码是: 我收到以下错误: PHP 警告:在 E:\home cesseecom1\Web\libraries\joomla ccess ccess.php 第 409 行为 foreach() 提供的参数无效 代码是: <?php /** * @package Joomla.Platform * @subpackage Access * * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE */ defined('JPATH_PLATFORM') or die; jimport('joomla.utilities.arrayhelper'); /** * Class that handles all access authorisation routines. * * @package Joomla.Platform * @subpackage Access * @since 11.1 */ class JAccess { /** * Array of view levels * * @var array * @since 11.1 */ protected static $viewLevels = array(); /** * Array of rules for the asset * * @var array * @since 11.1 */ protected static $assetRules = array(); /** * Array of user groups. * * @var array * @since 11.1 */ protected static $userGroups = array(); /** * Array of user group paths. * * @var array * @since 11.1 */ protected static $userGroupPaths = array(); /** * Array of cached groups by user. * * @var array * @since 11.1 */ protected static $groupsByUser = array(); /** * Method for clearing static caches. * * @return void * * @since 11.3 */ public static function clearStatics() { self::$viewLevels = array(); self::$assetRules = array(); self::$userGroups = array(); self::$userGroupPaths = array(); self::$groupsByUser = array(); } /** * Method to check if a user is authorised to perform an action, optionally on an asset. * * @param integer $userId Id of the user for which to check authorisation. * @param string $action The name of the action to authorise. * @param mixed $asset Integer asset id or the name of the asset as a string. Defaults to the global asset node. * * @return boolean True if authorised. * * @since 11.1 */ public static function check($userId, $action, $asset = null) { // Sanitise inputs. $userId = (int) $userId; $action = strtolower(preg_replace('#[\s\-]+#', '.', trim($action))); $asset = strtolower(preg_replace('#[\s\-]+#', '.', trim($asset))); // Default to the root asset node. if (empty($asset)) { $db = JFactory::getDbo(); $assets = JTable::getInstance('Asset', 'JTable', array('dbo' => $db)); $rootId = $assets->getRootId(); $asset = $rootId; } // Get the rules for the asset recursively to root if not already retrieved. if (empty(self::$assetRules[$asset])) { self::$assetRules[$asset] = self::getAssetRules($asset, true); } // Get all groups against which the user is mapped. $identities = self::getGroupsByUser($userId); array_unshift($identities, $userId * -1); return self::$assetRules[$asset]->allow($action, $identities); } /** * Method to check if a group is authorised to perform an action, optionally on an asset. * * @param integer $groupId The path to the group for which to check authorisation. * @param string $action The name of the action to authorise. * @param mixed $asset Integer asset id or the name of the asset as a string. Defaults to the global asset node. * * @return boolean True if authorised. * * @since 11.1 */ public static function checkGroup($groupId, $action, $asset = null) { // Sanitize inputs. $groupId = (int) $groupId; $action = strtolower(preg_replace('#[\s\-]+#', '.', trim($action))); $asset = strtolower(preg_replace('#[\s\-]+#', '.', trim($asset))); // Get group path for group $groupPath = self::getGroupPath($groupId); // Default to the root asset node. if (empty($asset)) { $db = JFactory::getDbo(); $assets = JTable::getInstance('Asset', 'JTable', array('dbo' => $db)); $rootId = $assets->getRootId(); } // Get the rules for the asset recursively to root if not already retrieved. if (empty(self::$assetRules[$asset])) { self::$assetRules[$asset] = self::getAssetRules($asset, true); } return self::$assetRules[$asset]->allow($action, $groupPath); } /** * Gets the parent groups that a leaf group belongs to in its branch back to the root of the tree * (including the leaf group id). * * @param mixed $groupId An integer or array of integers representing the identities to check. * * @return mixed True if allowed, false for an explicit deny, null for an implicit deny. * * @since 11.1 */ protected static function getGroupPath($groupId) { // Preload all groups if (empty(self::$userGroups)) { $db = JFactory::getDbo(); $query = $db->getQuery(true) ->select('parent.id, parent.lft, parent.rgt') ->from('#__usergroups AS parent') ->order('parent.lft'); $db->setQuery($query); self::$userGroups = $db->loadObjectList('id'); } // Make sure groupId is valid if (!array_key_exists($groupId, self::$userGroups)) { return array(); } // Get parent groups and leaf group if (!isset(self::$userGroupPaths[$groupId])) { self::$userGroupPaths[$groupId] = array(); foreach (self::$userGroups as $group) { if ($group->lft <= self::$userGroups[$groupId]->lft && $group->rgt >= self::$userGroups[$groupId]->rgt) { self::$userGroupPaths[$groupId][] = $group->id; } } } return self::$userGroupPaths[$groupId]; } /** * Method to return the JAccessRules object for an asset. The returned object can optionally hold * only the rules explicitly set for the asset or the summation of all inherited rules from * parent assets and explicit rules. * * @param mixed $asset Integer asset id or the name of the asset as a string. * @param boolean $recursive True to return the rules object with inherited rules. * * @return JAccessRules JAccessRules object for the asset. * * @since 11.1 */ public static function getAssetRules($asset, $recursive = false) { // Get the database connection object. $db = JFactory::getDbo(); // Build the database query to get the rules for the asset. $query = $db->getQuery(true); $query->select($recursive ? 'b.rules' : 'a.rules'); $query->from('#__assets AS a'); //sqlsrv change $query->group($recursive ? 'b.id, b.rules, b.lft' : 'a.id, a.rules, a.lft'); // If the asset identifier is numeric assume it is a primary key, else lookup by name. if (is_numeric($asset)) { $query->where('(a.id = ' . (int) $asset . ')'); } else { $query->where('(a.name = ' . $db->quote($asset) . ')'); } // If we want the rules cascading up to the global asset node we need a self-join. if ($recursive) { $query->leftJoin('#__assets AS b ON b.lft <= a.lft AND b.rgt >= a.rgt'); $query->order('b.lft'); } // Execute the query and load the rules from the result. $db->setQuery($query); $result = $db->loadColumn(); // Get the root even if the asset is not found and in recursive mode if (empty($result)) { $db = JFactory::getDbo(); $assets = JTable::getInstance('Asset', 'JTable', array('dbo' => $db)); $rootId = $assets->getRootId(); $query = $db->getQuery(true); $query->select('rules'); $query->from('#__assets'); $query->where('id = ' . $db->quote($rootId)); $db->setQuery($query); $result = $db->loadResult(); $result = array($result); } // Instantiate and return the JAccessRules object for the asset rules. $rules = new JAccessRules; $rules->mergeCollection($result); return $rules; } /** * Method to return a list of user groups mapped to a user. The returned list can optionally hold * only the groups explicitly mapped to the user or all groups both explicitly mapped and inherited * by the user. * * @param integer $userId Id of the user for which to get the list of groups. * @param boolean $recursive True to include inherited user groups. * * @return array List of user group ids to which the user is mapped. * * @since 11.1 */ public static function getGroupsByUser($userId, $recursive = true) { // Creates a simple unique string for each parameter combination: $storeId = $userId . ':' . (int) $recursive; if (!isset(self::$groupsByUser[$storeId])) { // Guest user (if only the actually assigned group is requested) if (empty($userId) && !$recursive) { $result = array(JComponentHelper::getParams('com_users')->get('guest_usergroup', 1)); } // Registered user and guest if all groups are requested else { $db = JFactory::getDbo(); // Build the database query to get the rules for the asset. $query = $db->getQuery(true); $query->select($recursive ? 'b.id' : 'a.id'); if (empty($userId)) { $query->from('#__usergroups AS a'); $query->where('a.id = ' . (int) JComponentHelper::getParams('com_users')->get('guest_usergroup', 1)); } else { $query->from('#__user_usergroup_map AS map'); $query->where('map.user_id = ' . (int) $userId); $query->leftJoin('#__usergroups AS a ON a.id = map.group_id'); } // If we want the rules cascading up to the global asset node we need a self-join. if ($recursive) { $query->leftJoin('#__usergroups AS b ON b.lft <= a.lft AND b.rgt >= a.rgt'); } // Execute the query and load the rules from the result. $db->setQuery($query); $result = $db->loadColumn(); // Clean up any NULL or duplicate values, just in case JArrayHelper::toInteger($result); if (empty($result)) { $result = array('1'); } else { $result = array_unique($result); } } self::$groupsByUser[$storeId] = $result; } return self::$groupsByUser[$storeId]; } /** * Method to return a list of user Ids contained in a Group * * @param integer $groupId The group Id * @param boolean $recursive Recursively include all child groups (optional) * * @return array * * @since 11.1 * @todo This method should move somewhere else */ public static function getUsersByGroup($groupId, $recursive = false) { // Get a database object. $db = JFactory::getDbo(); $test = $recursive ? '>=' : '='; // First find the users contained in the group $query = $db->getQuery(true); $query->select('DISTINCT(user_id)'); $query->from('#__usergroups as ug1'); $query->join('INNER', '#__usergroups AS ug2 ON ug2.lft' . $test . 'ug1.lft AND ug1.rgt' . $test . 'ug2.rgt'); $query->join('INNER', '#__user_usergroup_map AS m ON ug2.id=m.group_id'); $query->where('ug1.id=' . $db->Quote($groupId)); $db->setQuery($query); $result = $db->loadColumn(); // Clean up any NULL values, just in case JArrayHelper::toInteger($result); return $result; } /** * Method to return a list of view levels for which the user is authorised. * * @param integer $userId Id of the user for which to get the list of authorised view levels. * * @return array List of view levels for which the user is authorised. * * @since 11.1 */ public static function getAuthorisedViewLevels($userId) { // Get all groups that the user is mapped to recursively. $groups = self::getGroupsByUser($userId); // Only load the view levels once. if (empty(self::$viewLevels)) { // Get a database object. $db = JFactory::getDBO(); // Build the base query. $query = $db->getQuery(true); $query->select('id, rules'); $query->from($query->qn('#__viewlevels')); // Set the query for execution. $db->setQuery((string) $query); // Build the view levels array. foreach ($db->loadAssocList() as $level) { self::$viewLevels[$level['id']] = (array) json_decode($level['rules']); } } // Initialise the authorised array. $authorised = array(1); // Find the authorised levels. foreach (self::$viewLevels as $level => $rule) { foreach ($rule as $id) { if (($id < 0) && (($id * -1) == $userId)) { $authorised[] = $level; break; } // Check to see if the group is mapped to the level. elseif (($id >= 0) && in_array($id, $groups)) { $authorised[] = $level; break; } } } return $authorised; } /** * Method to return a list of actions for which permissions can be set given a component and section. * * @param string $component The component from which to retrieve the actions. * @param string $section The name of the section within the component from which to retrieve the actions. * * @return array List of actions available for the given component and section. * * @since 11.1 * * @deprecated 12.3 Use JAccess::getActionsFromFile or JAccess::getActionsFromData instead. * * @codeCoverageIgnore * */ public static function getActions($component, $section = 'component') { JLog::add(__METHOD__ . ' is deprecated. Use JAccess::getActionsFromFile or JAcces::getActionsFromData instead.', JLog::WARNING, 'deprecated'); $actions = self::getActionsFromFile( JPATH_ADMINISTRATOR . '/components/' . $component . '/access.xml', "/access/section[@name='" . $section . "']/" ); if (empty($actions)) { return array(); } else { return $actions; } } /** * Method to return a list of actions from a file for which permissions can be set. * * @param string $file The path to the XML file. * @param string $xpath An optional xpath to search for the fields. * * @return boolean|array False if case of error or the list of actions available. * * @since 12.1 */ public static function getActionsFromFile($file, $xpath = "/access/section[@name='component']/") { if (!is_file($file)) { // If unable to find the file return false. return false; } else { // Else return the actions from the xml. return self::getActionsFromData(JFactory::getXML($file, true), $xpath); } } /** * Method to return a list of actions from a string or from an xml for which permissions can be set. * * @param string|SimpleXMLElement $data The XML string or an XML element. * @param string $xpath An optional xpath to search for the fields. * * @return boolean|array False if case of error or the list of actions available. * * @since 12.1 */ public static function getActionsFromData($data, $xpath = "/access/section[@name='component']/") { // If the data to load isn't already an XML element or string return false. if ((!($data instanceof SimpleXMLElement)) && (!is_string($data))) { return false; } // Attempt to load the XML if a string. if (is_string($data)) { $data = JFactory::getXML($data, false); // Make sure the XML loaded correctly. if (!$data) { return false; } } // Initialise the actions array $actions = array(); // Get the elements from the xpath $elements = $data->xpath($xpath . 'action[@name][@title][@description]'); // If there some elements, analyse them if (!empty($elements)) { foreach ($elements as $action) { // Add the action to the actions array $actions[] = (object) array( 'name' => (string) $action['name'], 'title' => (string) $action['title'], 'description' => (string) $action['description'] ); } } // Finally return the actions array return $actions; } } 如果工作正常,只需尝试隐藏警告: error_reporting(0); 在文件开始处。

回答 1 投票 0

将 Terraform For_Each 循环用于 Azure Pipeline 阶段

我有以下 Terraform 配置文件 (alerts.tf),该文件旨在在我们的每个环境中设置 Azure 监控警报和操作组。对于这个插图,我限制了

回答 1 投票 0

为什么 PHP 数组中的 html '#XXXXXX' 颜色代码在调用所有以逗号分隔创建的数组值的 for 循环中会被忽略? (AshTag 问题?)

我试图允许基于浏览器的十六进制键盘的用户将十六进制键的颜色更改为 6 种基本(红色、紫色、蓝色、绿色、黄色、橙色)按键颜色以外的颜色。颜色定义为...

回答 1 投票 0

从嵌套数组中获取唯一值 - Laravel

我想从laravel中的数组中获取带有id的唯一名称。我怎样才能得到那个?我已经尝试过像下面这样的 foreach 循环,但没有成功。 $功能=[]; foreach ($vehicles as $key => $vehicle...

回答 1 投票 0

需要在 Powershell 中使用 2 个变量为 CSV 文件创建 foreach 循环

我是编码新手,我的大部分经验都是使用Python。 我正在编写一个简单的 powershell 脚本,该脚本在 CSV 文件中的 IP 地址和端口上运行 Test-NetConnection。 知识产权 港口 1.1.1.1 22 号 1...

回答 1 投票 0

Jmeter:如何为前一组的每个请求发布一个html请求

我正在Jmeter上安排压力测试:我有发布消息有效负载的html请求,然后在tearDown线程中发送不同的html请求(因为我需要它仅在

回答 1 投票 0

如何使用 foreach 循环进行迭代,以使用先前获取的值的增量值执行多次删除?

我有一个 JMeter 测试计划,其中一部分从请求的响应中提取值。到目前为止,我已经使用 JSON 提取器成功检索了三组值,并且能够评估...

回答 1 投票 0

JMeter:如何使用 foreach 循环进行迭代,以使用先前获取的值的增量值执行多次删除?

我有一个 JMeter 测试计划,其中一部分从请求的响应中提取值。到目前为止,我已经使用 JSON 提取器成功检索了三组值,并且能够评估...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.