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.

Recommended Answers

All 7 Replies

Hi Mossa,

Your code is clearly not a million miles away from doing what you want.

I think it would benefit from :

  • Rearranging to do more inside the switch/case structure
  • Having $loopCount as a simple incremental
  • Judicious use of the % operator to work out whether or not we are at the start of a new set of five
  • Explicitly creating a new inner array at the start of each new set.

Addressing all the above, I end up with the following, though I'm not sure it will cure the problem :

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;
    foreach ($rowData as $myValue) {
        switch ($loopCount % 5) {
            case 0:
                $array_two[$arrayIndex] = array();//explicitly create an 'inner' array
                $array_two[$arrayIndex]['cName'] = $myValue;
            break;
            case 1:
                // $array_two[$arrayIndex]['location'] = $myValue;
                //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';
                }
            break;
            case 2:
                $array_two[$arrayIndex]['contact_person'] = $myValue;
                //get contact phone number
                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;
            break;
        }
        $loopCount++;//simple increment
        $arrayIndex = (($loopCount % 5) == 0) ? $arrayIndex + 1 : $arrayIndex;//when appropriate, increment $arrayIndex ready for start of next set of five
    }
}

Hi Airshow,

I appreciate your thoughts and the structing of the code.

It appears that I'm still not able to get the intended array format. Not sure where the issue lies.

still looking...

Mossa, I've not tested my code. Does it give anything at all?

Looking at it again, you probably need to declare the main array before the outer foreach() loop, and populate it inside the loop.

If I'm right, then the switch/case structure can also disappear, as follows :

$arr = array();
foreach ($xpath->query('//table[@id="tblClubs"]/tr') as $node) {
    $rowData = array();
    foreach ($xpath->query('td', $node) as $cell) {
        $rowData[] = "\"$cell->nodeValue\"";
    }

    $i = $arr.push(array()) - 1;

    //From cell 0
    $arr[$i]['cName'] = $rowData[0];

    //From cell 1
    //escape single quote in location information
    $arr[$i]['location'] = str_replace("'", "\'", $rowData[1]);
    //get playing days
    if(preg_match("/?!\d[0-9]{4}/", $rowData[1], $match) == 1) {
        $arr[$i]['days_times'] = "'$match[0]'";
    } else {
        $arr[$i]['days_times'] = "'0'";
    }
    //get zip
    $pattern = '#([A-Z]{0})\s+(\d{5})#s';
    if(preg_match($pattern, $rowData[1], $match) == 1) {
        $arr[$i]['zip'] = trim($match[0]);//"'$match[0]'";
    } else {
        $arr[$i]['zip'] = 0;
    }
    //get state
    if(preg_match("/[A-Z]{2}(?=[\s]{1,}[0-9]{5})/", $rowData[1], $match) == 1) {
        $arr[$i]['state'] = "'$match[0]'";
    } else {
        $arr[$i]['state'] = '0';
    }

    //From cell 2
    $arr[$i]['contact_person'] = $rowData[2];
    //get contact phone number
    if(preg_match('/\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/', $rowData[2], $match) == 1) {
        $arr[$i]['contact_phone'] = "'$match[0]'";
    } else {
        $arr[$i]['contact_phone'] = "'0'";
    }
    //remove phone and leave contact name
    $arr[$i]['contact_person'] = preg_replace('/\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/','',$rowData[2]);
}

an error is thrown:

Fatal error: Call to undefined function push() in /home5/tablete7/public_html/spinsoftonline/hhhhhdh.php on line 48

Sorry, that's javascript!

try $i = array_push($arr, array()) - 1;.

Airshow,

That did the job!

I am able to format the array accordingly...Thank you very much!

Best,
Mossa

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.