470,848 Members | 1,252 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

How to tell php a variable is an array?

Hello,

What is the syntax for telling php that a variable is an array, or an
array of arrays?

Let's say I have $arrTerms

/*
@param array of arrays $arrTerms
*/
function test($arrTerms) {
$anotherArray = array('1', '2');

$intersect = array_intersect($anotherArray, $arrTerms[0]);

}

This doesn't work and php tells me I'm trying to access $arrTerms[0] as
a array though it is not.

But actually, since $arrTerms is supposed to be an array of arrays --
$arrTerms[0] is an array ... how can I tell php this?

Thanks,
sue yi

Apr 13 '06 #1
4 1407
su****@gmail.com wrote:
Hello,

What is the syntax for telling php that a variable is an array, or an
array of arrays?

Let's say I have $arrTerms

/*
@param array of arrays $arrTerms
*/
function test($arrTerms) {
$anotherArray = array('1', '2');

$intersect = array_intersect($anotherArray, $arrTerms[0]);

}

This doesn't work and php tells me I'm trying to access $arrTerms[0] as
a array though it is not.

But actually, since $arrTerms is supposed to be an array of arrays --
$arrTerms[0] is an array ... how can I tell php this?


To tell if a variable holds an array--use is_array(). To force a
variable into being an array, do (array) $var or settype($var,
'array').

Apr 13 '06 #2
Hi,

This is exactly the sort of answer I wanted ... thank you!! =)

Apr 14 '06 #3
Chung Leong wrote:
What is the syntax for telling php that a variable is an array, or an
array of arrays?


You can also type hint it:

function test(array $arrTerms) {

This requires PHP 5.x, however.

--
Carl Vondrick
Web-Engineer
www.carlsoft.net
Apr 14 '06 #4
On Thu, 13 Apr 2006 15:20:10 -0700, Chung Leong wrote:
To tell if a variable holds an array--use is_array(). To force a
variable into being an array, do (array) $var or settype($var,
'array').


Of course, type casting prevents you from seeing mistakes. I prefer to
kill the script if the scalar is passed instead of array. For that, there
is an "exit" function: if (!is_array($var)) {
exit(" var should be an array at line:".__LINE__);
}
--
http://www.mgogala.com

Apr 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Gord | last post: by
3 posts views Thread by Dave | last post: by
9 posts views Thread by Ian Richardson | last post: by
5 posts views Thread by greenflame | last post: by
18 posts views Thread by usr.root | last post: by
10 posts views Thread by jeniffer | last post: by
7 posts views Thread by icosahedron | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.