1

I am having trouble trying to convert null values to empty strings with the results of my json_encode:

    if ($uresult->num_rows >0) {


 while($urow = $uresult->fetch_assoc()) {


 $rresult = mysqli_query($con,"SELECT * FROM allid WHERE postid='$oldid' AND spaceid='$newid'");
 $lrow = mysqli_fetch_assoc($rresult);
 $tem = $lrow['postid'];

 $ujson = json_encode($tem);
 echo $ujson;
 }

} else {
}

Here is the result of the $ujson:

"10"nullnullnullnull"25"

I looked up answers and I got this answer:

array_walk($tem,function(&$item){$item=strval($item);});

Doing this caused me to get this error:

Warning: array_walk() expects parameter 1 to be array, string given

Is this the correct way to convert a null to an empty string, if so, what am I doing wrong?

1
  • it already says string given, it means $tem is a string Commented Jun 1, 2018 at 1:18

2 Answers 2

4

Try this way -

//Array
$array = ["id" => 1, "name" => "Rohit Suthar", "city" => null, "mobile" => null];

//Convert null value to empty string 
array_walk_recursive($array,function(&$item){$item=strval($item);});

echo json_encode($array);
Sign up to request clarification or add additional context in comments.

Comments

3

Like what I've said in the comments, it says $tem is a string. array_walk needs an array for it to work properly.

Anyway, if you intend null values to turn into empty string '', just array_map with strval the array you got, so that it applies strval each element in the array. Then finally, encode it.

Here's the general idea:

$lrow = array_map('strval', $lrow);

Looking more in depth, you should put all items inside a container first, and then finally encode them. Don't encode each batch. Regarding the $tem's, then you wouldn't need array_walk, just a ternary operator:

$post_ids = array();
if ($uresult->num_rows > 0) {
    while ($urow = $uresult->fetch_assoc()) {
        $rresult = mysqli_query($con,"SELECT * FROM allid WHERE postid='$oldid' AND spaceid='$newid'");
        $lrow = mysqli_fetch_assoc($rresult);
        $tem = !is_null($lrow['postid']) ? $lrow['postid'] : '';
        $post_ids[] = $tem;
    }
}

echo json_encode($post_ids);

Sidenote: As much as possible, avoid using a while loop, and inside it another query under it (imagine having a thousand rows; it will query a thousand times too). Use JOIN statements instead.

6 Comments

Ok I did this and got this error: array_map(): Argument #2 should be an array
@LaneyWilliams you apply it into an array, anyways, just check out the edit, it should show you in the right direction
@Darren looking at the question as a whole, i think the OP doesn't need an array_walk or array_map at all.
@Ghost Just did haha, it's early here and I haven't had my coffee yet. Your edit sums up nicely ;-)
@Darren coffee works wonders, my brain never work without it. :D
|

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.