php-general Digest 22 Dec 2006 11:47:59 -0000 Issue 4528

Topics (messages 246138 through 246145):

Re: Count empty array
        246138 by: Robert Cummings
        246139 by: Jon Anderson
        246140 by: Martin Marques
        246141 by: tg-php.gryffyndevelopment.com

Re: parsing objects
        246142 by: Marcus
        246144 by: Jochem Maas

Re: random selection from each subfolder
        246143 by: chris smith

Re: RegExp
        246145 by: WeberSites LTD

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        php-general@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On Thu, 2006-12-21 at 13:31 -0800, Kevin Murphy wrote:
> I'm wondering why this is.
> 
> $data = "";
> $array = explode(",",$data);
> $count = count($array);
> $count will = 1
> 
> $data = "Test";
> $array = explode(",",$data);
> $count = count($array);
> $count will = 1
> 
> $data = "Test,Test";
> $array = explode(",",$data);
> $count = count($array);
> $count will = 2
> 
> Why doesn't the first one give me an answer of 0 instead of 1.

For the same reason the second one gives you a count of one. There are
no commas and so only one item exists. Whether that be an string with
content or the empty string itself is irrelevant.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
Kevin Murphy wrote:
I'm wondering why this is.

$data = "";
$array = explode(",",$data);
$count = count($array);
$count will = 1

$data = "Test";
$array = explode(",",$data);
$count = count($array);
$count will = 1

$data = "Test,Test";
$array = explode(",",$data);
$count = count($array);
$count will = 2

Why doesn't the first one give me an answer of 0 instead of 1.
This:
   var_dump(explode(',',''));
Returns this:
   array(1) {
     [0]=>
     string(0) ""
   }

And oddly, This:
   var_dump(explode(',',NULL));
Returns this:
   array(1) {
     [0]=>
     string(0) ""
   }

That explains the count() result. It seems to me that the first case could go either way (The part of "" before the ',' is still ""), but I'm unable to think of a logical reason why the second doesn't return an empty array.
I know I could do a IF $data == "[empty]" and then not count if its empty and just set it to 0, but am wondering if there was a better way.
$array = empty($data) ? array() : explode(',',$data); /* That what you're looking for? */

jon

--- End Message ---
--- Begin Message ---
On Thu, 21 Dec 2006, Kevin Murphy wrote:

I'm wondering why this is.

$data = "";
$array = explode(",",$data);
$count = count($array);
$count will = 1

$array has 1 element: An empty string.

$data = "Test";
$array = explode(",",$data);
$count = count($array);
$count will = 1

$array has 1 element: The string "Test"

$data = "Test,Test";
$array = explode(",",$data);
$count = count($array);
$count will = 2

$array has 2 elements:.....

Why doesn't the first one give me an answer of 0 instead of 1. I know I could do a IF $data == "[empty]" and then not count if its empty and just set it to 0, but am wondering if there was a better way.

