dongyun3335 2014-12-08 21:54
浏览 56
已采纳

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

I've converted a csv to an array:

    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.)
        )

When looping through it, I get Notice: Undefined offset: (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]);}
    }}}}

any ideas?

  • 写回答

1条回答 默认 最新

  • dongwei6457 2014-12-08 22:01
    关注

    Your $length becomes initialized at the start, but inside the if condition it still contains no elements:

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

    Consider the first iteration through your loop. $length is empty at the start of the function, but it only grows if the preceding condition is satisfied. Since the condition is causing an error ($length[$g] or any other index for that matter in $length is undefined at that point), then the array will never grow.

    One solution for you is to initialize the array elements you are interested in to 0 prior to entering the loops:

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

    There's only ever four elements (...according to your code...) that can ever exist in $length, so try just setting them like this and see if it clears your error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?