Welcome Guest, Not a member yet? Register   Sign In
How to know if a value is inside the array?
#1

[eluser]Sinclair[/eluser]
Hi,

I have an array like this one:
Code:
Array ( [0] => stdClass Object ( [id_distrito] => 01 ) [1] => stdClass Object ( [id_distrito] => 04 ) [2] => stdClass Object ( [id_distrito] => 11 ) [3] => stdClass Object ( [id_distrito] => 13 ) )

How can I know if for example the value '11' is inside this array? I have tested with in_array() but with no success.


Best Regards,
#2

[eluser]brianw1975[/eluser]
standard array functions won't work since the values of the array indexes are objects. if possible you might modify your array to create something to the effect of

Code:
Array ( [1] => stdClass Object ( [id_distrito] => 01 ) [4] => stdClass Object ( [id_distrito] => 04 ) [11] => stdClass Object ( [id_distrito] => 11 ) [13] => stdClass Object ( [id_distrito] => 13 ) )

thus the index is the id, at least that's probably the easiest way to do it.
#3

[eluser]Sinclair[/eluser]
Hi,

This is a result from a SQL Query.

Code:
return $query->result();

How can I change the return to give me the result in a array?


Best Regards,
#4

[eluser]Sinclair[/eluser]
Found how to:

Code:
<?php foreach ($result2 as $row200): ?>
<?php $rows_array[] = $row200->id_distrito ;  ?>
<?php endforeach; ?>
<?php echo implode(', ', $rows_array); ?>
#5

[eluser]Chris CMC[/eluser]
I think you could also convert to an array directly, like this:
Code:
return $query->result_array();
I had a similar problem yesterday, and it worked for me.
#6

[eluser]JoostV[/eluser]
It's all in the user guide, folks...




Theme © iAndrew 2016 - Forum software by © MyBB