Page 1 of 1

problem with aray_filter()[SOLVED]

Posted: Mon Sep 28, 2009 4:13 pm
by mayanktalwar1988
in trying to solve apost here by a newbie on this link viewtopic.php?f=1&t=106810

i ttreid this code to give him as a example

Code: Select all

<?
function url($var)
{
if (strstr($var, “image”))
return true;
else 
return false;
}
$array1 = array("www.dev.com/hello.php", "www.dev.com/image/hello.jpg","www.dev.com/image/mayank.jpg");
 
print_r(array_filter($array1, "url"));
?>
but it only print this Array ( [0] => http://www.dev.com/hello.php [1] => http://www.dev.com/image/hello.jpg [2] => http://www.dev.com/image/mayank.jpg ) it sholud have printed out


Array ( [0] => http://www.dev.com/hello.php )


whre is the prob

[0] =>

Re: problem with aray_filter()

Posted: Mon Sep 28, 2009 4:37 pm
by requinix

Code: Select all

return ;
As I said,
returns true or false if the URL should be kept
Read that one more time.

Re: problem with aray_filter()

Posted: Mon Sep 28, 2009 4:44 pm
by mayanktalwar1988
hey that was just left out by mistake during copy paste now i edited it and corrected it...true is there in my script here on my pc
now tell what is the prob

Re: problem with aray_filter()

Posted: Mon Sep 28, 2009 5:07 pm
by jmaker
Let me understand this correctly. You only want links returned that do not have "image" in them? If that's the case then your return values are backwards.

From the php manual.
If the callback function returns true, the current value from $input is returned into the result array.
So from your code you are actually returning every link that has "image" in it. What you want is to return every value that doesn't have "image" in it.

Code: Select all

 
function url($var) {
    if(strstr($var, "image"))
        return false;
    else
        return true;
}
$array1 = array("www.dev.com/hello.php", "www.dev.com/image/hello.jpg","www.dev.com/image/mayank.jpg");
 
print_r(array_filter($array1, "url"));
 

Re: problem with aray_filter()

Posted: Mon Sep 28, 2009 9:54 pm
by mayanktalwar1988
did you try ur code it doing same thing try it please..<?php

Code: Select all

function url($var)
{
if (strstr($var, “image”))
return false;
else 
return true;
}
$array1 = array("www.dev.com/hello.php", "www.dev.com/image/hello.jpg","www.dev.com/image/mayank.jpg");
 
print_r(array_filter($array1, "url"));
?>
i have this code at home now which same as yours....no luck

Re: problem with aray_filter()

Posted: Mon Sep 28, 2009 10:13 pm
by jmaker
The code that I posted prints out this on my screen.

Code: Select all

 
Array
(
  [0] => http://www.dev.com/hello.php
)
 
Is that not what you wanted?

Re: problem with aray_filter()

Posted: Tue Sep 29, 2009 7:11 am
by mayanktalwar1988
ya sorryy mine mistake urs fine mine was also fine gr8.