Thursday, 5 September 2013

How to collapse (or clean) a given array, filtering some results in PHP for a further SQL search

How to collapse (or clean) a given array, filtering some results in PHP
for a further SQL search

I'm quite hesitant to ask here, but today I'm quite overwhelmed about a
problem I'm facing with arrays.
Before anything, I'd like to note that I'm a not a programmer nor engineer
(I come from design branch and self-taught).
But today I've been stuck for hours and I can't figure out how to get out
of the loop. I know it's kinda embarrassing, but I'd prefer answers of the
style "Explain it like I'm five".
I'm also kinda new to MVC architecture (as well as OOP), and although I
don't catch up at 100%, I've been making slow but steady progress building
the site.
So when the search form is sent from view to the controller, the
controller sends back to the model an array structured this way:
Array
(
[kind_ID] => 0
[type_ID] => 1
[city_ID] => 0
[address] => foo
[number] =>
[price] =>
// ... and so on (more fields with blank, zero or some data)
The problem is, I have no idea how to rebuild, or clean the array to
discard all elements which are left blank, or zero, (or are they null?);
maintaining all non-zero values and over everything, same indexes, as
indexes are needed for a further correct query when using db->like()
function.
I've read carefully all PHP documentation, and I haven't found any
function to search 'something' in an array, and unsetting it. I've tried
several array functions, but can't find any that does what I want.
Going to the point:
// I'd like to convert this array:
Array
{
[kind_ID] => 0
[type_ID] => 1
[city_ID] => 0
[address] => foo
[number] =>
[price] => 25000
...
// To this one
Array
{
[type_ID] => 1
[address] => foo
[price] => 25000
...
I hope I've been clear enough, and of course, tell me if I should need to
give it a different approach.
I've been playing with:
array_keys()
unset()
But also, I don't have much idea how to loop to the end of an array, or
how to build a loop based on that.
Thanks for your time, and thumbs up if you try to give me any hint. Any
help will be much appreciated!
Cheers,

No comments:

Post a Comment