English 中文(简体)
• 如何在阵列中添加标语和交换器
原标题:How to add object to array with foreach and switch, case

1. 甲型六氯环己烷

如下文所述,我通过一个阵列,根据最后的特性,即制造和开关,然后,我打造一个新阵列,并在这一新阵列中添加其最后特性,这样我就可以把这个新阵列和印刷起来,或重复这笔钱。

foreach ($answer as $value) {
  $last=substr($value,-1);
  $score=substr($value,-2,1);
  switch($last){
    case a:
      $a=array();
      array_push($a,$score);
      break;
    case b:
      $b=array();
      array_push($b,$score);
      break;
  }
}

In my html table I do this:

echo array_sum($a)

I can make it work if I add a digit like array_push($a, 2 ); but with the object it just overwrite the first one. what do I do wrong here?

问题回答

You re clearing your arrays on every pass. Define them before the foreach loop:

$a = array();
$b = array();

foreach ($answer as $value) {
  $last=substr($value,-1);
  $score=substr($value,-2,1);
  switch($last){
    case a:
      array_push($a,$score);
      break;
    case b:

      array_push($b,$score);
      break;
  }
}

你每当<条码>><<>> >> 字标贴上栏时,再造一个新阵列,这意味着你从每次时间开始,基本上放弃发言之前的所有时间。 <代码>$a = 阵列()和$b = 阵列(OUTSIDE the loop,因此,各阵列仅生成一次:

$a = array();
$b = array();
foreach (...) {
   ....
}

should fix up the problem





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签