Forum Moderators: coopster

Message Too Old, No Replies

array filters and multi-dimensional arrays

         

klaichdog

4:06 pm on Apr 20, 2007 (gmt 0)

10+ Year Member



Hello I have the following array in which I need to return the values in [1] and [9] however I need to only include those values where [9] = V2.05L. I have read the manual on array_filter however I cannot get it to loop through to grab them all. Any help getting my head wrapped around this one would be helpful.

Here is the array which is stuffed into this variable:

$msft_stats

array
(
[0] => Array
(
[0] =>
[1] => AND
[2] => www.example.com
[3] => example.net
[4] =>
[5] =>
[6] => 1234
[7] => 408
[8] =>
[9] => V2.05L
[10] => none
[11] => 12/0
[12] => 192.168
[13] => 1.100
)

[1] => Array
(
[0] =>
[1] => AAND
[2] => www.example.com
[3] => example.net
[4] =>
[5] =>
[6] => 1234
[7] => 408
[8] =>
[9] => V2.25L
[10] => none
[11] => 12/0
[12] => 192.168
[13] => 1.100
)

[2] => Array
(
[0] =>
[1] => FND
[2] => www.example.com
[3] => example.net
[4] =>
[5] =>
[6] => 1234
[7] => 408
[8] =>
[9] => V2.05L
[10] => none
[11] => 12/0
[12] => 192.168
[13] => 1.100
)

[3] => Array
(
[0] =>
[1] => nND
[2] => www.example.com
[3] => example.net
[4] =>
[5] =>
[6] => 1234
[7] => 408
[8] =>
[9] => V3.0
[10] => none
[11] => 12/0
[12] => 192.168
[13] => 1.100
)

So I created this function to take out the V2.05L

function myfunction($v)
{
if ($v=="V2.05L")
{
return true;
}
return false;
}
print_r(array_filter($msft_stats, "myfunction"));

Which does what it is supposed to however only returns the first occurance. I need to return any that has the V2.05L.

Thanks for your help in advance!
Khris

[edited by: eelixduppy at 8:36 pm (utc) on April 20, 2007]
exemplified ip and domain

[edited by: coopster at 9:56 pm (utc) on April 20, 2007]
[edit reason] fixed array index in top of message [/edit]

cameraman

6:00 pm on Apr 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since $msft_stats is an array of arrays, you need to run each of its elements through the filter:
foreach($msft_stats as $one_msft_stat)
print_r(array_filter($one_msft_stat, "myfunction"));

klaichdog

6:31 pm on Apr 20, 2007 (gmt 0)

10+ Year Member



When I try and use that code I get what you see below. I understand the concept and have to assume each one below is a match to the V2.05L how to get the data out :

Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( )

cameraman

8:19 pm on Apr 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OIC I don't think I've ever used array_filter() [us.php.net] and I didn't go look it up earlier.
It returns a new array containing only the elements for which your callback returned true. So to actually see them you'd do something similar to:
foreach($msft_stats as $outer_idx => $one_msft_stat) {
$new_array = array_filter($one_msft_stat, "myfunction");
foreach($new_array as $idx => $value)
echo "array[$outer_idx][$idx] = $value<br />\n";
} // EndForEach array in msft_stats

coopster

10:28 pm on Apr 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, klaichdog.

You can loop over the multidimensional array and return only those secondary arrays where the value you search for is found.

$search = 'V2.05L'; 
foreach [php.net] ($msft_stats as $k => $v) {
if (array_keys [php.net]($v, $search)) {
$found[$k] = $v;
}
}
print '<pre>'; print_r($found); print '</pre>';

klaichdog

6:18 pm on Apr 23, 2007 (gmt 0)

10+ Year Member



Hi Coopster...I just tried your code and I return nothing no matter what I try and use as my search variable any ideas?

Thanks-
khris

klaichdog

6:20 pm on Apr 23, 2007 (gmt 0)

10+ Year Member



Opps I realize now that I have spaces after and before is there a way to do a wild card search with that search variable just in case they might be other characters in the furture to deal with?

Thanks-

coopster

3:15 am on Apr 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would loop through and run a regular expression against them at that point.

adb64

6:36 am on Apr 24, 2007 (gmt 0)

10+ Year Member



I've adapted the code of coopster to the following:

$search = 'V2.05L';
$index = 9;
foreach ($msft_stats as $k => $v) {
if (trim($v[$index]) == $search) {
$found[$k] = $v;
}
}
print '<pre>'; print_r($found); print '</pre>';

The function trim [php.net] will remove leading and trailing whitespace characters from the string