0

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'=>'perl'))
2
  • And what is the format you are looking? Commented Dec 5, 2010 at 17:53
  • 1
    I think you need to specify the format a little more clearly. For example, does the array have to contain exactly two string keys (keys that cannot be interpreted as numbers), each of which is to contain a string value? Commented Dec 5, 2010 at 18:24

3 Answers 3

3
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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

the condition won't match is $val[$i] is numeric
Yes, that's sort of the point. The example says the format is mapping a string to a string.
If necessary, for a less strict test, !is_string($val[$i]) could be changed to is_array($val[$i]).
@Mike: Well, that's the problem with questions like these where the 'format' is given as a small subset example of 'includes' and a small subset example of 'excludes'. It is possible to interpret what the format is in almost an infinite number of ways that will pass on all includes and fail on all excludes but which aren't what the asker had in mind.
Yes, I agree, the format is somewhat open-ended. I'd voted up your first comment, and was just suggesting something that might satisfy ajreal's comment.
1

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');

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.