tags:

views:

151

answers:

4

word 1 word 2 word 3 word 4

And so on... How can I turn that into a php array, here will be a lot of items so I don't want to write them all out into an array manually and I know there has to be a simple way

I mean do I have to do

<?PHP
$items = array();

$items['word 1'];
$items['word 2'];
$items['word 3'];
$items['word 4'];
?>

UPDATE got it thanks

<?php

$items  = "word1 word2 word3 word4";
$items = explode(" ", $items);

echo $items[0]; // word1
echo $items[1]; // word2
?>
+2  A: 

If you mean:

word1 word2 word3

Then you want

$array = explode(' ', "word1 word2 word3");

If you actually mean "word 1 word 2 word 3", with numbers inbetween, then you'll need to get a little fancier with preg_split()

Frank Farmer
A: 

Hi,

If I understand the question, you have a set of variables, like this :

$word1 = 'a';
$word2 = 'b';
$word3 = 'c';
$word4 = 'd';
$word5 = 'e';
$word6 = 'f';
$word7 = 'g';
$word8 = 'h';

If yes, you could use PHP's Variable variables, like this :

$list = array();
for ($i = 1 ; $i <= 8 ; $i++) {
    $variable_name = 'word' . $i;
    $list[] = $$variable_name;
}

var_dump($list);

And you'd get, as output :

array
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)
  2 => string 'c' (length=1)
  3 => string 'd' (length=1)
  4 => string 'e' (length=1)
  5 => string 'f' (length=1)
  6 => string 'g' (length=1)
  7 => string 'h' (length=1)


If I didn't understand the question and you only have one string at first... You'll probably have to use explode to separate the words...


EDIT
(Well, I didn't understand the question, it seems... There were not as many examples given when I first answered -- sorry ^^)

Pascal MARTIN
+1  A: 

if you have all your words in a text file, one per line you can use the file function which will read the whole file into an array, one item per line

http://nz.php.net/manual/en/function.file.php

bumperbox
thats usefule thanks
jasondavis
A: 
$items = array("word1","word2","word3","word4");
Draemon