-1

I am trying to get the following output that will produce the following json

[{"description":"name","errorcode":777},{"description":"Department","errorcode":"yyy"}]

PHP

if(empty($name)){        
    $errordesc[] = array('description' => 'name','errorcode' =>  777);           
}
else if(empty($email)){
    $errordesc[] = array('description' => 'Department','errorcode' =>  "yyy");
}

when I do

 echo json_encode($errordesc);

it gives output as

[{"description":"name","errorcode":-2}]

The problem is in the php array.Should I use array_push? Please advice.Thanks in advance.

3

3 Answers 3

2

The is a syntax error in your code

else if(empty($email){<-----here

else if(empty($email)){<-----must be like this 

Seeing your code actually i think the array contains only one item ,that is why you get that out put array push is not required just correct your logic.

 $errordesc[] = array('description' => 'name1','errorcode' =>  1); 
 $errordesc[] = array('description' => 'name2','errorcode' =>  2); 

echo json_encode($errordesc);

you will get json string with two items

[{"description":"name1","errorcode":1},{"description":"name2","errorcode":2}]
Sign up to request clarification or add additional context in comments.

2 Comments

That is almost certainly just a copying error, since the code with a missing ) wouldn't even run.
hmm...I have corrected the missing bracket but still getting only one item...strange.
2

Using else means you'll just get one or the other entry in your array. If you want them both, try:

if(empty($name)){        
    $errordesc[] = array('description' => 'name','errorcode' =>  777);           
}
if(empty($email){
    $errordesc[] = array('description' => 'Department','errorcode' =>  "yyy");
}

I'm not sure why the error for a missing email says Department rather than email, but that's a different problem.

3 Comments

I think he's made lots of SO copying mistakes -- there's no -2 in the code, yet it's in the output.
ya i either he has not shown the full code or he is assuming the output
Thanks barmar.The problem was in the if else :)
1

Your code currently has the following structure:

if (condition) {
    # code...
} 
elseif (condition) {
    # code...
}

The code inside this block will get executed only if the first if condition evaluates to FALSE. The JSON output you're getting suggests that only one of the conditional blocks is being considered.

If you want to do both the checks, try using the following structure:

if (condition) {
    # code...
} 
if (condition) {
    # code...
}

With your code, it should look like:

if(empty($name)){        
    $errordesc[] = array('description' => 'name','errorcode' =>  777);           
}
if(empty($email)) {
    $errordesc[] = array('description' => 'Department','errorcode' =>  "yyy");
}

Demo!

Comments

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.