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
?>
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()
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 ^^)
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
I was working with the gallery shortcode in the wordpress and found the output was [212,232,34,45,456,56,78] something like this. The following code helped me to make array of the list and then deal with each attachment id.
$list = '212,232,34,45,456,56,78';
$tag_array = explode(',', $list );
foreach ($tag_array as $key => $tag ) {
// do something with each list item.
}
$items = array("word1","word2","word3","word4");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.