SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with multidimensional arrays

    I am trying to build a function that use multidimensional arrays. The function basically is an html generator. The reason i need to do this with a multidemsional array instead of just passing a number of arrays to the function is that the number of arrays sent to the function will vary.

    Say I have two arrays:

    PHP Code:
    $name[] = "category";
    $name[] = "website";
    $name[] = "domain name";

    $value[] = "sales";
    $value[] = "http:ascifi.com";
    $value[] = "yahoo.com"
    then in the script i create the multi array doing something like

    $array[name] = $name
    $array[value] = $value

    and then do

    function_name($array);


    now in the function i need a loop that gets

    $name and $value
    where the first time through the loop $name is category and $value is sales etc...

    any idea?

    I guess it will be based around this looping system through multi arrays from prof php

    PHP Code:
    while (list ($key1) = each ($new_array)) {
    echo (
    "$key1:<br>\n");

    while (list (
    $key2$val) = each ($new_array["$key1"])) {
    echo (
    "- $val<BR>\n");
        }

    the problem is that goes through a whole branch of the array, what i need to do is get the 1st element of each branch, then the 2nd element of each branch and so on if you see what i mean.

    Any ideas?

  2. #2
    SitePoint Enthusiast Stallion's Avatar
    Join Date
    Jan 2001
    Location
    Cumberland, RI, US
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could either restructure your array, or use a different loop. Restructuring your array would probably be best.
    PHP Code:
    $array[] = array("name"=>"category""value"="sales");
    $array[] = array("name"=>"website""value"=>"http://ascifi.com");
    $array[] = array("name"=>"domain name""value"=>"yahoo.com");

    foreach (
    $array as $element) {
      print 
    "$element[name] => $element[value]<br>\r\n";

    But you could also use a regular old for() loop:
    PHP Code:
    $name[] = "category";
    $name[] = "website";
    $name[] = "domain name";
    $value[] = "sales";
    $value[] = "http:ascifi.com";
    $value[] = "yahoo.com";

    for (
    $i=0;$i<count($name);$i++) {
      
    $name_item $name[$i];
      
    $value_item $value[$i];
      print 
    "$name_item => $value_item<br>\r\n";


  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks stallion, i am going to play with those, i think i see what is going on.

  4. #4
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of doing multidimensional arrays (which I think get really messy) I do this:

    array_push($histories, "$list[Title]|$list[BookID]");

    Then when I need to access the information I just use the explode function.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  5. #5
    SitePoint Enthusiast Stallion's Avatar
    Join Date
    Jan 2001
    Location
    Cumberland, RI, US
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats basically a multidimesional array, without the array manipulation functions, but with the added overhead of parsing.

    Don't see any advantage to it... :-(

  6. #6
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but its easier to code with, and any additional overhead it creates is not going to be noticeable.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  7. #7
    SitePoint Enthusiast Stallion's Avatar
    Join Date
    Jan 2001
    Location
    Cumberland, RI, US
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What if you need to sort, use keys, flip values, etc? Those are all built in functions for array manipulation. I just don't see how having to explode() a value, get the results, and then process it is easier than just using the variables.

  8. #8
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when I use it I don't need to do any of that.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •