[英]Search array by value in key and get another key's value
here is my array:这是我的数组:
[0] => Array
(
[messages] => Array
(
[0] => Array
(
[message] => This is for sandwich
)
[1] => Array
(
[message] => This message is for burger
)
)
[price] => Array
(
[amount] => 5
[currency] => USD
)
[1] => Array
(
[messages] => Array
(
[0] => Array
(
[message] => This is a message for a delicious hotdog
)
)
[price] => Array
(
[amount] => 3
[currency] => USD
)
)
i want to search in ALL arrays and I want to search for the word "burger".我想搜索所有 arrays 并且我想搜索“汉堡”这个词。 I want to get the price and amount of "burger" which is 5. If I search for the word "hotdog", it will return the price amount 3. How can i do that?
我想得到“汉堡”的价格和数量,即 5。如果我搜索“热狗”这个词,它将返回价格数量 3。我该怎么做? thanks
谢谢
If $array
be your array. 如果
$array
是您的数组。 I Think It may Work. 我认为这可能有效。
<?php
$check = 'hotdog';
foreach($array as $products){
foreach($products['messages'] as $messages){
if (strpos($messages['message'], $check) !== false) {
echo $check.' Found. Price'. $products['price']['amount'] .'</br>' ;
}
}
}
?>
You can use a foreach
loop and then use strpos
or stripos
. 您可以使用
foreach
循环,然后使用strpos
或stripos
。
foreach ($array as $row) {
foreach ($row['messages'] as $row2) {
if(strpos($row2['message'], 'burger') !== false) {
$stringFound = true;
} else {
$stringFound = false;
}
}
if($stringFound === true) {
$price = $row['price']['amount'];
} else {
$price = '0';
}
}
echo $price;
Here we are using array_column
, implode
and preg_match
. 在这里,我们使用
array_column
, implode
和preg_match
。
1.
array_column
for retrieving specific column of an array1.
array_column
用于检索数组的特定列2.
implode
joins a array with a glue to make it string.2.
implode
用胶水将一个数组连接成字符串。3.
preg_match
here for matching specific word in the given string.3.
preg_match
在这里用于匹配给定字符串中的特定单词。
Try this code snippet here 在此处尝试此代码段
$toSearch="hotdog";
foreach($array as $key => $value)
{
if(preg_match("/\b$toSearch\b/",implode(",",array_column($value["messages"],"message"))))
{
echo $value["price"]["amount"];
}
}
Because you are searching for the first qualifying price only, it is best practice to use a conditional break
to short circuit the loops.因为您只搜索第一个合格价格,所以最佳做法是使用条件
break
来短路循环。 There is no reason to do more cycles if you have found a qualifying entry.如果您找到了符合条件的条目,则没有理由进行更多循环。
Instantiate the $amount
variable with a null
value before the loop to properly distinguish between a successful search and an unsuccessful one.在循环之前用
null
值实例化$amount
变量,以正确区分成功搜索和不成功搜索。
str_contain()
is the modern function to case-sensitively search for a substring (PHP7.4 and higher). str_contain()
是现代 function 以区分大小写搜索 substring(PHP7.4 及更高版本)。 If you need case-insensitivity, you will need to use stripos($message['message'], $needle) == false
.如果您需要不区分大小写,则需要使用
stripos($message['message'], $needle) == false
。 If you need to match whole words, you will need to call preg_match("/\b$needle\b/i", $message['message'])
.如果您需要匹配整个单词,则需要调用
preg_match("/\b$needle\b/i", $message['message'])
。 If using regex and the $needle
is coming from user input, you will need to apply escaping beforehand with $needle = preg_quote($needle, '/');
如果使用正则表达式并且
$needle
来自用户输入,则需要预先使用$needle = preg_quote($needle, '/');
. .
$needle = 'burger';
$amount = null;
foreach ($haystack as ['messages' => $messages, 'price' => $price]) {
foreach ($messages as $message) {
if (str_contains($message['message'], $needle)) {
$amount = $price['amount'];
break;
}
}
}
var_export($amount);
// 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.