未处理的数组值

时间:2014-08-05 23:08:05

标签: php mysql

我整理了一些代码来处理数组中的值。但是,似乎只打印出第一行。我可以验证数组的大小是247(通过在循环之前包括sizeof()检查)。

我有没有在这里看到的东西?我的$title变量的声明是:

$title = array();

在全球范围内。

function pushTitleChanges()
{
  global $title, $first_name, $last_name;
  $index = 0;

  print_r($title);
  if(is_array($title))
  {
    for($x =0; $x< sizeof($title);$x++)
    {
        echo "The index is " . $index .' '. "and title size is " . sizeof($title);
        echo "<br/>";

        $title = str_replace(array('.', ','), '' , $title[$index]);

        $statement = "UPDATE guest_list_guests
        SET guest_list_guests.title = '{$title}'
        WHERE guest_list_guests.first_name ='{$first_name[$index]}'
        AND guest_list_guests.last_name = '{$last_name[$index]}';";
        $index++;
    }
  }
}

1 个答案:

答案 0 :(得分:2)

您将整个标题数组设置为`str_replace(array('。',','),'',$ title [$ index]);'当你想要只改变那个索引时,这会用String覆盖整个数组。因此,那条线真的是:

 $title[$index] = str_replace(array('.', ','), '' , $title[$index]);

我还建议使用您创建的$x变量,它存储与$index相同的值,使$index完全不必要。