I don't know how foreach can traverse such an array like this.

Source: Internet
Author: User
For example, there is an array such as:
Array (4) {
["title"]=>
Array (9) {
[0]=>
String (3) "Heading 1"
[1]=>
String (3) "Heading 2"
[2]=>
String (3) "Heading 3"
[3]=>
String (3) "Heading 4"
}
["id"]=>
Array (9) {
[0]=>
String (1) "N1"
[1]=>
String (1) "N2"
[2]=>
String (1) "N3"
[3]=>
String (1) "N4"
}
["Pic"]=>
Array (9) {
[0]=>
String (4) "1.jpg"
[1]=>
String (4) "2.jpg"
[2]=>
String (4) "3.jpg"
[3]=>
String (4) "4.jpg"
}
["Price"]=>
Array (9) {
[0]=>
String (5) "85.00"
[1]=>
String (6) "155.00"
[2]=>
String (5) "49.00"
[3]=>
String (5) "75.00"
}
.... More groups

}

I want to iterate through the data in this way:



  • Title 1
    85.00





  • Title 2
    155.00





  • Title 3
    49.00





  • Title 4
    75.00




  • What should be written in it?
    foreach ($arr as $key = $value)
    {
    foreach (it won't be written inside)
    {
    Echo '
  • ';
    Echo ';
    Echo ';
    echo $title;
    Echo '. $price. ';
    Echo ';
    Echo '
  • ';
    }
    }


    Reply to discussion (solution)

    foreach ($value as $k = $v)
    Red is the same value as the previous layer, and blue is different.

    You this array structure, it is best to use a two-dimensional array class I sent before to do a row and column conversion, it is much simpler, the output is traversed once can be

    You this array structure, it is best to use a two-dimensional array class I sent before to do a row and column conversion, it is much simpler, the output is traversed once can be
    Where is it? Link can you send me a bit?

    I send a few posts, you can search to find

    In fact, if you can confirm the array of key values corresponding, you can also directly traverse the two-dimensional key value
    for ($i =0; $i <= Two-dimensional key value Max; $i + +) {
    echo $array ["title"] [$i];
    ......
    }

    foreach ($value as $k = $v)
    Red is the same value as the previous layer, and blue is different.
    This comes out of all the data.
    The effect is this:
    Title 1
    Title 2
    Title 3
    Title 4
    N1
    N2
    N3
    N4
    1.jpg
    2.jpg
    3.jpg
    4.jpg
    85.00
    155.00
    49.00
    75.00

    Just took out all the data, not what I wanted.
    Title 1,n1,1.jpg,85.00
    Title 2,n2,2.jpg,155.00
    Title 3,n3,3.jpg,49.00
    Title 4,n4,4.jpg,75.00

    Just took out all the data, not what I wanted.
    Title 1,n1,1.jpg,85.00
    Title 2,n2,2.jpg,155.00
    Title 3,n3,3.jpg,49.00
    Title 4,n4,4.jpg,75.00
    That's what your data structure is, so let's change it, or write it in # #

    For data

    $ar = Array ("title" = = Array (  "Heading 1", "Title 2", "Heading 3", "Heading 4"),  "id" = = Array ("N1", "N2", "N3", "N4"),  "pic" = = Array ("1.jpg", "2.jpg", "3.jpg", "4.jpg"),  "price" = = Array ("85.00", "155.00", "49.00", "75.00"),);
    Method 1:
    foreach ($ar [' id '] as $i = + $r) {  echo <<< HTML
  • {$ar [' title '] [$i]} {$ar [' price '] [$i]}
  • HTML;}
    Scenario 2
    foreach (Array_map (null, $ar [' id '], $ar [' pic '], $ar [' title '], $ar [' Price ']) as $i + = $r) {  echo <<< HTML
  • $r [2]$r [3]
  • HTML;}
    can be output
  • Title 185.00
  • Title 2155.00
  • Title 349.00
  • Title 475.00
  • Related Article

    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.