0

Trying to get my array values into a usable variable. I would like to output these values into something readable to that I can use them in a SELECT call for my next statement. The final result I would like to be able to do something like echo $numresults and should look like this 9,8,6 My code as follows will out put the following using print_r as seen below.

Array ( [0] => 9 ) Array ( [0] => 8 ) Array ( [0] => 6 )

I feel like I am close but not sure how to get this array into a usable variable.

while ($rowa = mysql_fetch_assoc($resulta)) {
    if ($rowa['Count'] > 0) {
        $cids[]= $rowa['id'];
        display_id($rowa['id'], $levela + 1);
    } elseif ($rowa['Count']==0) {
        $cids[]= $rowa['id'];
    }

}
print_r($cids);
2
  • Let me know if you need help with my answer Commented Nov 11, 2017 at 16:59
  • Both are really close however when using the rtrim, substr($string, 0, -1); mb_substr($string, 0, -1); function to remove the last , in the output it will remove all the commas and bring the output from 9,8,6, to 986 Commented Nov 11, 2017 at 17:57

1 Answer 1

1
$cids = "";

while ($rowa = mysql_fetch_assoc($resulta)) {
    if ($rowa['Count'] > 0)  {
        $cids = $cids . $rowa['id'] . ",";
        display_id($rowa['id'], $levela + 1);
    } elseif ($rowa['Count']==0) {
        $cids = $rowa['id'];
    }
}

print_r($cids);
Sign up to request clarification or add additional context in comments.

2 Comments

Both are really close however when using the rtrim, substr($string, 0, -1); mb_substr($string, 0, -1); function to remove the last , in the output it will remove all the commas and bring the output from 9,8,6, to 986
Fixed it simply changed to $cids .",". $rowa['id']; from $cids . $rowa['id'] . ","; works perfect now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.