<?php /** * JSON data that has been loaded */ $json = '[{"barcode":"05000168176833","name":"McVities Hobnobs Milk Chocolate Biscuits 262g","price":null,"ingredients":"Rolled Oats (30%), Milk Chocolate (25%) [Sugar, Cocoa Butter, Cocoa Mass, Dried Skimmed Milk, Dried Whey (Milk), Butter Oil (Milk), Vegetable Fats (Shea, Palm), Emulsifiers (Soya Lecithin, E476), Natural Flavouring], Wholemeal Wheat Flour (16%), Sugar, Vegetable Oil (Palm), Glucose-Fructose Syrup, Raising Agents (Sodium Bicarbonate, Ammonium Bicarbonate), Salt","is_vegetarian":1,"is_vegan":0,"allergens":["Oats","Wheat","Soya","Palm Oil","Cow Milk"],"created_at":"2018-08-03T04:08:39.000000Z","updated_at":"2021-03-18T01:01:37.000000Z"},{"barcode":"05000328428581","name":"Walkers Sensations Lime & Coriander Chutney Sharing Poppadoms 82.5g","price":null,"ingredients":"Sunflower Oil (22%), Potato Granules (17%), Potato Starch (17%), Gram Flour (14%), Rice Flour, Lime & Coriander Chutney Seasoning, Salt, Lime and Coriander Chutney Seasoning contains: Flavouring, Sugar, Acidity Regulators (Sodium Diacetate, Citric Acid), Dried Garlic, Dried Onion, Coriander, Paprika, Chilli Powder, Cumin, Lime Oil, Barley Malt Extract","is_vegetarian":1,"is_vegan":2,"allergens":["Barley","Yeast"],"created_at":"2018-08-03T04:16:33.000000Z","updated_at":"2021-03-18T01:02:52.000000Z"},{"barcode":"05000137002965","name":"Jacobs Twiglets Original 150g","price":null,"is_vegetarian":1,"is_vegan":2,"allergens":["Barley","Wheat","May contain Milk","May contain Egg","May contain Mustard","May contain Sesame","Yeast","Palm Oil"],"created_at":"2018-08-03T09:33:43.000000Z","updated_at":"2021-03-18T01:02:08.000000Z"},{"barcode":"05035735162209","name":"McDougalls 00 Grade Premium Plain Flour 1kg","price":null,"ingredients":"Wheat Flour (with added Calcium, Iron, Niacin, Thiamin)","is_vegetarian":1,"is_vegan":2,"allergens":["Wheat"],"created_at":"2018-09-05T11:49:46.000000Z","updated_at":"2021-03-18T01:02:55.000000Z"},{"barcode":"00000001147667","name":"Sainsburys Lemon Curd Sponge Puddings 2 x 110g (220g)","price":null,"ingredients":"Fortified Wheat Flour (Wheat Flour, Calcium Carbonate, Iron, Niacin, Thiamin), Granulated Sugar, Pasteurised Free Range Whole Egg, Unsalted Butter (Cows Milk) (12%), Caster Sugar, Water, Cows Milk, Glucose Syrup, Salted Butter (Cows Milk) (5%), Cornflour, Lemon Juice (1.5%), Concentrated Lemon Juice (1%), Raising Agents: Calcium Phosphate, Disodium Diphosphate, Sodium Hydrogen Carbonate, Potassium Hydrogen Carbonate, Calcium Lactate, Lemon Zest, Acidity Regulator: Citric Acid, Gelling Agent: Pectin, Flavouring, Colour: Lutein","is_vegetarian":1,"is_vegan":0,"allergens":["Wheat","Eggs","Cow Milk"],"created_at":"2018-09-10T20:26:31.000000Z","updated_at":"2021-03-18T01:02:54.000000Z"},{"barcode":"04025500243548","name":"Müller Light Raspberry and Cranberry Fat Free Yogurt 160g","price":null,"ingredients":"Yogurt (Milk), Raspberry Puree (8%), Cranberry Juice from Concentrate (6%), Modified Maize Starch, Acidity Regulators: Citric Acid, Sodium Citrates, Sweetener: Aspartame, Flavourings, Colouring Foods: Beetroot y Juice Concentrate, Black Carrot Concentrate","is_vegetarian":1,"is_vegan":0,"allergens":["Cow Milk","Aspartame"],"created_at":"2019-05-03T11:56:05.000000Z","updated_at":"2021-03-18T01:02:45.000000Z"},{"barcode":"00000000375559","name":"Sainsburys Taste the Difference Rum & Raisin Sponge Puddings 2 x 88g (176g)","price":null,"ingredients":"Water, Fortified Wheat Flour (Wheat Flour, Calcium Carbonate, Iron, Niacin, Thiamin), Caster Sugar, Golden Syrup, Dates (7%), Demerara Sugar, British Whipping Cream (Cows Milk) (6%), Salted Butter (6%) (Butter (Cows Milk), Salt), Butter (Cows Milk) (3.5%), Raisins (3.5%), Rum (3.5%), Pasteurised Free Range Whole Egg, Dark Muscovado Sugar, Cornflour, Raising Agents: Potassium Hydrogen Carbonate, Disodium Diphosphate, Cinnamon Powder, Invert Sugar Syrup, Ground Mixed Spice (Coriander Seed, Cinnamon, Cloves, Nutmeg, Cassia), Flavouring","is_vegetarian":1,"is_vegan":0,"allergens":["Eggs","Cow Milk","Wheat"],"created_at":"2020-11-03T15:09:18.000000Z","updated_at":"2021-03-18T01:02:40.000000Z"},{"barcode":"07613037443301","name":"Milkybar White Chocolate Mini Eggs Sharing Bag 270g","price":null,"ingredients":"White Chocolate (Milk Powders (Whole and Skimmed), Sugar, Cocoa Butter, Vegetable Fats (Palm, Shea, Mango Kernel, Sal), Emulsifier (Sunflower Lecithin), Flavouring), Sugar, Glazing Agent (Carnauba Wax), Colour (Curcumin), White Chocolate contains Vegetable Fats in addition to Cocoa Butter","is_vegetarian":2,"is_vegan":0,"allergens":["Cow Milk","May contain Gluten","May contain Peanuts","May contain nuts","May contain Soya","Palm Oil"],"created_at":"2020-11-16T13:11:30.000000Z","updated_at":"2021-03-18T01:02:55.000000Z"},{"barcode":"05060469650098","name":"Gourmade Fish Pie 750g","price":null,"ingredients":"Potatoes (Sulphites) (40%), Milk, Prawns (6%) [Prawns (Crustacean), Water, Salt, Preservatives (E450, E451, E452)], Single Cream (Milk), Haddock (Fish) (5%), Dyed Smoked Haddock (5%) [Haddock (Fish), Salt, Turmeric], Salmon (Fish) (5%), Water, Unsalted Butter (Milk), Onions, Spinach, Full Cream Milk Powder, Modified Maize Starch, Wheat Flour (contains Calcium Carbonate, Iron, Niacin, Thiamin), Salt, Vegetable Bouillon [Sea Salt, Starch, Yeast Extract, Palm Fat, Vegetables (Parsnip, Onion, Parsley), Maltodextrin, Sugar, Spices (Celery Seed, Turmeric, Pepper, Garlic, Mace, Lovage, Nutmeg)], Parsley, Ground White Pepper","is_vegetarian":0,"is_vegan":0,"allergens":["Celery & Celeriac","Crustaceans","Fish","Cow Milk","Sulphites & Sulphur Dioxide","Wheat","Yeast","Palm Oil"],"created_at":"2020-11-18T12:22:12.000000Z","updated_at":"2021-03-18T01:02:45.000000Z"},{"barcode":"05000128739368","name":"Co-op Cheese & Chive Dip 200g","ingredients":"Mayonnaise (Rapeseed Oil, Water, White Wine Vinegar, Pasteurised Egg, Sugar, Salt, Stabilisers (Guar Gum, Xanthan Gum)), Reduced Fat Soured Cream (Milk) (28%), Vegetarian Mature Coloured Cheddar Cheese (Milk) (7%) (contains Colour (Annatto Norbixin)), Rapeseed Oil, Vegetarian Vintage Cheddar Cheese (Milk) (4%), Onion (2%), Chives (1%), Concentrated Lemon Juice, Cornflour, Sugar, Preservative (Potassium Sorbate), Potato Starch","is_vegetarian":1,"is_vegan":0,"allergens":["Eggs","Cow Milk"],"created_at":"2020-12-02T19:31:32.000000Z","updated_at":"2021-03-18T01:02:57.000000Z"}]'; /** * Enter solution below */ $data = json_decode($json); foreach($data as $d){ echo $d->name . $d->barcode . PHP_EOL; }
Write, Run & Share PHP code online using OneCompiler's PHP online compiler for free. It's one of the robust, feature-rich online compilers for PHP language, running on the latest version 7. Getting started with the OneCompiler's PHP compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as PHP
and start coding.
OneCompiler's PHP online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample PHP program which takes name as input and prints hello message with your name.
<?php
fscanf(STDIN, "%s\n", $name);
echo "Hello ".$name.".\n";
?>
PHP(Hypertext Preprocessor) is widely used server sripting language by Rasmus Lerdorf in the year 1994.
In PHP, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically. Variables are case-sensitive in PHP.
$variable_name = value;
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression){
//code
}
if(conditional-expression){
//code if condition is true
} else {
//code if condition is false
}
if(condition-expression1) {
//code if above condition is true
} elseif(condition-expression2){
//code if above condition is true
}
elseif(condition-expression3) {
//code if above condition is true
}
...
else {
//code if all the conditions are false
}
Switch is used to execute one set of statement from multiple conditions.
switch(conditional-expression) {
case value1:
// code if the above value is matched
break; // optional
case value2:
// code if the above value is matched
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
// you can use any of the below syntax
foreach ($array as $element-value) {
//code
}
foreach ($array as $key => $element-value) {
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
function function_name(parameters) {
//code
}
function_name (parameters)