Because explode divides the string in n+1 elements, where n is the amount of "," (or your favorite delimiter) found in the string. So if no "," is found, explode will return 1 element: the whole string (even if it's empty).

--
 21:50:04 up 2 days,  9:07,  0 users,  load average: 0.92, 0.37, 0.18
---------------------------------------------------------
Lic. Martín Marqués         |   SELECT 'mmarques' ||
Centro de Telemática        |       '@' || 'unl.edu.ar';
Universidad Nacional        |   DBA, Programador,
    del Litoral             |   Administrador
---------------------------------------------------------

--- End Message ---
--- Begin Message ---
Not sure why it does it, but doesn't seem to be a huge deal.  I'm guessing it's 
because an empty string is still a string. It's not null.

Anyway, it's documented at:

http://us3.php.net/manual/en/function.explode.php

A user writes:

"If you split an empty string, you get back a one-element array with 0 as the 
key and an empty string for the value."

-TG

= = = Original message = = =

I'm wondering why this is.

$data = "";
$array = explode(",",$data);
$count = count($array);
$count will = 1

$data = "Test";
$array = explode(",",$data);
$count = count($array);
$count will = 1

$data = "Test,Test";
$array = explode(",",$data);
$count = count($array);
$count will = 2

Why doesn't the first one give me an answer of 0 instead of 1. I know  
I could do a IF $data ==  "[empty]" and then not count if its empty  
and just set it to 0, but am wondering if there was a better way.


-- 
Kevin Murphy
Webmaster: Information and Marketing Services
Western Nevada Community College
www.wncc.edu
775-445-3326


___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

--- End Message ---
--- Begin Message ---
I need to output that as

Category:  XXXXX

   Sub-category:  yyyy1
   Sub-category: yyyy2

..
..

linking by id's to parent id's just like in a database.



-----Original Message-----
From: Roman Neuhauser [mailto:[EMAIL PROTECTED]
Sent: Friday, December 22, 2006 12:18 AM
To: Marcus
Cc: php-general@lists.php.net
Subject: Re: [PHP] parsing objects


# [EMAIL PROTECTED] / 2006-12-21 18:06:28 +0200:
> Hello
>
> I have a soap call that returns something like;
>
> Result from a print_r();
>
> stdClass Object ( [getCategoryTreeReturn] => Array ( [0] => stdClass
Object
> ( [iId] => 1 [sName] => Cars & Motorbikes [iParentId] => 0 [iTreeCount] =>
> 114302 [iLocalCount] => 0 [aSubCats] => Array ( [0] => stdClass Object (
> [iId] => 2 [sName] => Car Electronics [iParentId] => 1 [iTreeCount] => 0
> [iLocalCount] => 0 ) [1] => stdClass Object ( [iId] => 5 [sName] => Car
> Accessories [iParentId] => 1 [iTreeCount] => 57160 [iLocalCount] =>
57142 )
> [2] => stdClass Object ( [iId] => 16 [sName] => Motorcycles [iParentId] =>
1
> [iTreeCount] => 11 [iLocalCount] => 0 )
>
> I need to convert that to a multidimensional array to link ID's to Parent
> ID's.

What makes you think so?

--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE.             http://bash.org/?255991

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--- End Message ---
--- Begin Message ---
Marcus,

some facts:

1. you CAN return 'proper' values from a recursive function call.
2. you DONT need a recursive function call to retrieve the properties
of these objects.
3. you DONT need a recursive function to build a nested data structure
(using the parent<->child ID relationship)

with regard to number 3 try having a look at the following code
snippet (I leave it as an exercise to you to figure out how to
translate it to your own situation (my example happens to
use arrays because that's how the data was retrieved from a DB,
you'll want to stick with the collection of objects you get bgack from
the SOAP call - the principle is the same):

the trick is the use of the $parents 'stack' (array) in conjunction
with references to track parents ...

    // return data array (will contain a nested structure)
    $data = array();

    $parents = array();
    while(!empty($rows)) {
        foreach ($rows as $key => $row) {
            // add some tree related elements with default values.
            $row['selected']      = false;
            $row['childselected'] = false;
            $row['children']      = array();

            if (!$row['parentid']) {
                $dCount          = count($data);
                $data[ $dCount ] = $row;
                $parents[ $row['nodeid'] ] =& $data[ $dCount ];
            } else if (isset($parents[ $row['parentid'] ])) {
                $dCount          = count($parents[ $row['parentid'] 
]['children']);
                $parents[ $row['parentid'] ]['children'][ $dCount ] = $row;
                $parents[ $row['nodeid'] ] =& $parents[ $row['parentid'] 
]['children'][ $dCount ];
            }
            unset($rows[$key]);
        }
    }

Marcus wrote:
> Hello
> 
> I have a soap call that returns something like;
> 
> Result from a print_r();
> 
> stdClass Object ( [getCategoryTreeReturn] => Array ( [0] => stdClass Object
> ( [iId] => 1 [sName] => Cars & Motorbikes [iParentId] => 0 [iTreeCount] =>
> 114302 [iLocalCount] => 0 [aSubCats] => Array ( [0] => stdClass Object (
> [iId] => 2 [sName] => Car Electronics [iParentId] => 1 [iTreeCount] => 0
> [iLocalCount] => 0 ) [1] => stdClass Object ( [iId] => 5 [sName] => Car
> Accessories [iParentId] => 1 [iTreeCount] => 57160 [iLocalCount] => 57142 )
> [2] => stdClass Object ( [iId] => 16 [sName] => Motorcycles [iParentId] => 1
> [iTreeCount] => 11 [iLocalCount] => 0 )
> 
> I need to convert that to a multidimensional array to link ID's to Parent
> ID's. I have created a function which recursively scans the result variable.
> It writes the rows to a temporary file for later fetching, but it is
> somewhat slow. Can i maintain those recursive seek to an array. As function
> is being called inside and inside, i can not return a proper value. Is there
> any way to maintain those rows without writing to tmp.
> 
> 
> The function is :
> 
> --- BEGIN ---
> 
> function deepseek($val) {
> 
> foreach ($val as $key => $value) {
> 
>       if (is_object($value)) {
>       $newarr = get_object_vars($newarr);
>               foreach ($newarr as $key2 => $value2) {
>               deepseek($value2);
>               }
>       }
> 
>       if (is_array($value)) {
> 
>               foreach ($value as $key2 => $value2) {
>               deepseek($value2);
>               }
>       }
> 
>       if (!is_array($value) AND !is_object($value)) {
> 
>               if ($key == "iParentId" OR $key == "sName" OR $key == "iId") {
> 
>               $output .= "$value-";
> 
>               }
> 
>               if ($key == "iTreeCount") {
>               $handle = fopen("tmp/tmp","a");
>               fwrite($handle,$output."\n");
>               fclose($handle);
> 
>               unset($output);
> 
>               }
> 
> 
>       }
> 
> }
> 
> 
> }
> --- END ---
> 

--- End Message ---
--- Begin Message ---
On 12/22/06, Steven Macintyre <[EMAIL PROTECTED]> wrote:
Ok ... previous problem sorted ... now onto the next ...

The client wants the following;

A scroll bar with 3 random thumbs from each subdirectory in the /images/
folder

I was thinking ...

Going into each folder, getting files, pushing into an array - shuffling
array, taking first three items and combine it to a "master" array and use
that ...

As you get more & more images this will slow down, might take a while
but it will happen eventually.

Are you storing any info about images in a database (even just the
name and folder they appear in) ? You could use that to your
advantage..

--
Postgresql & php tutorials
http://www.designmagick.com/

--- End Message ---
--- Begin Message ---
I'm not trying to pre-populate the text area.
I just want to get the input from the user and make sure he can
only insert chars that I allow. The only char I have a problem with
is the " (Quote). If I enter a " into the text area the regexp bellow
does not allow it in.

preg_match("/^[à-úA-Za-z0-9_():,@\/\.\s\"- ]*$/i",$FieldValue) - Fails
preg_match("/^[à-úA-Za-z0-9_():,@\/\.\s\\"- ]*$/i",$FieldValue) (This one gives 
a php error cause the \\" closes the opening ")
preg_match('/^[א-תA-Za-z0-9_():,@\/\.\s"- ]*$/i',$FieldValue) - Fails
preg_match('/^[א-תA-Za-z0-9_():,@\/\.\s\"- ]*$/i',$FieldValue) - Fails
preg_match('/^[א-תA-Za-z0-9_():,@\/\.\s\\"- ]*$/i',$FieldValue) - Fails

So how can I allow a quote (") to be a valid char?

thanks
berber

-----Original Message-----
From: Richard Lynch [mailto:[EMAIL PROTECTED] 
Sent: Saturday, December 16, 2006 3:55 AM
To: WeberSites LTD
Cc: php-general@lists.php.net
Subject: Re: [PHP] RegExp

On Thu, December 14, 2006 11:47 pm, WeberSites LTD wrote:
> I'm trying to limit the text someone can submit in a text area with :
>
>
> Code:
> if(!preg_match("/^[à-úA-Za-z0-9_():,@\/\.\s\-\" ]*$/i",$FieldValue)) {
>
> }
>
>
> It works well but I'm having problems with the " (double quote).
> If there is a double quote (") it fails.

Fails in what way?

Are you sure you are remembering to do http://php.net/htmlentities on the data 
you send out for the TEXTAREA to pre-populate it?

Does PCRE need " escaped inside the [] bit?  I think not, but I suppose \\" 
instead of \" couldn't hurt.

You may also want to move the - to the end of [] character set and lose the \, 
just to keep it simpler.

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: 
http://www.php.net/unsub.php

--- End Message ---

Reply via email to