SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help getting variables from an array

    I have a array of data that is generated from another software program. The array has arrays inside of it and looks like this.
    PHP Code:
    [CONTROL] => Array ( 

    [
    0] => Array ( [ID] => Name_First [CAPTION] => First Name: [value] => Joe 
    [
    1] => Array ( [ID] => Name_Last [CAPTION] => Last Name:    [value] => Blogs 
    [
    2] => Array ( [ID] => Street [CAPTION] => Street: [value] => 1 Some St 
    [
    3] => Array ( [ID] => PostalCode [CAPTION] => Post Code: [value] => 1234 
    [
    4] => Array ( [ID] => City [CAPTION] => City: [value] => Mytown 
    [
    5] => Array ( [ID] => Online_Email [CAPTION] => Email: [value] => [email]me@me.net[/email] ) 
    [
    6] => Array ( [ID] => Phone_Number [CAPTION] => Ph: [value] => 1234 

    I can currently get the variables for the array by using this (The array is inside of a datastring $data):
    PHP Code:
    $Fname_label "" $data["CONTROL"][0]["CAPTION"];
    $Fname "" $data["CONTROL"][0]["value"];

    $Lname_label "" $data["CONTROL"][1]["CAPTION"];
    $Lname "" $data["CONTROL"][1]["value"];

    $street_label "" $data["CONTROL"][2]["CAPTION"];
    $street "" $data["CONTROL"][2]["value"];

    $post_label "" $data["CONTROL"][3]["CAPTION"];
    $post "" $data["CONTROL"][3]["value"];

    $city_label "" $data["CONTROL"][4]["CAPTION"];
    $city "" $data["CONTROL"][4]["value"];

    $email_label "" $data["CONTROL"][5]["CAPTION"];
    $email "" $data["CONTROL"][5]["value"];

    $phone_label "" $data["CONTROL"][6]["CAPTION"];
    $phone "" $data["CONTROL"][6]["value"]; 
    The problem I am having is that the sometimes not all of the values will be used in the array and at other times more will be added (upto 30 in the real code)

    This means thats the way I am assigning the variables wont work because the array with the email data in in may be in array [4] or it may be in array [17] or any other number.

    My question is - How can I match the data up to the right variables from the array?

  2. #2
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd try to rewrite the files personally as you will get quite process intense if you have to scan the entire file each time you are looking for something.

    Anyway, you could extract each index and check the value of named subindices....

    $array_count = count($data['control']);
    $stepp = -1;
    while(++$stepp < $array_count)
    {
    extract($data['control'][$array_count];
    if($caption == "Email")
    {
    $the_email_address = $value;
    }
    }

    Fly typed and will be case insensitive to what you need. Should get the idea across anyway
    teckis - that's news to me.

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok to cover each different one I could do this and the variables will be assigned to be used later?

    PHP Code:
    $array_count count($data['control']);
    $stepp = -1;
    while(++
    $stepp $array_count)
    {
    extract($data['control'][$array_count];

        if(
    $id == "Name_First"){
            
    $Fname_label $caption;
            
    $Fname $value
            
    }
        if(
    $id == "Name_Last"){
            
    $Lname_label $caption;
            
    $Lname $value
            
    }
        if(
    $id == "Street"){
            
    $street_label $caption;
            
    $street $value
            
    }
        if(
    $id == "PostalCode"){
            
    $post_label $caption;
            
    $post $value
            
    }
        if(
    $id == "City"){
            
    $city_label $caption;
            
    $city $value
            
    }
        if(
    $id == "PostalCode"){
            
    $post_label $caption;
            
    $post $value
            
    }
        if(
    $id == "Online_Email"){
            
    $email_label $caption;
            
    $email $value
            
    }
        if(
    $id == "Phone_Number"){
            
    $phone_label $caption;
            
    $phone $value
            
    }


  4. #4
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Use a switch() statement.
    PHP Code:
    switch ( $id )
    {
       case 
    'Name_First':
            
    $Fname_label $caption;
            
    $Fname $value;
            break;
       case 
    'Name_Last':
            
    // you get the idea
            
    break;


  5. #5
    SitePoint Zealot
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhh ok so the switch statement is better?

    I take it I would still nee to use this code to count through the array?

    PHP Code:
    $array_count count($data['control']);
    $stepp = -1;
    while(++
    $stepp $array_count)
    {
    extract($data['control'][$array_count];

    // Then the switch statement goes here
    switch ( $id )
    {
       case 
    'Name_First':
            
    $Fname_label $caption;
            
    $Fname $value;
            break;
       case 
    'Name_Last':
            
    // you get the idea
            
    break;


  6. #6
    SitePoint Zealot
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I have mad up a dummy array which resembles some of the data in the real array.

    I have tried putting in the count code as posted above and I get
    Code:
    Parse error: parse error, unexpected $ in /var/www/virtual/qoc.lan/test.php on line 46
    PHP Code:
    <?php

    $data 
    =
    array ( 
    order => 
            array(
    standardctrls => 
                    array(
    txtcontrol => 
                            array( 
    =>
                            
    "array([id]=>Name_First,[caption]=>Firstname:,[value]=>Joe)",     
                            
    "array([id]=>Name_Last,[caption]=>Lastname:,[value]=>Bloggs')",
                            
    "array([id]=>Street,[caption]=>Street:,[value]=>1 My Street)"
                            
    )
                        )
                )
        );


    $array_count count($data['order'][0]['standardctrls'][0]['txtcontrol']);
    $stepp = -1;
    while(++
    $stepp $array_count)
    {
    extract($data['order'][0]['standardctrls'][0]['txtcontrol'][$array_count]);

    switch ( 
    $id )
    {
       case 
    'Name_First':
            
    $Fname_label $caption;
            
    $Fname $value;
            break;
       case 
    'Name_Last':
            
    $Lname_label $caption;
            
    $Lname $value;
            break;
       case 
    'Street':
            
    $street_label $caption;
            
    $street $value;
            break;
    }

    echo 
    "$firstnamelbl $firstname <br>"
            
    ."$Lname_label $Lname <br>"
            
    ."$street_label $street";

    ?>
    Anyone have any ideas?

  7. #7
    SitePoint Zealot
    Join Date
    Oct 2002
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your WHILE-Loop isn't closed

    PHP Code:
    while(++$stepp $array_count)
    {
    extract($data['order'][0]['standardctrls'][0]['txtcontrol'][$array_count]); 
    include_once('./sig.inc.php');

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhh thanks, that fixed the error.

    Now to the code itself, as it does not assign the values from the array to the variables.

    I think the problem starts with the array count.

    PHP Code:
    $array_count count($data['order'][0]['standardctrls'][0]['txtcontrol']);

    echo 
    $array_count;
    ?> 
    returns 0

    why doesn't it count the three arrays in ['txtcontrol']?
    Last edited by direwolf; Jan 16, 2003 at 17:29.

  9. #9
    SitePoint Member Siddhartha's Avatar
    Join Date
    Mar 2004
    Location
    Bulgaria
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no! it starts with $data's initialization!

    you should enclose all key identifiers in quotes! else it is treated like constants and have value of 0!

    array ( order => ...) is the same as (0=>...) if order is not set.

    it returns 0 because you have 2 different variables $a[txtcontrol] and $a['txtcontrol'] .
    first one you'd set as $a[0] and have 3 values, but second you'd never set and have 0 values.

    $a[txtcontrol] == $a[0] != $a['txtcontrol']
    CSS


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
  •