如何在php中将值推送到数组中

<?php
    $ore = "piece1 piece2 piece3 piece4 piece5 piece6";
    $user = array();
    $alotted = array();

    //splitting string ore.
    $output = preg_split( "/ ( |
) /", $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.
        }
    } 

?>

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("/( |
)/", $ore); // space or newline
                   //  ^     ^ // no spaces

Refactored into this:

$ore = "piece1 piece2 piece3 piece4 piece5 piece6";
$output = preg_split("/( |
)/", $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;
}

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.
        }
    }
?>

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.

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.

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);   

or you can use something like this

$user[] = $output[$x]
$alloted[] = $output[$x]
<?php
    $ore = "piece1 piece2 piece3 piece4 piece5 piece6";
    $user = array();
    $alotted = array();
//splitting string ore.

    $output = preg_split( "/( |
)/", $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( "/ ( |
) /", $ore );

should be

$output = preg_split( "/( |
)/", $ore );