开发者

How to check if an array is in a specific format

Is there any way to check if an array is in the following format:

array('foo'=>'bar','year'=>'2011');

And not:

array('php','code','computer');

array('foo'=>('foo'=>'bar'),'php'=>('something'=>'p开发者_如何学Cerl'))


function isArrayInFormat($array)
{
    $key = array_keys($array);
    $val = array_values($array);
    $count = count($array);

    for ( $i = 0; $i < $count; $i++ )
    {
        if (!is_string($key[$i]) || !is_string($val[$i]))
        {
            return false;
        }
    }

    return true;
}


First of all you have an error in second statement. I think you wanted to write

array('foo' => array('foo'=>'bar'),'php' => array('something'=>'perl'));

So conditions are: 1. is assosiative array 2. value is not an array

function isAssocAndFlat(array $array) {
    // first we check if array is associatvie
    $keys = array_keys($array);

    // If the array keys of the keys match the keys, then the array must
    // not be associative (e.g. the keys array looked like {0:0, 1:1...}).
    if (array_keys($keys) !== $keys) {
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                return false;
            }
        }
        return true;
    }
    return false;
}

This function passes all your examples.

@Reese Moore your function return invalid value when you test array like this:

$test = array('first' => 'value1', '2' => 'value2');


If the array is not too big and always has equal pattern structure in its elements, you could serialize it and check it with regex-pattern.

Advantage: the code execution is fairly quickly and it is relatively more clean and intelligent. Disadvantage: hard to write the regex-pattern when the array is multidimensional and even worse, when the values of the same element are of different types. You must watch for changes made in the format of the serialized string in the release of a new version of the language (PHP -> serialize).

You can test the following examples of various types of arrays for each of them ...

Example 1:

$arr_1 = array(1,22,333,);       //-> a:3:{i:0;i:1;i:1;i:22;i:2;i:333;}
// $arr_1 = array(1,22,'333',);  //-> a:3:{i:0;i:1;i:1;i:22;i:2;s:3:"333";}

if (is_array($arr_1) && 
    preg_match('/^a:\d+:{(i:\d+;)+}$/', serialize($arr_1))
) {
    echo 'TRUE';
} else {
    echo 'FALSE';
}

If you want to combine the types in the above example you can use the following regular expression:

preg_match('/^a:\d+:{((i:\d+;)|(i:\d+;s:\d+:\"\w+\";))+}$/', serialize($arr_1))

Example 2:

$arr_2 = array('a', 'bb', 'ccccc',); //-> a:3:{i:0;s:1:"a";i:1;s:2:"bb";i:2;s:5:"ccccc";}
// $arr_2 = array('a', 'bb', 7,);    //-> a:3:{i:0;s:1:"a";i:1;s:2:"bb";i:2;i:7;}

if (is_array($arr_2) && 
    preg_match('/^a:\d+:{(i:\d+;s:\d+:\"\w+\";)+}$/', serialize($arr_2))
) {
    echo 'TRUE';
} else {
    echo 'FALSE';
}

Example 3:

$arr_3 = array(
    array('name'=>'War and Peace', 'year'=>1865),
    array('name'=>'Different Seasons', 'year'=>1982),
); // exit(serialize($arr_3));
// $arr_3 = array(
//  array('name'=>'War and Peace', 'year'=>1865),
//  array('name'=>'Different Seasons', 'year'=>'1982'),
// ); // exit(serialize($arr_3));

if (is_array($arr_3) && 
    preg_match('/^a:\d+:{(i:\d+;a:\d+:{s:\d+:\"name\";s:\d+:\"[\w+\s+]+\";s:\d+:\"year\";i:\d{4};})+}$/', serialize($arr_3))
) {
    echo 'TRUE';
} else {
    echo 'FALSE';
}

... etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