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
  • 139
  • 1
  • 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

3 Answers3

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,713
  • 2
  • 18
  • 31
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,237
  • 1
  • 18
  • 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
  • 14,792
  • 4
  • 23
  • 42