Hello,

I have the following array:

   Array
(
    [2] => Champlain Valley TTC(1/31/2015)
    [5] => Old Shelburne Town School5420 Shelburne RdSherburne, VT  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 RowRutland, VT  05701
    [17] => Wednesday - 6:00 - 10:00PM and 2 Fridays(6:00 - 10:00PM) & 2 Saturdays(7:00AM - 9:00PM) per 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 StNorwich, VT  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

        Array
(
    [cName] => Champlain Valley TTC(1/31/2015)
    [local] => Old Shelburne Town School5420 Shelburne RdSherburne, VT  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 RowRutland, VT  05701
    [days_time] => Wednesday - 6:00 - 10:00PM and 2 Fridays(6:00 - 10:00PM) & 2 Saturdays(7:00AM - 9:00PM) per 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 StNorwich, VT  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

Recommended Answers

All 6 Replies

What are you using to generate the array?

Maybe you could change the way the array is created. like so.. array('name' => 'value');

Hi gabrielcastillo,

The process for generating the array is a bit complicate. But I'm grabbing data from an html table <tr> and building the array. Here is the code (may not make any sense to you):

foreach($dom->find('tr[class="clubHeaderRow"]') as $trHdr) {
$trHdr = pq($trHdr);

$trHdr->replaceWith('' );//remove the table header

}

    $match_strip=strip_tags($match);//strip html from string
    $match_array=preg_split("/$\n?/m", $match_strip);//turn each newline in string into array of characters
    $match_array_clean_phase1 = array_map('trim', $match_array);//clean phase one, remove empty array
    $clubFount_array=array_filter($match_array_clean_phase1);//clean phase 2

Maybe you could use the table header for the array keys..

Capture the table header, then expload into an array. foreach table header is key and foreach table field is value.

Try this

$array_one = array('Champlain Valley TTC(1/31/2015)','Old Shelburne Town School5420 Shelburne RdSherburne, VT  05482','Friday - 6:30 - 10:00PM','Jack Devine802-877-3341','<-----NEXT_CLUB ----->','Green Mountain Table Tennis Club(GMTTC)(6/30/2014)','Knights of Columbus - Boys & Girls Club Gymnasium21 Merchants RowRutland, VT  05701','Wednesday - 6:00 - 10:00PM and 2 Fridays(6:00 - 10:00PM) & 2 Saturdays(7:00AM - 9:00PM) per year for  tournaments(last Fri/Sat in March & Fri/Sat in Nov or Dec)','Ronald Lewis802-247-5913','<-----NEXT_CLUB ----->','Upper Valley Table Tennis Club(9/30/2014)','Marion Cross Elementary School Gym22 Church StNorwich, VT  05051','Wednesday - 7:00 - 9:15PM(During School Year)','Douglas Haynes603-643-2178','<-----NEXT_CLUB ----->');

echo '<h2>First Array</h2>';
var_dump($array_one);
$array_two = array();
$arrayIndex=0;
$loopCount=0;
$second_index="";
foreach ($array_one 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+1 : $arrayIndex;
    $loopCount++;
}

echo '<h2>Second Array</h2>';

var_dump($array_two);

Bachov-Varghese,

Thank you for chiming in. Your suggested code is performing exactly as I intended. I appreciate that.

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

The code execution produces a multidimensional-array of the following structure:

Array
(
[0] => Array
    (
        [cName] => Greater Columbia Table Tennis Club
(3/31/2014)
        [local] => Denny Terrace
6429 Bishop Ave.
Columbia, SC  29223
        [days_times] => Sunday - 2:00 - 4:00PM and Wednesday - 6:30- 8:40PM
        [contact] => Francis Kovach
803-755-0071
        [marker] => <-----CLUB DATA ENDS ----->
    )

[1] => Array
    (
        [cName] => Greenville Table Tennis Club
(3/31/2014)
        [local] => Pavilion Greenville Recreation Center
400 Scottwood Rd.
Taylors, SC  29687
        [days_times] => Wednesday - 7:00 to 10:00 PM
        [contact] => David Blair
864-915-3993
        [marker] => <-----CLUB DATA ENDS ----->
    )

[2] => Array
    (
        [cName] => Hanahan Table Tennis Training Center
(6/30/2014)
        [local] => City of Hanahan Gym
1255 Yeamans Hall Rd.
Hanahan, SC  29410
        [days_times] => Thursday - 7:30 - 10:00PM and Sunday - 2:00 - 6:00OPM
        [contact] => Srinivas Janardhanan

        [marker] => <-----CLUB DATA ENDS ----->
    )

[3] => Array
    (
        [cName] => Palmetto Table Tennis Club
(1/31/2015)
        [local] => Richland Mall
3400 Forest Dr
Columbia, SC  29204

Directions:
Enter at Barnes & Noble entrance, lower level
        [days_times] => Tuesday, Wednesday, & Thursday - 5:30 - 8:30PM, Saturday - 10:00AM - 7:00PM and Sunday - 1:30 - 5:30PM
        [contact] => Joel Mitchell
803-447-0007
        [marker] => <-----CLUB DATA ENDS ----->
    )

)

my db table schema would be something like:

`ID` int(11) NOT NULL AUTO_INCREMENT,
  `cName` varchar(45) NOT NULL,
  `local` text(255) NOT NULL,
  `days_times` text(55) NOT NULL,
  `contact` varchar(45) NOT NULL,

resolved!

solution is as follows:

///* Working   
   foreach ($array_two as $k => $v) 
{
//$query = "INSERT INTO Clubs_test (".implode(',',array_keys($v)).") VALUES (". implode(',',$v).")";

$r = mysql_query($query,$c) or die(mysql_error());
    }
    mysql_close($c);  

//*/ //Working
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.