问题标签 [array-key-exists]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
php - 检查数组内的数组是否存在于php中
一次只能退回一种产品。当产品有成分时,我的代码按预期工作。如果没有,我会得到:
未定义的偏移量:0
其中引用了代码行:if ($response_decoded['products'][0]['ingredients'] != null){
我知道这是因为没有成分,但有时会出现这种情况,这是不可避免的。
所以,此时:
...我回来了:
等等...
然后我做:
因此,不仅仅是!= null
我相信我需要事先检查“成分”是否存在。我虽然可以array_key_exists
用来做这个。
现在可以了,它会告诉我产品是否存在……但是如何检查产品的成分是否存在?
php - If array key exists return its value
I have an array called all_teams which contains the following
I do a check within a foreach
to check if teamId
is in the array keys. If the array key exists I want to display the key's value.
So far I have
php - 检查数组是否包含索引 php
这是我从前端发布的数据。
我需要的是我确实想在 RULE 数组中检查它是否包含一个名为 NAME 的索引。如果我像下面的代码一样检查
我收到错误为未定义的索引名称。那么检查数组的索引或元素是否存在的方法是什么?提前致谢
php - Replacing array_key_exists() with isset() in PHP
Knowing the differences of array_key_exists()
and isset()
in PHP, I see a lot of advocates on the web suggesting for replacing array_key_exists()
with isset()
, but I am thinking is it safe to do so?
In one project, I have the value of $var submitted by users. The value can be anything, even NULL or not set at all. I want to use !empty($var)
to check if $var
holds a non-empty value, but I understand that using !empty($var)
alone is dangerous since if $var
isn't pre-defined, PHP will throw an error.
So I have isset($var) && !empty($var)
for checking whether $var
holds a non-empty value.
However, things get complicated when I have a value stored in an assoc array. Compare the followings, assuming array $arr
always exists but the key foo
may or may not exist in $arr
.
Code snipplet 2 will always work but it seems clumsy and harder to read. As for code snipplet 1, I had bad experience... I wrote something like that before in the past on a development machine. It ran fine, but when I deployed the code to the production machine, it threw errors simply because key didn't exist in array. After some debugging, I found that the PHP configs were different between the development and the production machines and their PHP versions are slightly different.
So, I am thinking is it really that safe to just replace array_key_exists()
with isset()
? If not, what can be of better alternatives of code snipplet 2?
php - 使用 PDO 和 Mysql 检查数组中是否存在记录
我正在尝试在我的 mysql 中获取记录,无论记录是否存在于数组中。我在网上搜索了有关使用 array_key_exists 函数的信息,不幸的是它总是给我“找不到匹配”,即使该记录存在于数组中。
这是我的输出。
通过告诉我的代码有什么问题来帮助我?
php - array_key_exists 没有按预期工作
问题
我在使用 PHP 函数时遇到问题array_key_exists
。即使我的数组有键,该函数也总是返回 false。我想知道使用动态增长的数组是否存在问题。我是 PHP 新手,如果这个问题很烦人,我很抱歉。
预期行为
array_key_exists
如果数组有键,我需要返回 true 的函数。
我试图解决的问题
我尝试isset(CounterArray[$key])
改用,但没有成功。
我已经阅读了特定功能的 PHP 文档,并且我还检查了有关堆栈溢出的类似问题,但它们都不适合我的情况。我可耻地为此花费了大量时间。
代码
php - array_key_exists 错误莫名其妙
这很可能不是可以通过复制和粘贴复制的东西。我在这里遇到的问题是array_key_exists
根本行不通。
我已将代码打印到页面上;它绝对是数组类型,不是必需的。数组键存在;我在字符串和整数之间进行了转换,但找不到它回来了。
现在,最令人费解的是我在
输出:
输出非常奇怪,因为在int
&string
搜索中,键匹配。事实上,在string
搜索中,它会输出“string found1”,这意味着它 === 数组中的一个键。那么,为什么array_key_exists
总是在生产FALSE
?
该变量$presets
是一些数组,其索引是使用 preg_match 正则表达式代码创建的以解析索引。这是我能想到的唯一增加的谜团,尽管手动比较已经证明发现了 === 值,因此这些都不重要。array_key_exists
应该返回TRUE
php - 如何查找字符串变量并与数组进行比较?
我有这个数组:
首先,我想保存数据类型、变量名和值。其次,我想比较一个变量,比如我在数组中有没有这个变量,然后找到它是否相等的值。这是我的代码:
所以,这里我有 $variable="own=1";。我想在数组中搜索变量名是否存在于数组中的变量,并比较它是否等于 1 的值。任何建议都将是可观的。
php - 两个数组,一个有索引,一个没有 - 如何测试
我有两个数组。一个数组是没有索引的单个记录数组 - 这是供应商提供所有单个记录的方式。第二个数组(有两个或多个记录包含索引 [0] - [9]。我有一个 foreach 循环,它在多个记录上效果很好,但 foreach 不适用于单个记录(可能是一个字符串)。我需要一个解决方案来测试这两者之间的差异,最好的选择似乎是索引本身。如果您有不同的看法,请告诉我。
这是数组键(每次调用都相同): Array ( [0] => 0 )
以下是单条记录结果:
这是多记录结果(注意索引):
正如我所看到的,我必须测试'[0]',如果存在零,那么 X 否则 Y 计数适用于多条记录,但不适用于单条记录。我尝试了 array_key_exists 但没有任何运气我也尝试了数组搜索但我没有到达那里......
我的问题是:我如何测试这个组合中的索引 0?我愿意接受任何适用于这种情况的解决方案。非常感谢!