0

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". 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? thanks

John Jaoill
  • 159
  • 2
  • 9
  • 3
    Please also show us what you have already tried. – Fred Gandt May 27 '17 at 03:21
  • Use a similar solution, replace the exact value match with a `pregmatch` or `strpos` https://stackoverflow.com/questions/8102221/php-multidimensional-array-searching-find-key-by-specific-value – Gayan Hewa May 27 '17 at 03:37
  • @GayanL can you show example please? thanks – John Jaoill May 27 '17 at 03:42
  • 1
    @JohnJaoill I think every question has its own importance and value, but here you should make a try first then post for help with the code you have tried. – Sahil Gulati May 27 '17 at 03:44
  • @JohnJaoill the accepted answer in the above comment , `$product[$field] === $value` this step needs to change `strpos('burger', $product[$field]) > 0` – Gayan Hewa May 27 '17 at 03:44

4 Answers4

2

If $array be your 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>' ;
         }
        }
    }
?>
Adharsh M
  • 2,773
  • 2
  • 20
  • 33
2

You can use a foreach loop and then use strpos or 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;
Edward
  • 2,291
  • 2
  • 19
  • 33
2

Here we are using array_column, implode and preg_match.

1. array_column for retrieving specific column of an array

2. implode joins a array with a glue to make it string.

3. preg_match here for matching specific word in the given string.

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"];
    }
}
Community
  • 1
  • 1
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
0
  1. Because you are searching for the first qualifying price only, it is best practice to use a conditional break to short circuit the loops. There is no reason to do more cycles if you have found a qualifying entry.

  2. Instantiate the $amount variable with a null value before the loop to properly distinguish between a successful search and an unsuccessful one.

  3. str_contain() is the modern function to case-sensitively search for a substring (PHP7.4 and higher). If you need case-insensitivity, you will need to use 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']). If using regex and the $needle is coming from user input, you will need to apply escaping beforehand with $needle = preg_quote($needle, '/');.

Code: (Demo)

$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
mickmackusa
  • 43,625
  • 12
  • 83
  • 136