数组的最后一个元素

时间:2019-05-18 19:00:54

标签: php arrays foreach

我正在尝试遍历数组以从中创建一个新数组。在运行此代码时尝试记录所有内容:

$this->writeToLog(print_r($this->assembledText, true), 'ass.log');
foreach ($this->assembledText as $paragraphsKey => $paragraphs) {
    $this->writeToLog("Paragraph Key:".$paragraphsKey.print_r($paragraphs, true), 'para.log'); 
    $i = 0;
    foreach ($paragraphs as $words) {
        $newText[$paragraphsKey][$i] = $words;
        $i++;
    }
}

在此之前,我正在记录文本,以下是到目前为止的内容:

ass.log给我这个:

[2019-05-18 20:32:38] Array
(
    [0] => Array
        (
            [0] => One
            [1] => thing
            [2] => was
            [3] => certain,
            [4] => that
            [5] => the
            [6] => white
            [7] => kitten
            [8] => had
            [9] => had
            [10] => nothing
            [11] => to
            [12] => do
            [13] => with
            [14] => it:
            [15] => —
            [16] => it
            [17] => was
            [18] => the
            [19] => black
            [20] => kitten’s
            [21] => fault
            [22] => entirely.
            [23] => For
            [24] => the
            [25] => white
            [26] => kitten
            [27] => had
            [28] => been
            [29] => having
            [30] => its
            [31] => face
            [32] => washed
            [33] => by
            [34] => the
            [35] => old
            [36] => cat
            [37] => for
            [38] => the
            [39] => last
            [40] => quarter
            [41] => of
            [42] => an
            [43] => hour
            [44] => (and
            [45] => bearing
            [46] => it
            [47] => pretty
            [48] => well,
            [49] => considering);
            [50] => so
            [51] => you
            [52] => see
            [53] => that
            [54] => it
            [55] => couldn’t
            [56] => have
            [57] => had
            [58] => any
            [59] => hand in
            [61] => the
            [62] => mischief.
            [63] => 
        )

    [1] => Array
        (
            [0] => 
            [1] => The
            [2] => way
            [3] => Dinah
            [4] => washed
            [5] => her
            [6] => children’s
            [7] => faces
            [8] => was
            [9] => this:
            [10] => first
            [11] => she
            [12] => held
            [13] => the
            [14] => poor
            [15] => thing
            [16] => down
            [17] => by
            [18] => its
            [19] => ear
            [20] => with
            [21] => one
            [22] => paw,
            [23] => and
            [24] => then
            [25] => with
            [26] => the
            [27] => other
            [28] => paw
            [29] => she
            [30] => rubbed
            [31] => its
            [32] => face
            [33] => all
            [34] => over,
            [35] => the
            [36] => wrong
            [37] => way,
            [38] => beginning
            [39] => at
            [40] => the
            [41] => nose:
            [42] => and
            [43] => just
            [44] => now,
            [45] => as
            [46] => I
            [47] => said,
            [48] => she
            [49] => was
            [50] => hard
            [51] => at
            [52] => work on
            [54] => the
            [55] => white
            [56] => kitten,
            [57] => which
            [58] => was
            [59] => lying
            [60] => quite
            [61] => still
            [62] => and
            [63] => trying
            [64] => to
            [65] => purr
            [66] => —
            [67] => no
            [68] => doubt
            [69] => feeling
            [70] => that
            [71] => it
            [72] => was
            [73] => all
            [74] => meant
            [75] => for
            [76] => its
            [77] => good.
            [78] => 
        )

    [2] => Array
        (
            [0] => 
            [1] => But
            [2] => the
            [3] => black
            [4] => kitten
            [5] => had
            [6] => been
            [7] => finished
            [8] => with
            [9] => earlier
            [10] => in
            [11] => the
            [12] => afternoon,
            [13] => and
            [14] => so,
            [15] => while
            [16] => Alice
            [17] => was
            [18] => sitting
            [19] => curled
            [20] => up
            [21] => in
            [22] => a
            [23] => corner
            [24] => of
            [25] => the
            [26] => great
            [27] => arm-chair,
            [28] => half
            [29] => talking
            [30] => to
            [31] => herself
            [32] => and
            [33] => half
            [34] => asleep,
            [35] => the
            [36] => kitten
            [37] => had
            [38] => been
            [39] => having
            [40] => a
            [41] => grand
            [42] => game
            [43] => of
            [44] => romps
            [45] => with
            [46] => the
            [47] => ball
            [48] => of
            [49] => worsted
            [50] => Alice
            [51] => had
            [52] => been
            [53] => trying
            [54] => to
            [55] => wind up,
            [57] => and
            [58] => had
            [59] => been
            [60] => rolling
            [61] => it
            [62] => up
            [63] => and
            [64] => down
            [65] => till
            [66] => it
            [67] => had
            [68] => all
            [69] => come
            [70] => undone
            [71] => again;
            [72] => and
            [73] => there
            [74] => it
            [75] => was,
            [76] => spread
            [77] => over
            [78] => the
            [79] => hearth-rug,
            [80] => all
            [81] => knots
            [82] => and
            [83] => tangles,
            [84] => with
            [85] => the
            [86] => kitten
            [87] => running
            [88] => after
            [89] => its
            [90] => own
            [91] => tail
            [93] => the
            [94] => middle.
            [95] => 
        )

    [3] => Array
        (
            [0] => 
            [1] => ‘Oh,
            [2] => you
            [3] => wicked
            [4] => little
            [5] => thing!’
            [6] => cried
            [7] => Alice,
            [8] => catching up
            [10] => the
            [11] => kitten,
            [12] => and
            [13] => giving
            [14] => it
            [15] => a
            [16] => little
            [17] => kiss
            [18] => to
            [19] => make
            [20] => it
            [21] => understand
            [22] => that
            [23] => it
            [24] => was
            [25] => in
            [26] => disgrace.
            [27] => 
        )

)

