SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multidimensional array function from seperate arrays

    Hello,

    I've tried to get help on this but a bit prematurely so I am trying again.

    I have 2 arrays but I need to make them into a multidimensional array but the factors need to change using IF statements and I need this all done in a function for callback in Jpgraph.

    Here is my code:

    PHP Code:
    //the following 2 arrays are pulled from my DB and work
    //determine size

    $cirsz[0] = 'small';
    $cirsz[1] = 'medium';
    $cirsz[2] = 'large';

    //determine color from these values
    $circlr[0] =  23;
    $circlr[1] = 20;
    $circlr[2] = 18;

    //breaksdown somewhere in the function

    function SettingSet($cirsz$circlr) {

    global 
    $cirsz
    global 
    $circlr
        
    // This callback will adjust the fill color and size of
        // the datapoint according to the data value
        
    $i 0;
    //get sizes
    foreach($cirsz as $s){
    if(
    $s == 'small'){
    $sz 15;
    }elseif(
    $s == 'medium'){
    $sz 30;
    }else{
    $sz 50;
    }

    if(
    $circlr <= 25 && ($circlr >= 18.75)){
    $col "red";
    }elseif(
    $circlr 18.75 && ($circlr >= 12.5)){
    $col ="orange";
    }elseif(
    $circlr 12.5 && ($circlr >= 6.25)){
    $col "yellow";
    }else{
    $col "green";
    }
    $i++;
    }
      return array(
    $sz,"",$col); 

    I need the array which the function returns to look similar to this:

    Code:
    array(
    $a[0] = 15 => "" => red;
    $a[1] = 30 => "" => red;
    $a[2] = 50 => "" => orange;
    }
    Thank You in advance for taking the time to help me.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    PHP Code:
    function SettingSet($cirsz$circlr) {
      
    // This callback will adjust the fill color and size of
      // the datapoint according to the data value
      
      
    $result = array();

      
    //get sizes
      
    foreach ($cirsz as $s) {
        if(
    $s == 'small') {
          
    $sz 15;
        } elseif (
    $s == 'medium') {
          
    $sz 30;
        } else {
          
    $sz 50;
        }

        if (
    $circlr <= 25 && ($circlr >= 18.75)) {
          
    $col "red";
        } elseif (
    $circlr 18.75 && ($circlr >= 12.5)) {
          
    $col ="orange";
        }  elseif (
    $circlr 12.5 && ($circlr >= 6.25)) {
          
    $col "yellow";
        } else {
          
    $col "green";
        }

        
    $result[] = array($sz""$col);
      }

      return 
    $result



  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    PHP Code:
    function SettingSet($cirsz$circlr) {
      
    // This callback will adjust the fill color and size of
      // the datapoint according to the data value
      
      
    $result = array();

      
    //get sizes
      
    foreach ($cirsz as $s) {
        if(
    $s == 'small') {
          
    $sz 15;
        } elseif (
    $s == 'medium') {
          
    $sz 30;
        } else {
          
    $sz 50;
        }

        if (
    $circlr <= 25 && ($circlr >= 18.75)) {
          
    $col "red";
        } elseif (
    $circlr 18.75 && ($circlr >= 12.5)) {
          
    $col ="orange";
        }  elseif (
    $circlr 12.5 && ($circlr >= 6.25)) {
          
    $col "yellow";
        } else {
          
    $col "green";
        }

        
    $result[] = array($sz""$col);
      }

      return 
    $result


    Thank You. This is now returning these messages:

    Warning: Missing argument 2 for SettingSet()

    Warning: Invalid argument supplied for foreach()
    $result is also returning as NULL

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I figured out the problem but I'm not sure how to fix it.

    This:

    PHP Code:
    $result[] = array($sz""$col); 
    is returning as this ArrayArrayArray.

    Any ideas?

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should use var_dump() or print_r() if you want to view the contents of a variable, or the return value of a function. echo/print isn't suited to displaying arrays.

    This is probably a good time for you to read a tutorial on php functions.

  6. #6
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    You should use var_dump() or print_r() if you want to view the contents of a variable, or the return value of a function. echo/print isn't suited to displaying arrays.

    This is probably a good time for you to read a tutorial on php functions.
    I have used all of the above. They all return as either NULL or nothing at all. If i echo $sz or $col within the function they return correctly for each increment of the respective arrays. I am also getting this error when i print_r($result) or callback the function: Fatal error: Unsupported operand types

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    post some code which demonstrates your problem.

  8. #8
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    post some code which demonstrates your problem.
    I've made a few adjustments and now I have the array printing correctly when it's called back inside the function. When it is called from without using

    print_r($result); It returns nothing just whitespace.
    Using:
    print SettingSet(); it calls back the word Array.

    Code is below:

    PHP Code:
     function SettingSet() {   
     
    // This callback will adjust the fill color and size of   
     // the datapoint according to the data value     
      
    global $cirsz
      global 
    $circlr;
      
     
    //$result = array();    
      //get sizes   
      
    $i=0;
      foreach (
    $cirsz as $s) { 
      
      if(
    $s == 'small') {       
      
    $sz 15;    
       } elseif (
    $s == 'medium') {      
        
    $sz 30;     
        } else {      
         
    $sz 50;     
         }     
    //$sizefin = array($sz);
     
    if ($circlr[$i] <= 25 && ($circlr[$i] >= 18.75)) {     
         
    $col "red";     
         } elseif (
    $circlr[$i] < 18.75 && ($circlr[$i] >= 12.5)) {    
          
    $col ="orange";    
           }  elseif (
    $circlr[$i] < 12.5 && ($circlr[$i] >= 6.25)) {      
            
    $col "yellow";    
             } else {      
              
    $col "green";     
              } 
             
    // $colfin = array($col);
              //$b = "";
             // $bl = array($b);
               
    $i++;  
               
    $result[] = array($sz""$col);
                          }  
                
    //print_r($result);  //uncommenting this line prints the array correctly.
                
    return $result
                 } 
    Thanks

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    print and echo are not suitable for displaying an array. You should use print_r() or var_dump() to display an array.

    Your function returns an array.
    PHP Code:
    print_r(SettingSet()); 

  10. #10
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    print and echo are not suitable for displaying an array. You should use print_r() or var_dump() to display an array.

    Your function returns an array.
    PHP Code:
    print_r(SettingSet()); 
    Odd, i tried that one, maybe i typo'd. Any idea what this means?

    Fatal error: Unsupported operand types

    That is what i am getting when inserting the value into the graphs using:

    PHP Code:
    $sp1->mark->SetCallback("SettingSet"); 

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The error message probably tells you where the error is(file and line)

  12. #12
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    The error message probably tells you where the error is(file and line)
    it doesn't there aren't even close to 306 lines of code. Which is where it says the error is. I think it's something to do with the way the array is called back but I'm really stumped at this point.

    It was originally built from this function they give as an example in jpgraph.

    PHP Code:
    function FCallback($aVal) {
        
    // This callback will adjust the fill color and size of
        // the datapoint according to the data value according to
        
    if( $aVal 30 $c "blue";
        elseif( 
    $aVal 70 $c "green";
        else 
    $c="red";
        return array(
    floor($aVal/3),"",$c);

    The problem with using this function is that is scales to the Y axis values of the graph, I need it to scale to a piece of data that isn't represented in my graph.


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
  •