Page 1 of 1

Check array values and change where needed.

Posted: Wed Oct 12, 2011 7:11 pm
by EcazS
Let's say I have an array like this,
[0] => cg/template
[1] => search
[2] => start
[3] => ending
now, what if I wanted to check each of these values and see if the value string started with "cg/"

I tried doing something like this,
foreach(array_values($checks[1]) as $checks) {
	if(stristr($checks, "cg/") === FALSE) {
		$checks = "cg/". $checks;
		echo $checks;
	}
}
Which is going to add "cg/" to the ones that don't have it. But if the value already has it then it's not going to echo it out along with the rest.

So basically, how would I check each value in an array, modify the values without "cg/" to include "cg/" and then print everything out?

Re: Check array values and change where needed.

Posted: Wed Oct 12, 2011 7:36 pm
by jacek
There is no need for array_values() there :s

You can use & to indicate that the variable should be a reference... an example is the best way to explain this.
foreach ($checks as &$check){
    // in here, what ever you do to $check will directly affect the value in the array.
}

Re: Check array values and change where needed.

Posted: Wed Oct 12, 2011 7:45 pm
by EcazS
But then using the stristr it's going to generate, PHP Warning: stristr() expects parameter 1 to be string, array given in blablabla :S

Re: Check array values and change where needed.

Posted: Wed Oct 12, 2011 7:57 pm
by jacek
$check is the value of the element, not an array of any kind :s

Re: Check array values and change where needed.

Posted: Wed Oct 12, 2011 8:02 pm
by EcazS
But...o_O waaat...

How would I then use the stristr on it, just doing it like the old example isn't working and I don't really know anything about the &-thing :S

//Edit
Never mind! I just did it wrong :lol:

I still got one little problem though,
foreach ($checks[1] as &$check){
		if(stristr($check, "http://") === FALSE) {
			$check = $dir ."cg/". $check;
			print_r($checks[1]);
		}
	}
This is going to do the right thing. However, let's say I have 4 values without "cg/" and one with it, it's then going to create four arrays and they're all looking like this,
Array
(
    [0] => cg/1
    [1] => cg/2
    [2] => 3
    [3] => 4
    [4] => 5
)
Array
(
    [0] => cg/1
    [1] => cg/2
    [2] => cg/3
    [3] => 4
    [4] => 5
)
and so on, so in the end they would all have "cg/" before.

//Edit again!
Alright, I fixed it by just doing the print_r OUTSIDE the foreach :D

Also, is there someway to actually echo/print out arrays like print_r but without viewing the source?

Re: Check array values and change where needed.

Posted: Wed Oct 12, 2011 9:25 pm
by jacek
jacek wrote:Also, is there someway to actually echo/print out arrays like print_r but without viewing the source?
Not really sure what you mean :? You could use implode I guess...
echo implode(', ', $words);
would produce a comma separated list of the words in the $words array.

Re: Check array values and change where needed.

Posted: Wed Oct 12, 2011 9:34 pm
by EcazS
Well, if you print_r and view the page source it looks like this.
Array(
     [0] => "zero"
     [1] => "one"
     [x] => "and so on"
)
but if you just view the page it's,
Array( [0] => "zero" [1] => "one" [x] => "and so on" )
But is there a way to actually output it like how it is in the source but on the page.
I guess you could use a foreach with structure it like that but I was just wondering if there were a "better" way.

Re: Check array values and change where needed.

Posted: Wed Oct 12, 2011 9:44 pm
by jacek
Ah, you can wrap it in <pre> tags
echo '<pre>', print_r($array, true), '</pre>';
The true as the second parameter just means that the result should be returned. So that it works with echo.

Re: Check array values and change where needed.

Posted: Wed Oct 12, 2011 9:54 pm
by EcazS
Oooooh! That is looking sexy!

Re: Check array values and change where needed.

Posted: Thu Oct 13, 2011 3:21 pm
by jacek
EcazS wrote:Oooooh! That is looking sexy!
Haha, yes it should be formatted well :D I normally just view the source because it means less typing.