para.log给了我这个,就像我没有数组的最后一个元素,但是我有一个索引:

[2019-05-18 20:32:38] Paragraph Key:0Array
(
    [0] => One
    [1] => thing
    [2] => was
    [3] => certain,
    [4] => that
    [5] => the
    [6] => white
    [7] => kitten
    [8] => had
    [9] => had
    [10] => nothing
    [11] => to
    [12] => do
    [13] => with
    [14] => it:
    [15] => —
    [16] => it
    [17] => was
    [18] => the
    [19] => black
    [20] => kitten’s
    [21] => fault
    [22] => entirely.
    [23] => For
    [24] => the
    [25] => white
    [26] => kitten
    [27] => had
    [28] => been
    [29] => having
    [30] => its
    [31] => face
    [32] => washed
    [33] => by
    [34] => the
    [35] => old
    [36] => cat
    [37] => for
    [38] => the
    [39] => last
    [40] => quarter
    [41] => of
    [42] => an
    [43] => hour
    [44] => (and
    [45] => bearing
    [46] => it
    [47] => pretty
    [48] => well,
    [49] => considering);
    [50] => so
    [51] => you
    [52] => see
    [53] => that
    [54] => it
    [55] => couldn’t
    [56] => have
    [57] => had
    [58] => any
    [59] => hand in
    [61] => the
    [62] => mischief.
    [63] => 
)

[2019-05-18 20:32:38] Paragraph Key:1Array
(
    [0] => 
    [1] => The
    [2] => way
    [3] => Dinah
    [4] => washed
    [5] => her
    [6] => children’s
    [7] => faces
    [8] => was
    [9] => this:
    [10] => first
    [11] => she
    [12] => held
    [13] => the
    [14] => poor
    [15] => thing
    [16] => down
    [17] => by
    [18] => its
    [19] => ear
    [20] => with
    [21] => one
    [22] => paw,
    [23] => and
    [24] => then
    [25] => with
    [26] => the
    [27] => other
    [28] => paw
    [29] => she
    [30] => rubbed
    [31] => its
    [32] => face
    [33] => all
    [34] => over,
    [35] => the
    [36] => wrong
    [37] => way,
    [38] => beginning
    [39] => at
    [40] => the
    [41] => nose:
    [42] => and
    [43] => just
    [44] => now,
    [45] => as
    [46] => I
    [47] => said,
    [48] => she
    [49] => was
    [50] => hard
    [51] => at
    [52] => work on
    [54] => the
    [55] => white
    [56] => kitten,
    [57] => which
    [58] => was
    [59] => lying
    [60] => quite
    [61] => still
    [62] => and
    [63] => trying
    [64] => to
    [65] => purr
    [66] => —
    [67] => no
    [68] => doubt
    [69] => feeling
    [70] => that
    [71] => it
    [72] => was
    [73] => all
    [74] => meant
    [75] => for
    [76] => its
    [77] => good.
    [78] => 
)

[2019-05-18 20:32:38] Paragraph Key:2Array
(
    [0] => 
    [1] => But
    [2] => the
    [3] => black
    [4] => kitten
    [5] => had
    [6] => been
    [7] => finished
    [8] => with
    [9] => earlier
    [10] => in
    [11] => the
    [12] => afternoon,
    [13] => and
    [14] => so,
    [15] => while
    [16] => Alice
    [17] => was
    [18] => sitting
    [19] => curled
    [20] => up
    [21] => in
    [22] => a
    [23] => corner
    [24] => of
    [25] => the
    [26] => great
    [27] => arm-chair,
    [28] => half
    [29] => talking
    [30] => to
    [31] => herself
    [32] => and
    [33] => half
    [34] => asleep,
    [35] => the
    [36] => kitten
    [37] => had
    [38] => been
    [39] => having
    [40] => a
    [41] => grand
    [42] => game
    [43] => of
    [44] => romps
    [45] => with
    [46] => the
    [47] => ball
    [48] => of
    [49] => worsted
    [50] => Alice
    [51] => had
    [52] => been
    [53] => trying
    [54] => to
    [55] => wind up,
    [57] => and
    [58] => had
    [59] => been
    [60] => rolling
    [61] => it
    [62] => up
    [63] => and
    [64] => down
    [65] => till
    [66] => it
    [67] => had
    [68] => all
    [69] => come
    [70] => undone
    [71] => again;
    [72] => and
    [73] => there
    [74] => it
    [75] => was,
    [76] => spread
    [77] => over
    [78] => the
    [79] => hearth-rug,
    [80] => all
    [81] => knots
    [82] => and
    [83] => tangles,
    [84] => with
    [85] => the
    [86] => kitten
    [87] => running
    [88] => after
    [89] => its
    [90] => own
    [91] => tail
    [93] => the
    [94] => middle.
    [95] => 
)

[2019-05-18 20:32:38] Paragraph Key:3

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

因此,我在php Bug报告中找到了解决方案:https://bugs.php.net/bug.php?id=60534

这意味着即使我不需要它也必须使用&(引用),只是为了避免此错误

PHP版本5.6.33