Upkar
Upkar

Reputation: 31

How to push values into an array in PHP

Consider:

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

    // Splitting string ore.
    $output = preg_split("/ ( |\n) /", $ore);

    // Entering even value of array output to user and odd to allotted.
    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(allotted, $output[$x]); // Trying to put value in allotted.
        }
    }
?>

Upvotes: 1

Views: 1271

Answers (7)

Vipin Kumar KM
Vipin Kumar KM

Reputation: 366

Or you can use something like this

$user[] = $output[$x]
$allotted[] = $output[$x]

Upvotes: 1

Sharma Vikram
Sharma Vikram

Reputation: 2470

Try this code:

$ore = "piece1 piece2 piece3 piece4 piece5 piece6";
$user = array();
$allotted = 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($allotted, $output[$x]); // Trying to put value in allotted.
    }
}
echo '<pre>';
print_r($user);

Upvotes: 1

rasah_dipikir_jerojero
rasah_dipikir_jerojero

Reputation: 175

Check your user and allotted variable. They should be 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 allotted.
        }
    }
?>

Upvotes: 0

Kevin
Kevin

Reputation: 41885

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

Upvotes: 1

Venkata Krishna
Venkata Krishna

Reputation: 1776

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

Upvotes: 0

Simon Brahan
Simon Brahan

Reputation: 2076

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.

Upvotes: 0

Robert Bengtsson
Robert Bengtsson

Reputation: 194

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.

Upvotes: 2

Related Questions