0

I have a json file with this below exact format structure.

[  
 {  
    "name":"banana",
    "type":"fruit",
    "rate":10
 },
 {  
    "name":"orange",
    "type":"fruit",
    "rate":20
 },
 {  
    "name":"apple",
    "type":"fruit",
    "rate":30
 }
]

The goal logic would be

  1. if the fruit rate searched is 30 ,i would update it to 31

  2. if the fruit rate searched is 31 ,i would update it to 32

  3. if the fruit rate searched is 32 ,i would update it to 33

  4. if the fruit rate searched is 33 , stop updating and echo "Over Priced";

I would like to update the rate of the fruit when searched with fruit name "apple" by +1 upto +3 when i match a search for it ?.

How can i achieve the goal logic ?

I tried programming lengthy code here but it failed with finding index to modify - finding php json array index number

1
  • you can update what you want and check other limit, after update it you can break the loop with use break at the foreach. Commented Sep 18, 2014 at 6:12

2 Answers 2

1
$text = file_get_contents('fruits.txt');
$json = json_decode($text , true);
$searchedFruit = 'apple';

foreach ($json as &$fruit){
  if($fruit['name'] === $searchedFruit){
    if($fruit['rate'] >= 30 && $fruit['rate'] < 33){
      $fruit['rate']++;        
    } else if($fruit['rate'] === 33){
      echo "Over priced";
    }
  }
}

var_dump($json);

file_put_contents('fruits.txt', json_encode($json));
Sign up to request clarification or add additional context in comments.

1 Comment

I don't want all the fruit rates to be increased to +1 , instead if i search orange , it should update only the value of orange rate by +1
0

You went too far with your previous code

$json = '[{"name":"banana","type":"fruit","rate":31},
 {"name":"orange","type":"fruit","rate":32},
 {"name":"apple","type":"fruit","rate":33}
]';

$array = json_decode($json, true);

$maximum_rate = 32;
$searched_fruit = "apple";

foreach ($array as &$value) {
  if ($value['name'] == $searched_fruit) {
    if ($value['rate'] > $maximum_rate) {
      echo 'Over priced : ' . $value['name'] . "\n";
      break;
    } else {
      $value['rate']++;
      break;
    }
  }
}

$json = json_encode($array);

var_dump($json);

Output :

Over priced : apple
[{"name":"banana","type":"fruit","rate":31},
 {"name":"orange","type":"fruit","rate":32},
 {"name":"apple","type":"fruit","rate":33}]

4 Comments

I don't want all the fruit rates to be increased to +1 , instead if i search orange , it should update only the value of orange rate by +1
@BetaCoder It won't, it will update only the fields corresponding to $searched_fruit. I used a foreach in the case where you'd have several fruits / types or don't know what with the same name. Edit : Oh right, I got what you mean when I added breaks. Your question wasn't clear about the "the wanted fruit only"
it will +1 every fruit that is not the searched one with your answer. You have only 1 if clause with all conditions, so the else will be executed for every non searched element
Ok... your answer was edited while I was writing... ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.