detection of 2d array? [message #183442] |
Thu, 24 October 2013 21:39 |
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 |
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 |
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 |
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 |
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 |
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 |
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
==================
|
|
|