Skip to content
Advertisement

How to write foreach loop for array in php

After submitting the form i am getting the array

$regData = {"student":"699","eposter":"99","exhibitor":"1199","Single-2-nights":"400"};

I need this look like this:

student : 699  
eposter : 99  
exhibitor : 1199  
Single-2-nights : 400  

I am writing this like:

<?php
foreach($regData as $key => $value){  
    $selected .= $key." :". $value."<br/>";  
}  
?>

Showing error

invalid argument passed to forech

Please help me to fix this error

Advertisement

Answer

It is a JSON String, You have to decode it first using Json_decode

    $regData = '{"student":"699","eposter":"99","exhibitor":"1199","Single-2-nights":"400"}';
    $arrayData = json_decode($regData,true);
    $selected ='';
    foreach($arrayData as $key => $value){
    $selected .= $key." :". $value."<br/>";
    }
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement