-1

JSON decode :

$test=json_decode($tableData,TRUE);

The result of this was:

 [{
    "IngredientName": "Sunflower",
    "Quantity": "6",
    "Free_Quantity": "0",
    "Rate": "6"
}, {
    "IngredientName": "ganapathi",
    "Quantity": "6",
    "Free_Quantity": "0",
    "Rate": "6"
}]

How do I access the IngredientName from each element of the result array, using a for..each loop?

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
mul1103
  • 11
  • 6

5 Answers5

0

Try this:

$test=json_decode($tableData);
foreach($test as $arr) {
  echo $arr->IngredientName."<br />";
}
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
0

The result of decoding a JSON is an array of arrays. You can iterate through them the following way:

$test=json_decode($tableData,TRUE);
foreach($test as $ingredient) {
  echo $ingredient['IngredientName'] . '<br />';
}
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
0

This might be a working approach:

<?php
$encoded = '[{"IngredientName":"Sunflower","Quantity":"6","Free_Quantity":"0","Rate":"6"},{"IngredientName":"ganapathi","Quantity":"6","Free_Quantity":"0","Rate":"6"}]';
$decoded = json_decode($encoded);

foreach ($decoded as $object) {
  var_dump($object->IngredientName);
}

The obvious output is:

string(9) "Sunflower"
string(9) "ganapathi"
arkascha
  • 41,620
  • 7
  • 58
  • 90
0

Try something like this

foreach($tableData as $value){
echo json_decode($value['IngredientName']);
}
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
0
$array=json_decode($tableData,TRUE);  
foreach ($array as $key => $value) {
     echo $value['IngredientName'] . '<br />';
}
kiran gadhvi
  • 228
  • 2
  • 16