Forum Moderators: coopster

Message Too Old, No Replies

Quick way to appen data onto each array value

         

erikcw

1:00 am on Jun 22, 2004 (gmt 0)

10+ Year Member



Is there a php function that the will append a string to the end of (or beginning) of each value in an array?

Example:

I have an array that has a list of cities. I would like to add the state to each of the cities in the array. I know I can use a loop to do this, but if there is a pre-built function, it would be good to know about...

Thanks!

jatar_k

1:13 am on Jun 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I don't think there is a prebuilt function but this is the place to look PHP String Functions [ca2.php.net]

you could use a foreach but it depends on what you are appending

is the array of states in an array with the same number of elements?
where are the states coming from?
if they are from db you could select them and put them into the array at the same time as everything else.

erikcw

1:17 am on Jun 22, 2004 (gmt 0)

10+ Year Member



The array of states is predefined (50 states). I'm going to search the string functions, but if I don't find anything I will just use a loop.

Thanks!

jatar_k

1:23 am on Jun 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so will you have to compare each one in the loop to figure out what goes where?

erikcw

1:31 am on Jun 22, 2004 (gmt 0)

10+ Year Member



$term

$array //with 50 US States

LOOP FOR 50

$array[item1] = $term . " " . $array[item1];

$i++

END LOOP

That is the basic logic of what I am going to do with the loop if I can't find an array function that does it already.

jatar_k

1:45 am on Jun 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I guess array functions would be better
[ca3.php.net...]

hehe, didn't realize I got confused ;)

I can't really think of one in particular that might work, the loop should be fine.

coopster

3:39 pm on Jun 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Rather than create a loop you may want to create your function then use array_walk [php.net].