FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » detection of 2d array?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
detection of 2d array? [message #183442] Thu, 24 October 2013 21:39 Go to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
given the example>>>

array=("item","photo"),
array=("item",array=("photo1","photo2")),
array=("item","photo")

Is there any way of knowing when an array item has a subarray item?
Re: detection of 2d array? [message #183443 is a reply to message #183442] Thu, 24 October 2013 22:10 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
On 24/10/13 23:39, richard wrote:
> given the example>>>
>
> array=("item","photo"),
> array=("item",array=("photo1","photo2")),
> array=("item","photo")
>
> Is there any way of knowing when an array item has a subarray item?
>
is_array() shoulr be able to tell you if a variable is an array

$array=array("item", array("photo1","photo2"));

foreach($array as $key => $cell)
if(is_array($cell))
echo "cell {$key} has an array\n";

see http://www.php.net/manual/en/function.is-array.php

--

//Aho
Re: detection of 2d array? [message #183444 is a reply to message #183443] Thu, 24 October 2013 22:12 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Fri, 25 Oct 2013 00:10:29 +0200, J.O. Aho wrote:

> On 24/10/13 23:39, richard wrote:
>> given the example>>>
>>
>> array=("item","photo"),
>> array=("item",array=("photo1","photo2")),
>> array=("item","photo")
>>
>> Is there any way of knowing when an array item has a subarray item?
>>
> is_array() shoulr be able to tell you if a variable is an array
>
> $array=array("item", array("photo1","photo2"));
>
> foreach($array as $key => $cell)
> if(is_array($cell))
> echo "cell {$key} has an array\n";
>
> see http://www.php.net/manual/en/function.is-array.php

thanks. I'll play with it and see what happens.
Re: detection of 2d array? [message #183448 is a reply to message #183442] Fri, 25 October 2013 14:58 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Thu, 24 Oct 2013 17:39:44 -0400, richard wrote:

> given the example>>>
>
> array=("item","photo"),
> array=("item",array=("photo1","photo2")),
> array=("item","photo")
>
> Is there any way of knowing when an array item has a subarray item?

Yes. PHP contains several functions that can be used to determine the
type of a variable, and as an array element is just another variable,
these can be applied at the array element level.

http://www.php.net/manual/en/ref.var.php

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: detection of 2d array? [message #183454 is a reply to message #183448] Sat, 26 October 2013 00:37 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Denis McMahon wrote:

> On Thu, 24 Oct 2013 17:39:44 -0400, richard wrote:
>> given the example>>>
>>
>> array=("item","photo"),
>> array=("item",array=("photo1","photo2")),
>> array=("item","photo")

… which is not even syntactically valid PHP code …

>> Is there any way of knowing when an array item has a subarray item?
>
> Yes. PHP contains several functions that can be used to determine the
> type of a variable, and as an array element is just another variable,
> these can be applied at the array element level.
>
> http://www.php.net/manual/en/ref.var.php

Nonsense again. An array element is _not_ a variable. The correct answer
has been given by J.O. Aho.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
Re: detection of 2d array? [message #183647 is a reply to message #183442] Wed, 06 November 2013 13:24 Go to previous messageGo to next message
Struchkov Vladimir is currently offline  Struchkov Vladimir
Messages: 1
Registered: November 2013
Karma: 0
Junior Member
I write short test for 2d dimension


$a=array(1,2,3,4=>array(3,4,5,7=>array(1)));
if (preg_match('/;a:[0-9]+:{/',serialize($a)))
{
echo 'I have more than one dimensions';
}
else
{
echo 'I have one demension';
}
Re: detection of 2d array? [message #183656 is a reply to message #183647] Wed, 06 November 2013 22:04 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 11/6/2013 8:24 AM, Struchkov Vladimir wrote:
> I write short test for 2d dimension
>
>
> $a=array(1,2,3,4=>array(3,4,5,7=>array(1)));
> if (preg_match('/;a:[0-9]+:{/',serialize($a)))
> {
> echo 'I have more than one dimensions';
> }
> else
> {
> echo 'I have one demension';
> }
>

He was asking if a certain element of the first array is an array or
not; that's what is_array() is for.

J.O. has the best answer.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: detecting a file in use
Next Topic: Mysqli or PDO Persistent prepared statements
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 20:08:16 GMT 2024

Total time taken to generate the page: 0.03315 seconds