Php 按键中的值搜索数组并获取另一个键';s值
这是我的阵列:Php 按键中的值搜索数组并获取另一个键';s值,php,arrays,loops,multidimensional-array,Php,Arrays,Loops,Multidimensional Array,这是我的阵列: [0] => Array ( [messages] => Array ( [0] => Array ( [message] => This is for sandwich ) [1] => 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
)
)
我想搜索所有数组,我想搜索单词“burger”。我想知道“汉堡”的价格和数量是5。如果我搜索“热狗”这个词,它将返回价格金额3。我该怎么做?感谢如果
$array
成为您的阵列。我想这可能有用
<?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>' ;
}
}
}
?>
如果
$array
是你的数组。我想这可能有用
<?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>' ;
}
}
}
?>
您可以使用
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;
您可以使用
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;
这里我们使用的是
array\u column
,内爆
和preg\u match
1。用于检索数组的特定列
2.用胶水将数组连接起来,使其成为字符串
3.此处用于匹配给定字符串中的特定单词
这里我们使用的是
array\u column
,内爆
和preg\u match
1。用于检索数组的特定列
2.用胶水将数组连接起来,使其成为字符串
3.此处用于匹配给定字符串中的特定单词
还请向我们展示您已经尝试过的内容。使用类似的解决方案,将精确的值匹配替换为
pregmatch
或strpos
@GayanL您可以展示示例吗?thanks@JohnJaoill我认为每个问题都有它自己的重要性和价值,但在这里,你应该先尝试一下,然后就你尝试过的代码发布帮助信息。@johnjaoil在上面的评论中给出公认的答案,$product[$field]===$value
这一步需要更改strops('burger',$product[$field])>0
请向我们展示您已经尝试过的内容。使用类似的解决方案,将精确的值匹配替换为pregmatch
或strpos
@GayanL您可以展示示例吗?thanks@JohnJaoill我认为每个问题都有它自己的重要性和价值,但在这里,你应该先尝试一下,然后用你尝试过的代码发布帮助。@johnjaoil在上面的评论中给出公认的答案,$product[$field]===$value
这一步需要更改strops('burger',$product[$field])>0