Hello,

I have the following array

Array
(
    [0] => Array
        (
            [cName] => 'Lexington Table Tennis Club
(9/30/2014)'
            [local] => 'Castlewood Gym
201 Castlewood Dr.
Lexington, KY  40505'
            [days_times] => 'Saturday - 2:30 - 6:00PM and Sunday - 9:15AM - 12:00PM'
            [contact] => 'Babak xxxx
859-xxx-2498'
            [marker] => '<-----CLUB DATA ENDS ----->'
        )

    [1] => Array
        (
            [cName] => 'Table Tennis Club of Louisville
(3/31/2014)'
            [local] => 'Beechmont Community Center
205 W. Wellington Ave
Louisville, KY  40214

Directions:
Located just 2 miles from Louisville International Airport'
            [days_times] => 'Tues - 4:00 -9:00PM, Wed - 11:00AM - 2:00PM, & Sat -   9:00AM - 1:00PM'
            [contact] => 'Paul xxxxx
502-805-xxxx'
            [marker] => '<-----CLUB DATA ENDS ----->'
        )

)

I would like to look inside each sub array with keys [0] and [1] for the sub-array [local]. Now inside the value of [local] I would like to extract the 5 digit zipcode and then insert a new array after [local] with a key of [zip] => 'the extracted zip'

as an example, array with key [1] would look like:

  [1] => Array
            (
                [cName] => 'Table Tennis Club of Louisville
    (3/31/2014)'
                [local] => 'Beechmont Community Center
    205 W. Wellington Ave
    Louisville, KY  40214

    Directions:
    Located just 2 miles from Louisville International Airport'
     [zip] => '40214'
                [days_times] => 'Tues - 4:00 -9:00PM, Wed - 11:00AM - 2:00PM, & Sat -   9:00AM - 1:00PM'
                [contact] => 'Paul xxxxx
    502-805-xxxx'
                [marker] => '<-----CLUB DATA ENDS ----->'
            )

Is this doable? if so, may I ask for some assistance...

Thank you!

Recommended Answers

All 4 Replies

Yes, it can be done. Inside the loop that generates this array place this code:

if($loopCount == 1)
{
    if(preg_match("/[0-9]{5}/", $myValue, $match) == 1)
    {
        $array_two[$arrayIndex]['zip'] = $match[0];
    }
    else
    {
        $array_two[$arrayIndex]['zip'] = 0;
    }   
}

Full example:

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;

    if($loopCount == 1)
    {
        if(preg_match("/[0-9]{5}/", $myValue, $match) == 1)
        {
            $array_two[$arrayIndex]['zip'] = $match[0];
        }
        else
        {
            $array_two[$arrayIndex]['zip'] = 0;
        }

    }

    $arrayIndex = ($loopCount>=4) ? $arrayIndex+1 : $arrayIndex;
    $loopCount++;
}

It could be placed also inside case '1' but in that case it will return before the local key.

cereal,

Great stuff! works great and as expected. I appreciate that!

How would I grab the state 2 characters and create a sub array as well.

I just realize that I will need that as well. Sorry for not mentioning in the initial post.

Always inside the loopCount statement if($loopCount == 1) add this:

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'] = '';
}

The pattern will search for two uppercase characters right before five digits, so NY 40214 is valid, while NY a40214 is not valid.

Exactly what I wanted! Thanks again...
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.