Check array values and change where needed.

Ask about a PHP problem here.
Post Reply
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Check array values and change where needed.

Post 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?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Check array values and change where needed.

Post 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.
}
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Check array values and change where needed.

Post 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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Check array values and change where needed.

Post by jacek »

$check is the value of the element, not an array of any kind :s
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Check array values and change where needed.

Post 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?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Check array values and change where needed.

Post 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.
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Check array values and change where needed.

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Check array values and change where needed.

Post 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.
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Check array values and change where needed.

Post by EcazS »

Oooooh! That is looking sexy!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Check array values and change where needed.

Post 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.
Image
Post Reply