Let's say I have an array like this,
[syntax=text]
[0] => cg/template
[1] => search
[2] => start
[3] => ending
[/syntax]
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,
[syntax=php]
foreach(array_values($checks[1]) as $checks) {
if(stristr($checks, "cg/") === FALSE) {
$checks = "cg/". $checks;
echo $checks;
}
}
[/syntax]
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?
Check array values and change where needed.
Re: Check array values and change where needed.
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.
[syntax=php]foreach ($checks as &$check){
// in here, what ever you do to $check will directly affect the value in the array.
}[/syntax]
You can use & to indicate that the variable should be a reference... an example is the best way to explain this.
[syntax=php]foreach ($checks as &$check){
// in here, what ever you do to $check will directly affect the value in the array.
}[/syntax]
Re: Check array values and change where needed.
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.
$check is the value of the element, not an array of any kind :s
Re: Check array values and change where needed.
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![Laughing :lol:](./images/smilies/icon_lol.gif)
I still got one little problem though,
[syntax=php]
foreach ($checks[1] as &$check){
if(stristr($check, "http://") === FALSE) {
$check = $dir ."cg/". $check;
print_r($checks[1]);
}
}
[/syntax]
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,
[syntax=text]
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
)
[/syntax]
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![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
Also, is there someway to actually echo/print out arrays like print_r but without viewing the source?
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
![Laughing :lol:](./images/smilies/icon_lol.gif)
I still got one little problem though,
[syntax=php]
foreach ($checks[1] as &$check){
if(stristr($check, "http://") === FALSE) {
$check = $dir ."cg/". $check;
print_r($checks[1]);
}
}
[/syntax]
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,
[syntax=text]
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
)
[/syntax]
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
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
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.
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
![Confused :?](./images/smilies/icon_e_confused.gif)
[syntax=php]echo implode(', ', $words);[/syntax]
would produce a comma separated list of the words in the $words array.
Re: Check array values and change where needed.
Well, if you print_r and view the page source it looks like this.
[syntax=text]
Array(
[0] => "zero"
[1] => "one"
[x] => "and so on"
)
[/syntax]
but if you just view the page it's,
[syntax=text]
Array( [0] => "zero" [1] => "one" [x] => "and so on" )
[/syntax]
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.
[syntax=text]
Array(
[0] => "zero"
[1] => "one"
[x] => "and so on"
)
[/syntax]
but if you just view the page it's,
[syntax=text]
Array( [0] => "zero" [1] => "one" [x] => "and so on" )
[/syntax]
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.
Ah, you can wrap it in <pre> tags
[syntax=php]echo '<pre>', print_r($array, true), '</pre>';[/syntax]
The true as the second parameter just means that the result should be returned. So that it works with echo.
[syntax=php]echo '<pre>', print_r($array, true), '</pre>';[/syntax]
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.
Oooooh! That is looking sexy!
Re: Check array values and change where needed.
EcazS wrote:Oooooh! That is looking sexy!
Haha, yes it should be formatted well
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)