problem with aray_filter()[SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

problem with aray_filter()[SOLVED]

Post 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] =>
Last edited by mayanktalwar1988 on Tue Sep 29, 2009 7:15 am, edited 2 times in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: problem with aray_filter()

Post by requinix »

Code: Select all

return ;
As I said,
returns true or false if the URL should be kept
Read that one more time.
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: problem with aray_filter()

Post 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
jmaker
Forum Newbie
Posts: 16
Joined: Tue May 21, 2002 11:13 pm

Re: problem with aray_filter()

Post 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"));
 
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: problem with aray_filter()

Post 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
jmaker
Forum Newbie
Posts: 16
Joined: Tue May 21, 2002 11:13 pm

Re: problem with aray_filter()

Post 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?
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: problem with aray_filter()

Post by mayanktalwar1988 »

ya sorryy mine mistake urs fine mine was also fine gr8.
Post Reply