Questions about the Foreach Loop in PHP

Source: Internet
Author: User
$a = ' 1=a # 2=b # 3=c # 4=d '; $alist = explode (' # ', $a), foreach ($alist as $key + = $value) {$arr =explode (' = ', $value); $blist [$arr [0]]= $arr [1];echo] $blist [$key] ";}


echo "$blist [$key]"; Only output ABC is missing the last one each time how to output ABCD


Reply to discussion (solution)

Direct echo $arr [1]; No, it's okay.

Be sure to use $blist output words should be written as echo "$blist [$key +1]"; Because your $blist array index is starting from 1 to 4

Oh, wrong, it should be echo $blist [$key +1];

Key is 0-3
and $arr[0] is 1-4
So different, change it to this.

$a = ' 1=a # 2=b # 3=c # 4=d '; $alist = explode (' # ', $a), foreach ($alist as $key + = $value) {    $arr =explode (' = ', $value); c1/> $blist [$arr [0]-1]= $arr [1];    echo "$blist [$key]";}

$arr [1] can actually output ABCD to solve this problem.

So how do you write the output C? $arr [1[3]]? It seems wrong.

Key is 0-3
and $arr[0] is 1-4
So different, change it to this.

$a = ' 1=a # 2=b # 3=c # 4=d '; $alist = explode (' # ', $a), foreach ($alist as $key + = $value) {    $arr =explode (' = ', $value); c4/> $blist [$arr [0]-1]= $arr [1];    echo "$blist [$key]";}




$arr [1] can actually output ABCD to solve this problem.

So how do you write the output C? $arr [1[3]]? It seems wrong.

Direct echo $arr [1]; No, it's okay.

Be sure to use $blist output words should be written as echo "$blist [$key +1]"; Because your $blist array index is starting from 1 to 4



$arr [1] can actually output ABCD to solve this problem.

So how do you write the output C? $arr [1[3]]? It seems wrong.

That is, outputting a single data

Echo $blist [3]; C

Notice the difference between the associative array and the subscript array

$arr [1] can actually output ABCD to solve this problem.

So how do you write the output C? $arr [1[3]]? It seems wrong.


Key is 0-3
and $arr[0] is 1-4
So different, change it to this.

$a = ' 1=a # 2=b # 3=c # 4=d '; $alist = explode (' # ', $a), foreach ($alist as $key + = $value) {    $arr =explode (' = ', $value); c1/> $blist [$arr [0]-1]= $arr [1];    echo "$blist [$key]";}




$arr [1] can actually output ABCD to solve this problem.

So how do you write the output C? $arr [1[3]]? It seems wrong.



Echo $blist [2];
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.