注意:未定义的偏移:4在php中循环遍历数组

时间:2014-12-08 21:54:49

标签: php csv

我已将csv转换为数组:

    Array
(
    [0] => Array
        (
            [0] => ADAMS #221
            [1] => ADAMS #221|SQUIRE||PENNIMAN||ELLIS|SMITH|CLARK|PUTNEY|ELDRIDGE|LONGLEY|AVERY
            [2] => Somersetshire, England|Braintree|Braintree, Medfield|Medfield|Newbury Medfield||Hawley|Charlemont|Charlemont|Devonshire, Eng||Newbury Andover
            [3] => 1640|||1652||1678|1708|1728|1791|1812|1847|1895
            [4] => John ADAMS b/d Somersetshire Eng; m. Agnes ___. [per Savage:  John, a tailor, was in Newbury, MA, by 1640.]  |Henry ADAMS b. Somerset, Eng. abt 1583; orig. proprietor of Braintree;  m. Eng., 1609, Edith SQUIRE; d. Braintree 1646.  Ancestor of Pres. John Adams.|Edward ADAMS of Medfield, b. Kingweston/Somerset, Eng. 1629; m. 1652 in MA, Lydia PENNIMAN. Edward was selectman and Rep in first Gen Courts. In Braintree by 1689.|Jonathan ADAMS b. Medfield 1655; m. 1678 Mary ELLIS; d. Medfield 1718.|Mary ADAMS b. Medfield 1681; m. 1708 Henry2 SMITH; he d. Medfield 1743.|[That couple had dau Mary SMITH b. Medfield 1709; m. 1728 David CLARK; d. Medfield 1770. |Then their son John CLARK b. CT abt 1736; m. Sarah ___; d. Goshen 1822; |then their dau Mary/Molly CLARK m. Goshen 1791, Ebenezer PUTNEY.|Mary/Molly and Ebenezer had dau Sarah/Sally PUTNEY b. Ashfield 1792; m. 1812 Levi H. ELDRIDGE; res Hawley. |This couple had Roswell H. ELDRIDGE, who m. 1847 Eliza Hawks LONGLEY; d. Charlemont 1891. |These were the parents of Henrietta Maria ELDRIIDGE, who m. 1895 Oscar Carter AVERY.]|See ADAMS GENEALOGY at CTHS.    GMN 10:2; 10:3; 2:3; 3:13; GMN 304
        )

    [1] => Array
        (
            [0] => ADAMS #120
            [1] => ADAMS #120|SQUIRE||PENNIMAN||ELLIS|SMITH|CLARK|PUTNEY|ELDRIDGE|LONGLEY|AVERY
            [2] => |Braintree|Braintree, Medfield|Medfield|Newbury Medfield||Hawley|Charlemont|Charlemont|Devonshire, Eng||Newbury Andover
            [3] => --|||1652||1678|1708|1728|1791|1812|1847|1895
            [4] => Peter ADAMS b. Eng.; d. Ottery St. Mary/Devon, Eng.  |Henry ADAMS b. Somerset, Eng. abt 1583; orig. proprietor of Braintree;  m. Eng., 1609, Edith SQUIRE; d. Braintree 1646.  Ancestor of Pres. John Adams.|Edward ADAMS of Medfield, b. Kingweston/Somerset, Eng. 1629; m. 1652 in MA, Lydia PENNIMAN. Edward was selectman and Rep in first Gen Courts. In Braintree by 1689.|Jonathan ADAMS b. Medfield 1655; m. 1678 Mary ELLIS; d. Medfield 1718.|Mary ADAMS b. Medfield 1681; m. 1708 Henry2 SMITH; he d. Medfield 1743.|[That couple had dau Mary SMITH b. Medfield 1709; m. 1728 David CLARK; d. Medfield 1770. |Then their son John CLARK b. CT abt 1736; m. Sarah ___; d. Goshen 1822; |then their dau Mary/Molly CLARK m. Goshen 1791, Ebenezer PUTNEY.|Mary/Molly and Ebenezer had dau Sarah/Sally PUTNEY b. Ashfield 1792; m. 1812 Levi H. ELDRIDGE; res Hawley. |This couple had Roswell H. ELDRIDGE, who m. 1847 Eliza Hawks LONGLEY; d. Charlemont 1891. |These were the parents of Henrietta Maria ELDRIIDGE, who m. 1895 Oscar Carter AVERY.]|See ADAMS GENEALOGY at CTHS.    GMN 10:2; 10:3; 2:3; 3:13; GMN 304
        )

    [2] => Array
        (
            [0] => AINSWORTH
            [1] => AINSWORTH|SQUIRE||PENNIMAN||ELLIS|SMITH|CLARK|PUTNEY|ELDRIDGE|LONGLEY|AVERY|ADAMS #120||WILMOTT|PHELPS
            [2] => Eng.; Boston|Braintree|Braintree, Medfield|Medfield|Newbury Medfield||Hawley|Charlemont|Charlemont|Devonshire, Eng||Newbury Andover||Westford|Charlestown NH|Groton, MA
            [3] => |||1652||1678|1708|1728|1791|1812|1847|1895|--||By 1682|Abt 1660
            [4] => Anchor AINSWORTH of Boston.|Henry ADAMS b. Somerset, Eng. abt 1583; orig. proprietor of Braintree;  m. Eng., 1609, Edith SQUIRE; d. Braintree 1646.  Ancestor of Pres. John Adams.|Edward ADAMS of Medfield, b. Kingweston/Somerset, Eng. 1629; m. 1652 in MA, Lydia PENNIMAN. Edward was selectman and Rep in first Gen Courts. In Braintree by 1689.|Jonathan ADAMS b. Medfield 1655; m. 1678 Mary ELLIS; d. Medfield 1718.|Mary ADAMS b. Medfield 1681; m. 1708 Henry2 SMITH; he d. Medfield 1743.|[That couple had dau Mary SMITH b. Medfield 1709; m. 1728 David CLARK; d. Medfield 1770. |Then their son John CLARK b. CT abt 1736; m. Sarah ___; d. Goshen 1822; |then their dau Mary/Molly CLARK m. Goshen 1791, Ebenezer PUTNEY.|Mary/Molly and Ebenezer had dau Sarah/Sally PUTNEY b. Ashfield 1792; m. 1812 Levi H. ELDRIDGE; res Hawley. |This couple had Roswell H. ELDRIDGE, who m. 1847 Eliza Hawks LONGLEY; d. Charlemont 1891. |These were the parents of Henrietta Maria ELDRIIDGE, who m. 1895 Oscar Carter AVERY.]|See ADAMS GENEALOGY at CTHS.    GMN 10:2; 10:3; 2:3; 3:13; GMN 304|Peter ADAMS b. Eng.; d. Ottery St. Mary/Devon, Eng.  |Robert ADAMS b. Devonshire, Eng. 1602; m. Eng., Eleanor WILMOTT; d. Newbury, MA 1682.|Elizabeth ADAMS b. 1641/2; m. MA, Edward PHELPS of Andover. Edward d. Andover 1689. (Their son|John b. Newbury 1657, was k by Inds at Scarborough 1677.)
        )

循环时,我得到通知:未定义的偏移量:(1,2,3,4):

    public function arrayToMySQL($line_of_text){ $length = array();
    if ($line_of_text[0]) {
        $arraySize=sizeof($line_of_text);
        for($i=0; $i<$arraySize; $i++){
            for($g=0; $g<4; $g++)
            if( strlen($line_of_text[$i][$g])>$length[$g]){$length[$g]=strlen($line_of_text[$i][$g]);}
    }}}}

任何想法?

1 个答案:

答案 0 :(得分:0)

您的$length在开始时已初始化,但在if条件内,它仍然不包含任何元素:

if( strlen($line_of_text[$i][$g])>$length[$g])

考虑循环中的第一次迭代。 $length在函数开头是空的,但如果满足前面的条件,它只会增长 。由于条件导致错误($length[$g]$length中的任何其他索引在此时未定义),因此数组将永远不会增长。

一个解决方案是在进入循环之前将您感兴趣的数组元素初始化为0:

$length = [ 0, 0, 0, 0 ];

$length中只有四个元素(...根据您的代码......),所以请尝试将它们设置为这样,看看它是否清除了您的错误。