以数组形式返回每个输出的php

以数组形式返回每个输出的php,php,arrays,foreach,Php,Arrays,Foreach,对于给定的foreach循环,使用echo显示输出 $output=array(); $counts=数组('45'、'55'、'75'、'95'); foreach($count计为$count){ $output=$count+10; echo$输出 } 它给 输出:556585105 但是我不想打印它,而是想将$output存储为数组变量 我想得到$output=array('55','65','85','105')因此,稍后我可以使用键值从$output中获取任何值。您可以修改原始数

对于给定的foreach循环,使用echo显示输出

$output=array();
$counts=数组('45'、'55'、'75'、'95');
foreach($count计为$count){
$output=$count+10;
echo$输出
}
它给

输出:556585105
但是我不想打印它,而是想将
$output
存储为数组变量


我想得到
$output=array('55','65','85','105')
因此,稍后我可以使用键值从
$output
中获取任何值。

您可以修改原始数组,如:

$counts = array('45','55','75','95');
foreach ($counts as &$count) {
    $count += 10;
}
print_r($counts);

您可以修改原始数组,如:

$counts = array('45','55','75','95');
foreach ($counts as &$count) {
    $count += 10;
}
print_r($counts);
试试这个

<?php

$output = array();
$counts = array('45','55','75','95');

foreach ($counts as $count) {   
    $output[] = $count + 10 ;
    print_r($output);
}
?>

您的错误是在必须执行
$output[]=
时执行了
$output=
。还请记住,您不能使用
echo
这样的变量,但您可以使用
print\u r
var\u dump

试试这个

<?php

$output = array();
$counts = array('45','55','75','95');

foreach ($counts as $count) {   
    $output[] = $count + 10 ;
    print_r($output);
}
?>


您的错误是在必须执行
$output[]=
时执行了
$output=
。还请记住,您不能
echo
这样的变量,但您可以使用
print\u r
var\u dump
,您需要更改赋值。您正在做的是覆盖$output。所以$output包含最后一个赋值

$output = array();
$counts = array('45','55','75','95');

foreach ($counts as $count) {

    $output[] = $count + 10 ; // change this        
}

你需要更改作业。您正在做的是覆盖$output。所以$output包含最后一个赋值

$output = array();
$counts = array('45','55','75','95');

foreach ($counts as $count) {

    $output[] = $count + 10 ; // change this        
}

您可以这样做:

   $array = array();
   $counts = array('45','55','75','95');

   foreach ($counts as $count) {
        $array[] = $count + 10 ;        
   }
   print_r($array); // here is your array

您可以这样做:

   $array = array();
   $counts = array('45','55','75','95');

   foreach ($counts as $count) {
        $array[] = $count + 10 ;        
   }
   print_r($array); // here is your array

您可以使用这个array_push()函数

<?php
$output = array();
$counts = array('45','55','75','95');
foreach ($counts as $count) {
array_push($output,$count + 10 );
}
print_r($output);
?>


array_push()函数是php的内置函数

您可以使用此array_push()函数

<?php
$output = array();
$counts = array('45','55','75','95');
foreach ($counts as $count) {
array_push($output,$count + 10 );
}
print_r($output);
?>



array_push()函数是php的内置函数

我使用整数加法只是一个例子;我的问题是将输出存储为数组变量,而不是打印每个值。@galexy此处$output在foreach中一次存储一个元素。所以,无论您存储在$output数组中的是什么,以后都可以检索;我的问题是将输出存储为数组变量,而不是打印每个值。@galexy此处$output在foreach中一次存储一个元素。因此,无论您存储在$output数组中的是什么,以后都可以检索。我希望将其存储为数组变量,而不是使用print\r或var\u dump打印每个值。整数的加法仅用于示例目的。是的。它们存储在$output变量中。打印只是让你看看它的样子Hanks@raygo,我明白了!它存储在获取打印之前。我不想使用print\r或var\u dump打印每个值,而是将其存储为数组变量。整数的加法仅用于示例目的。是的。它们存储在$output变量中。打印只是让你看看它的样子Hanks@raygo,我明白了!谢谢@glavic的帮助link你的答案对执行一些数学运算的整数值数组很有帮助,但是如何处理字符串数组呢。我想将输出作为数组变量返回,而不是打印。@Galaxy:同样的方法可以应用于字符串,你只需要
=
而不是
+=
,比如@SamV:ofc
array\u walk
,但是对于初学者来说,basic
foreach
是一个很好的例子。我是php方面的初学者,我想知道有没有办法将输出存储为数组而不是打印?我使用了return,但它破坏了操作。感谢@glavic的帮助link您的回答对执行一些数学运算的整数值数组很有帮助,但是如何处理字符串数组。我想将输出作为数组变量返回,而不是打印。@Galaxy:同样的方法可以应用于字符串,你只需要
=
而不是
+=
,比如@SamV:ofc
array\u walk
,但是对于初学者来说,basic
foreach
是一个很好的例子。我是php方面的初学者,我想知道有没有办法将输出存储为数组而不是打印?我使用了return,但它破坏了操作。