Modifying PHP arrays without [ ] syntax

Want to chit chat about anything, do it here ... posting here won't increase your post count (or shouldn't!). Please do not post any "phpBB" specific topics here unless they do not fit into the category above. Do not post bug reports, feature or support requests!
Forum rules
Please do not post any "phpBB" specific topics here unless they do not fit into the category above.

Do not post bug reports, feature or support requests! No really... Do not post bug reports, feature or support requests! Doing so will make Bertie a very sad bear indeed. :(
Post Reply
SamG
Registered User
Posts: 1241
Joined: Fri Aug 31, 2001 6:35 pm

Modifying PHP arrays without [ ] syntax

Post by SamG »

I'm wondering about the wisdom of doing something like the following:

Code: Select all

  foreach ( $foo as $bar ) {
    $my_array = preg_replace("/($bar)/i", "<span class=\"underline\">\$1</span>", $my_array);
  }
where $my_array is an array. This works in that all instances of $bar are wrapped with <span> in all relevant elements of $my_array, but is this good coding? :oops:
"I hate trolls!" - Willow Ufgood

User avatar
A_Jelly_Doughnut
Registered User
Posts: 1780
Joined: Wed Jun 04, 2003 4:23 pm

Re: Modifying PHP arrays without [ ] syntax

Post by A_Jelly_Doughnut »

I would personally get $foo and $my_array to have the same keys, then use $key of $foo like so:

Code: Select all

foreach ($foo as $key => $bar)
{
      $my_array = str_replace($bar, '<span class="testclass">' . $bar . '</span>', $my_array[$key]);
}
However, both preg_replace and str_replace are array friendly. It certinally isn't wrong to do it the way you propoes.
A_Jelly_Doughnut

SamG
Registered User
Posts: 1241
Joined: Fri Aug 31, 2001 6:35 pm

Re: Modifying PHP arrays without [ ] syntax

Post by SamG »

Two things:

First, it has to be a case-preserved replace, so that's why there's a back reference.

Second, my real question is whether or not it's right to modify the contents of an array as shown in the example.

As an alternative, one could for loop through the array and address each element individually - $my_array[$i] - to do this in a way that might be more correct and readable. But if in PHP it's considered normal to examine and modify arrays in their entirety without the [ ] syntax, then there's no point in looping through all the elements of the array.
"I hate trolls!" - Willow Ufgood

User avatar
naderman
Consultant
Posts: 1727
Joined: Sun Jan 11, 2004 2:11 am
Location: Berlin, Germany
Contact:

Re: Modifying PHP arrays without [ ] syntax

Post by naderman »

Depends on the function you use. In this case it is correct because preg_replace returns an array when $subject is an array.
php.net/preg_replace wrote: mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit [, int &count]] )
naderman
Last edited by naderman on Fri Dec 02, 2005 11:50 pm, edited 1 time in total.

Uchiha Nick
Registered User
Posts: 397
Joined: Tue Jul 20, 2004 6:21 am
Location: Rotterdam, The Netherlands
Contact:

Re: Modifying PHP arrays without [ ] syntax

Post by Uchiha Nick »

so like.. can anyone give me a explanation of how a Array works? i have read tons of tutorials but all of them where vaque oO
Image

ILoserMan
Registered User
Posts: 39
Joined: Mon Oct 27, 2003 2:12 pm
Contact:

Re: Modifying PHP arrays without [ ] syntax

Post by ILoserMan »

an array holds many a groups of information in one easy to remeber variable.

Code: Select all

$my_array = array();

$my_array['first'] = "Cat";
$my_array['second'] = "foo";
$my_array['3'] = "bar";
$my_array['five'] = "none";
Where you name the array, than the name of the index, or the name of the storeage place. Like a name tag, or a place holder, than assign a value to it.

Code: Select all

echo $my_array['first'];
You get:
Output wrote: Cat

function.array


ILM

Uchiha Nick
Registered User
Posts: 397
Joined: Tue Jul 20, 2004 6:21 am
Location: Rotterdam, The Netherlands
Contact:

Re: Modifying PHP arrays without [ ] syntax

Post by Uchiha Nick »

thanks.. thats a lot more clearer :D
Image

Yoda_IRC
Registered User
Posts: 158
Joined: Tue Mar 01, 2005 10:19 pm

Re: Modifying PHP arrays without [ ] syntax

Post by Yoda_IRC »

This was the way it was always described to me:

You should know an ordinary variable is a place to store data. Image it is a box in the computers memory.

An array is just a group of boxes that can be accessed by the same name, however you use a key to say which box to look in. PHP allows string keys, but some languages have much tighter restrictions (i.e. Java permits only numeric keys starting from 0 and size is fixed before starting to fill array) However as PHP is loose typed it doesn't really bother with this

An ordinary variable:
myVar

Code: Select all

_______
| Bob |
|_____|
now an array

myArray

Code: Select all

  1         2         3       
_______   _______   _______   
| bob |   | sam |   | jan |   
|_____|   |_____|   |_____|   
You can refer to your set of boxes as a whole using "myArray" and to a specific part of it with "myArray[2]" for example. Arrays can ofcourse contian other arrays or objects. You can also have multi-dimensionall arrays.

Post Reply