user14691343
user14691343

Reputation:

PHP Array only shows the last pushed value

I made a php script where you have to add things to your bucketlist, but it isn't working properly. The script needs to output the bucketlist, but it only outputs the last value that is entered.

<?php
echo "How much activities would you like to add?\n";
$a = readline ();
$d = "What do you want to add to your bucket list?\n";

if (is_numeric($a)) {
    for ($x = 1; $x <= $a; $x++) {
        echo $d;
        $c = readline();
    }
} else {
    echo "'$a' isn't a number, try again.";
    exit;
}

$bucketlist = [$c];
array_push($bucketlist);

foreach ($bucketlist as $z) {
    echo "On your bucket list:\n";
    echo $z;
    echo "\n";
}
?>

I need it to output everything that is entered in readline $c

Upvotes: 2

Views: 399

Answers (3)

Mustafa Poya
Mustafa Poya

Reputation: 3027

on each iteration of for loop, $c = readline(); is reading a new value and without adding it to $bucketlist = []; you lose the value so you need to also add the value of $c on each iteration to the $bucketlist[] and you have to add $c value to $bucketlist[] inside the for loop:

  1. define $bucketlist = []; as an array
  2. use array_push($bucketlist, $c); or $bucketlist[] = $c in loop
echo "How much activities would you like to add?\n";
$a = readline ();
$d = "What do you want to add to your bucket list?\n";

$bucketlist = [];

if (is_numeric($a)) {
    for ($x = 1; $x <= $a; $x++) {
        echo $d;
        $c = readline();
        $bucketlist[] = $c;
        // array_push($bucketlist, $c);
    }
} else {
    echo "'$a' isn't a number, try again.";
    exit;
}

foreach ($bucketlist as $z) {
    echo "On your bucket list:\n";
    echo $z;
    echo "\n";
}
?>

Upvotes: 1

dopesky
dopesky

Reputation: 200

as mentioned by the above authors, $c = readline(); happens inside a loop so it is overridden with each iteration. Try this instead:

echo "How much activities would you like to add?\n";
$a = readline ();
$d = "What do you want to add to your bucket list?\n";
$bucketlist = [];
if (is_numeric($a)) {
    for ($x = 1; $x <= $a; $x++) {
        echo $d;
        $c = readline();
        array_push($bucketlist, $c);
    }
} else {
    echo "'$a' isn't a number, try again.";
    exit;
}

foreach ($bucketlist as $z) {
    echo "On your bucket list:\n";
    echo $z;
    echo "\n";
}
?>

Upvotes: 3

ADyson
ADyson

Reputation: 61849

$bucketlist = [$c]; happens outside your loop, so it only has access to the value of $c as it stood when the loop ended.

Move it inside your for loop and also make it say $bucketlist[] = $c; so it appends to bucketlist instead of overwriting it.

And you don't need your array_push line (which I think did nothing anyway, since you didn't specify anything to push into the array).

Also you should define $bucketlist as an empty array before you start looping.

echo "How much activities would you like to add?\n";
$a = readline ();
$d = "What do you want to add to your bucket list?\n";
$bucketlist = array();

if (is_numeric($a)) {
    for ($x = 1; $x <= $a; $x++) {
        echo $d;
        $c = readline();
        $bucketlist[] = $c;
    }
} else {
    echo "'$a' isn't a number, try again.";
    exit;
}

foreach ($bucketlist as $z) {
    echo "On your bucket list:\n";
    echo $z;
    echo "\n";
}

Upvotes: 2

Related Questions