0

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

4
  • 3
    $regData looks more like JSON and as it stands, won't work as an array. If you had quotes round it, then try json_decode(). Commented Sep 23, 2020 at 12:22
  • Please provide your debugging attempts, and a full reproducable example. In the given snippet, $regData is undefined - where does it come from? Commented Sep 23, 2020 at 12:25
  • $regData = {"student":"699","eposter":"99","exhibitor":"1199","Single-2-nights":"400"}; isn't valid PHP. This can't be your real code. Please clarify. Is it actually a string like $regData = '{"student":"699","eposter":"99","exhibitor":"1199","Single-2-nights":"400"}';, perhaps? That would be valid. Then, as the first comment above says, you could decode it into an array / object and then use it easily in a loop. Commented Sep 23, 2020 at 12:26
  • Are looking for something like this ? sandbox.onlinephpfunctions.com/code/… Commented Sep 23, 2020 at 12:35

4 Answers 4

3

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/>";
    }
Sign up to request clarification or add additional context in comments.

Comments

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

will result an object

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
1
$regData = ["student" => "699",
            "eposter" => "99",
            "exhibitor" => "1199",
            "Single-2-nights" => "400"];

in php, array should be in square brackets

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

Foreach code is OK.

?>

2 Comments

In PHP, arrays also use => instead of :.
@executable the OP's data isn't valid PHP, so, no-one can use that. If you see the comments on the main thread, above, you'll note that people are asking OP to clarify if it's actually meant to be a JSON string, or what. But this example here would serve a a valid sample, assuming that the data in the original question then gets decoded to a valid associative array.
0

Hi Ganesh I Checked your code, your foreach loop syntax is fine but It's a better practice to write endforeach; at the end of loop + in your array replace : with =>

3 Comments

but It's a better practice to write endforeach; - where do you get this idea from?
That page only mentions endforeach once and nothing in there says it's best practice

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.