Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I was trying to find a way on how to get the values from print_r(), I already typed-in the same scenario looking for the same problem and answers but only to find out answers that will only print the values and not the way of using it.

let say:

$scontain = "SELECT id FROM voting";
$qcontain = mysql_query($scontain);

$r_idarray = array();
while ($rcontain = mysql_fetch_array($qcontain))
{
 $r_idarray[] = $rcontain['id'];
}

here was the print_r():

 print_r($r_idarray);

the output of this if it will be printed:

 Array([0]==>1 [0]==>5)

What I want to do is to get the values like 1 and 5 and put it in a variable like let say $box and whenever i use $box it will contain 1 and 5 or more values.

my objective was to put $box in the if else condition like:

if(1 == $box)
 {
   echo "1 is equal to ".$box;
 }
 else
 {
   echo "1 is not equal to ".$box;
 }

in here $box will be able to give all it contains for comparison. Is it possible or are there any other way which will do the same. Thank you very much.

share|improve this question
1  
Plain PHP questions belong to Stack Overflow. – toscho Feb 1 at 6:21

closed as off topic by toscho Feb 1 at 6:21

Questions on WordPress Answers are expected to relate to WordPress within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

print_r() is used for displaying data. You can just use the variable instead. To check a single value is part of an array, use the in_array() PHP function:

$box = $r_idarray;

if ( in_array( 1, $box ) {
    // 1 is part of the $box array
}
else {
     // 1 is not part of the $box array
}
share|improve this answer
in_array was used for specific values right? but I would want to assume that I don't know the number of values contained in the $box. would array_search() be possible. Thank you for the great help. – user26856 Feb 1 at 5:54
is_array() is just saying: "If 1 is assigned to any item in the array, then return true." It doesn't have to be 1; it could be a string. It has nothing to do with the array length. – bungeshea Feb 1 at 5:56
ok, thank you for the time and idea. I'll try it. – user26856 Feb 1 at 6:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.