<?php
$ore = "piece1 piece2 piece3 piece4 piece5 piece6";
$user = array();
$alotted = array();
//splitting string ore.
$output = preg_split( "/ ( |\n) /", $ore );
//entering even value of array output to user and odd to alotted.
for ($x = 0; $x < sizeof($output); $x++)
{
if ($x % 2 == 0)
{
array_push(user,$output[$x]); //trying to put values in array user.
}
else
{
array_push(alotted,$output[$x]);//trying to put value in alotted.
}
}
?>
-
There is no array_push look at the answer in this post: http://stackoverflow.com/questions/2121548/how-to-push-both-value-and-key-into-array-php?rq=1 – jhedm Dec 11 '14 at 07:53
-
4There is `array_push`, read manuals before answering. @Upkar print_r your $output, check if there' re values – u_mulder Dec 11 '14 at 07:54
7 Answers
So firstly, you should look into explode for splitting strings by string: http://php.net/manual/en/function.explode.php
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
Secondly you could use the array[]-syntax to push new elements into an array:
$user[] = $array[$i];
To answer your questions, I think the main problem with your code is that you do not prefix the variables user and alotted with the $-char that PHP requires all variables to have.

- 194
- 3
- 12
First off, if this is not a typo, you forgot the $
signs on:
array_push($user,$output[$x]);
// ^ $
array_push($alotted,$output[$x]);
// ^
Then on your regex, remove the leading and trailing space:
$output = preg_split("/( |\n)/", $ore); // space or newline
// ^ ^ // no spaces
Refactored into this:
$ore = "piece1 piece2 piece3 piece4 piece5 piece6";
$output = preg_split("/( |\n)/", $ore );
// $output = explode(' ', $ore);
$user = $alotted = array();
for ($x = 0; $x < sizeof($output); $x++) {
($x % 2 == 0) ? array_push($user,$output[$x]) : array_push($alotted,$output[$x]);
}
I don't know why you have to use a regular expression on this, explode()
should suffice in this particular string example.
Code:
$ore = "piece1 piece2 piece3 piece4 piece5 piece6";
foreach(explode(' ', $ore) as $x => $piece) {
($x % 2 == 0) ? $user[] = $piece : $alotted[] = $piece;
}

- 41,694
- 12
- 53
- 70
try this code
$ore = "piece1 piece2 piece3 piece4 piece5 piece6";
$user = array();
$alotted = array();
$output=explode(" ", $ore);
print_r($output);
echo'<br>';
for ($x = 0; $x < sizeof($output); $x++)
{
if ($x % 2 == 0)
{
array_push($user,$output[$x]); //trying to put values in array user.
}
else
{
array_push($alotted,$output[$x]);//trying to put value in alotted.
}
}
echo '<pre>';
print_r($user);

- 2,440
- 6
- 23
- 46
or you can use something like this
$user[] = $output[$x]
$alloted[] = $output[$x]

- 356
- 5
- 17
check your user and alotted variable should using $
in php
<?php
for ($x = 0; $x < sizeof($output); $x++)
{
if ($x % 2 == 0)
{
array_push($user,$output[$x]); //trying to put values in array user.
}
else
{
array_push($alotted,$output[$x]);//trying to put value in alotted.
}
}
?>

- 155
- 5
Your regular expression is incorrect.
preg_split( "/\s+/", $ore );
will split the string correctly. Also, You need to prefix variable names with $
as given in the answers above.

- 2,016
- 1
- 14
- 22
<?php
$ore = "piece1 piece2 piece3 piece4 piece5 piece6";
$user = array();
$alotted = array();
//splitting string ore.
$output = preg_split( "/( |\n)/", $ore );
//entering even value of array output to user and odd to alotted.
for ($x = 0; $x < sizeof($output); $x++)
{
if ($x % 2 == 0)
{
array_push($user,$output[$x]); //trying to put values in array user.
}
else
{
array_push($alotted,$output[$x]);//trying to put value in alotted.
}
}
?>
Firstly, You missed $ for user and alotted in array_push. Also,for preg_split dont give space after and before /.
$output = preg_split( "/ ( |\n) /", $ore );
should be
$output = preg_split( "/( |\n)/", $ore );

- 1,768
- 2
- 14
- 21