Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post

    Changing int keys in an array to names

    Hello,

    I have the following array:

    PHP Code:
      Array
    (
        [
    2] => Champlain Valley TTC(1/31/2015)
        [
    5] => Old Shelburne Town School5420 Shelburne RdSherburneVT  05482
        
    [6] => Friday 6:30 10:00PM
        
    [9] => Jack Devine802-877-3341
        
    [10] => <-----NEXT_CLUB ----->
        [
    13] => Green Mountain Table Tennis Club(GMTTC)(6/30/2014)
        [
    16] => Knights of Columbus Boys Girls Club Gymnasium21 Merchants RowRutlandVT  05701
        
    [17] => Wednesday 6:00 10:00PM and 2 Fridays(6:00 10:00PM) & 2 Saturdays(7:00AM 9:00PMper year for  tournaments(last Fri/Sat in March Fri/Sat in Nov or Dec)
        [
    20] => Ronald Lewis802-247-5913
        
    [21] => <-----NEXT_CLUB ----->
        [
    23] => Upper Valley Table Tennis Club(9/30/2014)
        [
    27] => Marion Cross Elementary School Gym22 Church StNorwichVT  05051
        
    [28] => Wednesday 7:00 9:15PM(During School Year)
        [
    36] => Douglas Haynes603-643-2178
        
    [37] => <-----NEXT_CLUB ----->

    I would like to replace the int keys for every block of 4 with name key like so:

    PHP Code:
     Array
    (
        [
    cName] => Champlain Valley TTC(1/31/2015)
        [
    local] => Old Shelburne Town School5420 Shelburne RdSherburneVT  05482
        
    [days_times] => Friday 6:30 10:00PM
        
    [contact] => Jack Devine802-877-3341
        
    [marker] => <-----NEXT_CLUB ----->
        [
    cName] => Green Mountain Table Tennis Club(GMTTC)(6/30/2014)
        [
    local] => Knights of Columbus Boys Girls Club Gymnasium21 Merchants RowRutlandVT  05701
        
    [days_time] => Wednesday 6:00 10:00PM and 2 Fridays(6:00 10:00PM) & 2 Saturdays(7:00AM 9:00PMper year for tournaments(last Fri/Sat in March Fri/Sat in Nov or Dec)
        [
    contact] => Ronald Lewis802-247-5913
        
    [marker] => <-----NEXT_CLUB ----->
        [
    cName] => Upper Valley Table Tennis Club(9/30/2014)
        [
    local] => Marion Cross Elementary School Gym22 Church StNorwichVT  05051
        
    [days_time] => Wednesday 7:00 9:15PM(During School Year)
        [
    contact] => Douglas Haynes603-643-2178
        
    [marker] => <-----NEXT_CLUB ----->

    I appreciate some thoughts on this.

  • #2
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Doing it like so would overwrite the pre-existing values. You need to use a multi-dimensional array like the following
    PHP Code:
    Array (
    => array('cName'=>'...','local''=>'...),
    => array(.....),
    Etc

    Forgive my shorthand and typos, I'm writing with a tablet.

    You could use a loop to easily convert it.
    Something like
    PHP Code:
    $new_array = array();
    $i=1;

    Foreach(
    $array as $key){
     Switch 
    $i {
     Case 
    2:
       
    $array_key 'local';
       Break;
    Case 
    3:
    $array_key 'days_time'; break;
    Case 
    4:
    $array_key contact; break;
    Case 
    5$array_key marker$i=1; break;
    Default: 
    $array_key cName;
    }
    $new_array[][$array_key]=$key;
    ++
    $i;



    Something along the lines of that will work. You will need to clean up the syntax, it won't run as-is.

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,026
    Thanks
    2
    Thanked 314 Times in 306 Posts
    Array keys must be unique, so you cannot do exactly what you have shown (the last value you assign for any key value would be what the element contains.)

    You could make an array of arrays, where the primary key is just an incrementing integer, then the sub-array within each primary key value could be the repeating ['cName'], ['local'], ['days_times'], and ['contact'] keys.

    To break up the data, you can either use a counter with a modulus operator or more simply detect the '<-----NEXT_CLUB ----->' data value to complete the sub-array for one section and start the next.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Thank you all for chiming in and suggesting solutions!

    zealer, building on your specific suggestion, I have the following code that structures the the array:

    PHP Code:
    $array_two = array();
    $arrayIndex=0;
    $loopCount=0;
    $second_index="";
    foreach (
    $result as $myValue) {
    $loopCount = ($loopCount<5) ? $loopCount 0;
    switch (
    $loopCount) {
    case 
    '0':
    $second_index="cName";
    break;
    case 
    '1':
    $second_index="local";
    break;
    case 
    '2':
    $second_index="days_times";
    break;
    case 
    '3':
    $second_index="contact";
    break;

    case 
    '4':
    $second_index="marker";
    break;

    }
    $array_two[$arrayIndex][$second_index]=$myValue;
    $arrayIndex = ($loopCount>=4) ? $arrayIndex+$arrayIndex;
    $loopCount++;

    and the structure of the array is:

    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    cName] => Fairbanks Interior Table Tennis FITT
    (4/30/2014)
                [
    local] => Patty Center
    Univ
    of Alaska Campus
    Fairbanks
    AK  99775
                
    [days_times] => Sunday 3:00 to 5:45PM Tuesday 6:00 8:30PM some Thursday evenings
                
    [contact] => Diann Darnall
    907
    -479-5421
                
    [marker] => <-----CLUB DATA ENDS ----->
            )
     [
    1] => Array
            (
                [
    cName] => Greenville Table Tennis Club
        
    (3/31/2014)
                [
    local] => Pavilion Greenville Recreation Center
        400 Scottwood Rd
    .
        
    TaylorsSC  29687
                
    [days_times] => Wednesday 7:00 to 10:00 PM
                
    [contact] => David Blair
        864
    -915-3993
                
    [marker] => <-----CLUB DATA ENDS ----->
            )


    If I may trouble you or any other members here again, I would like to take this array and insert into mysql, with each sub-array being a record.


    my db table schema would be something like:

    PHP Code:
     `IDint(11NOT NULL AUTO_INCREMENT,
          `
    cNamevarchar(45NOT NULL,
          `
    localtext(255NOT NULL,
          `
    days_timestext(55NOT NULL,
          `
    contactvarchar(45NOT NULL

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Why are you not just dumping the sql fetch into an array instead? I assumed that the 'marker' is something added, but you can simply do as:
    PHP Code:
    $aData = array();
    while (
    $row mysqli_fetch_assoc($qry))
    {
        
    $aData[] = $row;
    }
    // or if you don't care about readability:
    while ($aData[] = mysqli_fetch_assoc($qry) or array_pop($aData)); 
    That should give you a result for $aData like:
    Code:
    Array
    (
        [0] => Array
            (
                [cName] => Fairbanks Interior Table Tennis - FITT
    (4/30/2014)
                [local] => Patty Center
    Univ. of Alaska Campus
    Fairbanks, AK  99775
                [days_times] => Sunday - 3:00 to 5:45PM & Tuesday - 6:00 - 8:30PM & some Thursday evenings
                [contact] => Diann Darnall
    xxx-xxx-xxxx
            )
    
    )
    When you pull it out of the db.
    Also, you should consider handling the days_times a little better. This is fine for display purposes, but if you were to try and search it you will find this will be very complicated. I don't know what this is for, but I'm picturing a course type thing so I can sign up to learn tennis for example. Searching on days of week and times would be very convenient.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Fou-lu,

    thanks for your input!

    Firstly, I'm building a db of clubs in the area and the data is actually being through Parsing HTML on the web. I am using phpquery.php and $dom->find(). This is the reason for structuring the array the way I have.

    Your suggestion assumes that the data is coming from an internal db, but it is not. However, I want to take the generated array and put its content into my db.

    I'm thinking something like this:
    if my array db is:

    PHP Code:
    $array_two=Array
    (
        [
    0] => Array
            (
                [
    cName] => Fairbanks Interior Table Tennis FITT
    (4/30/2014)
                [
    local] => Patty Center
    Univ
    of Alaska Campus
    Fairbanks
    AK  99775
                
    [days_times] => Sunday 3:00 to 5:45PM Tuesday 6:00 8:30PM some Thursday evenings
                
    [contact] => Diann Darnall
    907
    -479-5421
                
    [marker] => <-----CLUB DATA ENDS ----->
            )

     [
    1] => Array
            (
                [
    cName] => Green Mountain Table Tennis Club(GMTTC)
    (
    6/30/2014)
                [
    local] => Knights of Columbus Boys Girls Club Gymnasium
    21 Merchants Row
    Rutland
    VT  05701
                
    [days_times] => Wednesday 6:00 10:00PM and 2 Fridays(6:00 10:00PM) & 2 Saturdays(7:00AM 9:00PMper year for tournaments(last Fri/Sat in March Fri/Sat in Nov or Dec)
                [
    contact] => Ronald Lewis
    802
    -247-5913
                
    [marker] => <-----CLUB DATA ENDS ----->
            )

    )


    PHP Code:
    $c connect();// my db connection function
        
    foreach ($array_two as $k => $v//error LINE 133 
    {
            echo 
    $query "INSERT INTO Clubs_test (id,".
                    
    implode(',',array_keys($v)).
                    
    ") VALUES ($k,".
                    
    implode(',',$v).
                
    ")";
            
    $r mysql_query($query,$c);
        }
        
    mysql_close($c); 
    but getting an the following error:
    PHP Code:
    WarningInvalid argument supplied for foreach() in /home5/********/public_html/******/********.php on line 133 
    Any thoughts!
    Last edited by mbarandao; 01-31-2014 at 03:17 PM.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yes I see, my method would work great for retrieving which I had assumed you were doing.
    Personally, I would just stick with what you have for the original prior to insertion. The names aren't really relevant, and you can chunk split the array. The names can be applied after the fact if you really need them for the query (I'd assume id isn't required since the $k doesn't have a lot of control usage based on input):
    PHP Code:
    $aRecords array_chunk($aData5);
    $aFields = array('cName''local''days_times''contact');
    foreach (
    $aRecords AS $record)
    {
        
    array_pop($record);
        
    $aCombined array_combine($aFields$record);
        
    // $aCombined is now an associative array which you can use in the query

    Where $aData is your original name.

    For your error, $array_two is not an array within the scope of the foreach call. You can determine what it is with a var_dump($array_two).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    mbarandao (02-02-2014)


  •  

    Posting Permissions

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