在数组php中打破一个数组

时间:2016-10-03 04:22:59

标签: javascript php jquery arrays

我有一个函数允许我为循环中的每个进程保存已删除的对象。 Fiddle here问题是我为每个进程创建一个数组并将已删除的项目存储在其中。我用来保存droped项目的功能:

<script>
var LISTOBJ = {
    saveList: function() {
        $(".leader").each(function() {
          var listCSV = [];
          $(this).find("li").each(function(){
              listCSV.push($(this).text());
          });
          var values = listCSV.join(', ');
          $(".output").append("<input type='hidden' name='leader[]' value='"+values+"' />");
          $("#output").append("<p>"+values+"</p>");
              console.debug(listCSV);
        });
    }
}
</script>

打印保存对象后得到的结果:

Array ( [0] => aaronjames,adelyn [1] => benny,bryanooi )

我想在数据库中存储名称并且能够执行此操作,但它存储了一个数组。所以我正在寻找一种方法来打破数组内部的数组,而不是存储一个完整的数组。 我有两张桌子:'project' and 'process'。我想在'process'表中插入名称。我能够这样做。但它仍然是一个数组。 这是我用来在数据库中存储接收值的函数。我正在生成项目编号并使用foreach为每个流程分配名称

function insertRecord(){
    global $holdcode,$holdtitle,$projLeader,$projChecker,$holdremark,$holdprocess,$holdnumber,$procLeader,$procChecker, $prodStuff;

    // To protect MySQL injection (more detail about MySQL injection)
    $holdcode = cleanInputData($holdcode);
    $holdtitle = cleanInputData($holdtitle);
    $projLeader = cleanInputData($projLeader);
    $projChecker = cleanInputData($projChecker);
    $holdremark = cleanInputData($holdremark);

    $holdcode = mysql_real_escape_string($holdcode);
    $holdtitle = mysql_real_escape_string($holdtitle);
    $projLeader = mysql_real_escape_string($projLeader);
    $projChecker = mysql_real_escape_string($projChecker);
    $holdremark = mysql_real_escape_string($holdremark);

    $result = getLastProjectNo();

    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }

    if(mysql_num_rows($result) == 0)
    {
        $year = date("y");
        $month = date("m");
        $number = sprintf("%04d", 1);
        $yearStr = strval($year);
        $monthStr = strval($month);
        $numberStr = strval($number);
        $projectNo = $yearStr . $monthStr . $numberStr;
    }

    else{   

        if ($row_last = mysql_fetch_array($result))
        {
            $row_last[0] = cleanOutputData($row_last[0]);
            $projectNo = $row_last[0];
            $extractedNumberStr = substr($projectNo, -4);
            $extractedYearMonthStr = substr($projectNo, 0, 4);

            $year = date("y");
            $month = date("m");
            $yearStr = strval($year);
            $monthStr = strval($month);
            $currentYearMonthStr = $yearStr . $monthStr;

            if($extractedYearMonthStr == $currentYearMonthStr)
            {
                $extractedNumber = intval($extractedNumberStr);
                $extractedNumber++;
                $extractedNumber = sprintf("%04d", $extractedNumber);
                $extractedNumberStr = strval($extractedNumber);
                $projectNo = $currentYearMonthStr . $extractedNumberStr;
            }

            else
            {
                $number = sprintf("%04d", 1);
                $projectNo = $currentYearMonthStr . $number;
            }
        }
    }

    mysql_set_charset('utf8');
    //query result
    $result = insertProject($projectNo,$_SESSION['login_user'],$holdcode,$holdtitle,$projLeader,$projChecker,$holdremark);

    // Check result
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }

    $result = getLastProjectNo();

    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }

    $result = getLastProjectNo();

    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }       

    if ($row_last = mysql_fetch_array($result))
    {
        $row_last[0] = cleanOutputData($row_last[0]);
        $projectNo = $row_last[0];
        if (is_array($holdprocess) || is_object($holdprocess))
        {
            foreach( $holdprocess as $code => $eq ) 
            {   
                $record = insertProcess($projectNo,$eq,$holdnumber[$code],$procLeader[$code],$procChecker[$code], $prodStuff[$code]);
                if (!$record) {
                    die('Invalid query: ' . mysql_error());
                }
            }               
        }
    }
}

我想要的是打破数组中的数组。例如我有Array ( [0] => aaronjames,adelyn [1] => benny,bryanooi )我需要将[0] => aaronjames,adelyn分解为允许我为表中的每一行存储每个名称的东西

1 个答案:

答案 0 :(得分:1)

让我告诉你一种读取数组值的方法:

    for($x = 0; $x < count($array_name); $x++) {
    echo $array_name[$x];
    echo "<br>";
}

如果您需要访问一组对象,请尝试使用

foreach($array_name as $obj){
$status = $obj->status;

}

如果从数组中收到组合字符串(如'aaronjames,adelyn'),则可以使用Php的'explode()'函数拆分组合字符串

<?php
$str = "aaronjames,adelyn";
$str_array=(explode(",",$str));
for($i=0;$i<count($str_array);$i++)
    echo $str_array[$i]."<br/>";
?>