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

Source: Internet
Author: User
I don't know how foreach traverses such an array. for example, there is an array like this: array (4) & nbsp ;{& nbsp; & nbsp; ["title"] = & gt; & nbsp; array (9) & nbsp; {& nbsp; [0] = & gt; & nbsp; & nbsp doesn't understand how foreach traverses such an array in this way
For example, there is an array:
Array (4 ){
["Title"] =>
Array (9 ){
[0] =>
String (3) "Title 1"
[1] =>
String (3) "title 2"
[2] =>
String (3) "title 3"
[3] =>
String (3) "title 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 arrays

}

I want to traverse the data like this:



  • Title 1
    85.00





  • Title 2
    155.00





  • Title 3
    49.00





  • Title 4
    75.00




  • How should I write it?
    Foreach ($ arr as $ key => $ value)
    {
    Foreach (it won't be written)
    {
    Echo'
  • ';
    Echo '';
    Echo '';
    Echo $ title;
    Echo''. $ Price .'';
    Echo '';
    Echo'
  • ';
    }
    } Shared :?? Array (9 )? {???? [0] => ???? String (3 )? "Title 1 "???? [1] => ???? S... 'data-pics = ''>
    ------ Solution --------------------
    For data Reference
    $ Ar = array (
    "Title" => array ("title 1", "title 2", "title 3", "title 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;
    }
    Solution 2
    foreach(array_map(null, $ar['id'], $ar['pic'], $ar['title'], $ar['price']) as $i=>$r) {
      echo <<< HTML



  • $r[2]
    $r[3]


  • HTML;
    }
    Both outputs



  • Title 1
    85.00




  • Title 2
    155.00




  • Title 3
    49.00




  • Title 4
    75.00


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