I am generating a multidimensional array which outputs in the following format:
Array
(
[0] => Array
(
[cName] => "Club Name (Exp. Date)"
[location] => ""
[days_times] => '0'
[zip] => 0
[state] => 0
[contact_person] => ""
[contact_phone] => '0'
)
)
Array
(
[0] => Array
(
[cName] => "Club Name (Exp. Date)"
[location] => ""
[days_times] => '0'
[zip] => 0
[state] => 0
[contact_person] => ""
[contact_phone] => '0'
)
)
I need it to output in the following format Each new record with a unique key:
Array
(
[0] => Array
(
[cName] => "Club Name (Exp. Date)"
[location] => ""
[days_times] => '0'
[zip] => 0
[state] => 0
[contact_person] => ""
[contact_phone] => '0'
)
[1] => Array
(
[cName] => "Club Name (Exp. Date)"
[location] => ""
[days_times] => '0'
[zip] => 0
[state] => 0
[contact_person] => "Contacts"
[contact_phone] => '0'
)
)
I am using the code below to build the array.
foreach ($xpath->query('//table[@id="tblClubs"]/tr') as $node) {
$rowData = array();
foreach ($xpath->query('td', $node) as $cell) {
$rowData[] = "\"$cell->nodeValue\"";
}
$data[] = $rowData;
$array_two = array();
$arrayIndex=0;
$loopCount=0;
$second_index="";
foreach ($rowData as $myValue) {
$loopCount = ($loopCount<5) ? $loopCount : 0;
switch ($loopCount) {
case '0':
$second_index="cName";
break;
case '1':
$second_index="location";
break;
case '2':
$second_index="contact_person";
break;
}
$array_two[$arrayIndex][$second_index]=$myValue;
if($loopCount == 1)
{
//escape single quote in location information
$locale=str_replace("'", "\'", $myValue);
$array_two[$arrayIndex]['location'] = $locale;
//get playing days
if(preg_match("/?!\d[0-9]{4}/", $myValue, $match) == 1)
{
$array_two[$arrayIndex]['days_times'] = "'$match[0]'";
}
else
{
$array_two[$arrayIndex]['days_times'] = "'0'";
}
//get zip
$pattern = '#([A-Z]{0})\s+(\d{5})#s';
if(preg_match($pattern, $myValue, $match) == 1)
{
$array_two[$arrayIndex]['zip'] = trim($match[0]);//"'$match[0]'";
}
else
{
$array_two[$arrayIndex]['zip'] = 0;
}
//get state
if(preg_match("/[A-Z]{2}(?=[\s]{1,}[0-9]{5})/", $myValue, $match) == 1)
{
$array_two[$arrayIndex]['state'] = "'$match[0]'";
}
else
{
$array_two[$arrayIndex]['state'] = '0';
}
}
//get contact phone number
if($loopCount == 2)
{
if(preg_match('/\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/', $myValue, $match) == 1)
{
$array_two[$arrayIndex]['contact_phone'] = "'$match[0]'";
}
else
{
$array_two[$arrayIndex]['contact_phone'] = "'0'";
}
//remove phone and leave contact name
$newContact=preg_replace('/\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/','',$myValue);
$array_two[$arrayIndex]['contact_person'] = $newContact;
}
$arrayIndex = ($loopCount>=4) ? $arrayIndex+1 : $arrayIndex;
$loopCount++;
}
}
May I ask for some assistance in direct me to where in the code needing modification for the desired output format.
I appreciate any thoughts on this.