0

hi want to loop through json below how can i do this using php

{
values: [
{
   nameValuePairs: {
   address: "0111",
   body: "Transaction cost, Ksh28.00.",
   date: "1571473093291",
}
},
{
   nameValuePairs: {
   address: "0101",
   body: "Transaction cost, Ksh0.00.",
   date: "1498823347530",
  }
  },
]
}

need the values of address,body and date in a foreach loop thanks in advance

Fredmax
  • 21
  • 6

1 Answers1

0

You can convert json to array by using json_decode and loop through the array

 $a =  json_decode($json, true);

Use Loop

 foreach($a['values'] as $v){
  echo $v['nameValuePairs']['address']."\n";
  echo $v['nameValuePairs']['body']."\n";
  echo $v['nameValuePairs']['date']."\n";
 }

See demo here : https://3v4l.org/QC06g

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
  • i alredy tried this $a = json_decode($json, true); foreach($a as $row){ $row["address"] } not getting result – Fredmax Oct 20 '19 at 11:22
  • @Fredmax you should have valid json format, check the link – Rakesh Jakhar Oct 20 '19 at 11:23
  • Hi @Rakesh thank you for your response, i was able to get result as per your link https://3v4l.org/IdrZk, kindly how do i loop through to get values of address,body and date i tried foreach($new_array as $row ) { echo $row["address"]; } – Fredmax Oct 20 '19 at 11:32
  • @Fredmax here https://3v4l.org/QC06g, all the information iteration – Rakesh Jakhar Oct 20 '19 at 11:35
  • @RakeshJakhar please [edit] your answer to include what you have in the comments. – Nick Oct 20 '19 at 11:37