Output Function results to an array

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
User avatar
DanMan3395
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Feb 13, 2018 12:39 pm
Location: Minneapolis, MN

Mon Feb 19, 2018 2:06 pm

Code: Select all

for ($numbers1 = 1; $numbers1 <= 150; ++$numbers1)
  {
       echo "the number " . $numbers1;
       echo "<br>";
  }
------------

Code: Select all

$paper = array("Copier", "Inkjet", "Laser", "Photo");
  $j = 0;

  foreach($paper as $item)
  {
    echo "$j: $item<br>";
    ++$j;
  }
how do i put the results of the first code example through the second code example?
Dan

User avatar
DanMan3395
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Feb 13, 2018 12:39 pm
Location: Minneapolis, MN

Mon Feb 19, 2018 6:12 pm

actually if someone could just show me the syntax for outputting the results of a loop into an array in general that would help.
Dan

chorn
php-forum Fan User
php-forum Fan User
Posts: 559
Joined: Fri Apr 01, 2016 2:18 am

Mon Feb 19, 2018 10:29 pm

use array_push() function

User avatar
DanMan3395
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Feb 13, 2018 12:39 pm
Location: Minneapolis, MN

Tue Feb 20, 2018 1:38 pm

I tried that but I can't seem to get the syntax right.

I tried this:

Code: Select all

for ($numbers1 = 1; $numbers1 <= 150; ++$numbers1)
  {
       echo "the number " . $numbers1;
       echo "<br>";
  }

<?php
$Push=array(numbers1);
array_push($Push);
print_r($a);
?> 
This of course just barfs errors. I need the output of the for loop to somehow actually fill the array. I think there is a build in PHP array type for it but I am struggling to find it or work the proper syntax to make anything work for it.
Dan

User avatar
DanMan3395
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Feb 13, 2018 12:39 pm
Location: Minneapolis, MN

Tue Feb 20, 2018 2:40 pm

Got IT! thanks for the help all!

Code: Select all

$numbers_array = array();

for ($numbers2 = 1; $numbers2 <= 150; ++$numbers2)
  {
       array_push($numbers_array,$numbers2);
  }

print_r($numbers_array);
Dan

chorn
php-forum Fan User
php-forum Fan User
Posts: 559
Joined: Fri Apr 01, 2016 2:18 am

Tue Feb 20, 2018 10:51 pm

you could use range() for this.

User avatar
benanamen
New php-forum User
New php-forum User
Posts: 54
Joined: Mon Oct 16, 2017 1:06 pm

Wed Feb 21, 2018 11:55 am

What is the real problem you're trying to solve? I don't mean your attempt at solving it. This sounds like an XY problem.
The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

User avatar
DanMan3395
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Feb 13, 2018 12:39 pm
Location: Minneapolis, MN

Fri Feb 23, 2018 2:18 pm

I actually ended up using this instead of the push. I am not solving a specific problem, just trying to understant how to feed array's with the output of other data streams, such as a simple loop. Still trying to get used to PHP syntax.

Code: Select all

$list = array();

for ($numbers1 = 1; $numbers1 <= 150; ++$numbers1)
{

    $value = "the number " . $numbers1 . "<br/>\n";
	

    $list[] = $value;
}
	print_r($list);

unset ($value, $numbers1);


                foreach ($list as $value) {
                    echo $value;
                }
Dan

Post Reply