0

When I encode the json data fetching from the mysql database, I get this:

[{"userid":"11","postid":"12"},{"userid":"13","postid":"12"},{"userid":"16","postid":"12"}]

And I am trying to get only the userid's like this: 11, 13, 16.

Decoding the json data gives me an output of: Array. And nothing else. When I var_dump this is my results:

array(3) { 
        [0]=> array(2) { 
                    ["userid"]=> string(2) "11" 
                    ["postid"]=> string(2) "12" 
                } 
        [1]=> array(2) { 
                    ["userid"]=> string(2) "13" 
                    ["postid"]=> string(2) "12" 
                } 
        [2]=> array(2) { 
                    ["userid"]=> string(2) "16" 
                    ["postid"]=> string(2) "12" 
                } 

So I know there is data present, it's just not showing for some reason.

Here is what my query looks like:

if(isset($_POST['postuserid'])){
    $uid = $_POST['postuserid'];

    $ssql = "SELECT * FROM foodid WHERE postid=$uid"; 
    $rresult = mysqli_query($db,$ssql); 
    while ($lrow = mysqli_fetch_assoc($rresult)){ 
        $ret[] = $lrow;
        $postjson = json_encode($ret);  
        $decode_postid = json_decode($postjson, true);
        $new_postid = $decode_postid;
    } 
    var_dump($new_postid);
    die($new_postid);
    // die($new_postid['userid']); is what I need along with all data userid fetched.

}

Is there a reason why this works when I encode json data, but not when I decode the json data?

1
  • 1
    $new_postid is being loaded in a while loop, but you are over writing it each time round the loop Commented Jul 10, 2018 at 21:55

2 Answers 2

1

Are you trying to get that array out of the die function? It can only take a string or int.

http://php.net/manual/en/function.exit.php

Your array is there... just do something like:

$ids = '';
foreach ($new_postid as $post) {
    $ids .= $post['userid'] . ',';
}
$ids = rtrim($ids,',');
die($ids);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this is what I'm looking for! Thank you so much!
1

Try mapping those results:

$new_postid = $decode_postid;
$new_postid = array_map(function($item){
    return $item['userid'];
}, $new_postid);

var_dump($new_postid);

Output

array(3) { [0]=> string(2) "11" 1=> string(2) "13" [2]=> string(2) "16" }

If you need to output as string, just use implode function

die(implode(',',$new_postid));

http://php.net/manual/en/function.array-map.php

4 Comments

Thank you, that's what I got, but I was expecting these results with die($new_postid): 11, 13, 16. Just the numbers
No problem. IMO it's better when using language construction. The code becomes cleaner and easier to mantain. Avoid substrings and loops if you have a better way to do.
I agree! Also with this I plan one convert the numbers into names. I.E. "Billy" with take the place of "11". I have to think of some way to do that with php query tables.
I have one more question, say If i wanted the postid as well. Would I do the same thing here and just change the variable names?

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.