php-general Digest 14 Dec 2011 11:48:47 -0000 Issue 7609

Topics (messages 315991 through 316006):

Re: Split
        315991 by: Daniel Brown
        315992 by: Dajka Tamás

Unique items in an array
        315993 by: Marc Guay
        315994 by: Marc Guay
        315995 by: Ashley Sheridan
        315996 by: Matijn Woudt
        315997 by: Marc Guay
        315998 by: Lester Caine

Question about socket_select
        315999 by: Mihai Anghel
        316000 by: Matijn Woudt
        316006 by: Mihai Anghel

Re: How to use a variable variable in an array walk?
        316001 by: Nils Leideck
        316002 by: Al

How to use a variable variable with an array
        316003 by: Nils Leideck
        316004 by: Laruence
        316005 by: FeIn

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

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


----------------------------------------------------------------------
--- Begin Message ---
On Tue, Dec 13, 2011 at 15:33, Jack <jacklistm...@gmail.com> wrote:
> OK so I have seen enough errors about split, so I decided to update my code:
>
>  return split("/", $sPath, $iMax);
>
>
>
> I tried:
>
> return preg_split("/", $sPath, $iMax);
>
> return preg_split("/", $sPath, $iMax, PREG_SPLIT_DELIM_CAPTURE);
>
>
>
> and a few other combinations, in the end always with errors. not sure I get
> the additional aspect.
>
> I do see ther error log it tells me no ending delimiter
>
>
>
> Any help appreciated.

    That's because those functions use regexp strings to split.
Besides, split() itself is deprecated as of 5.3.  Instead, what you're
looking for is explode().  Give that a try and you should be good to
go.

-- 
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/

--- End Message ---
--- Begin Message ---
Hi,

First, read the help of 'preg_replace' at php.net.

Second: try this: preg_split('/\//',$sPath,$iMax)
Third: use explode: explode('/',....)

Cheers,

        Tamas



2011.12.13. dátummal, 21:33 időpontban Jack <jacklistm...@gmail.com> írta:

> OK so I have seen enough errors about split, so I decided to update my code:
> 
>  return split("/", $sPath, $iMax);
> 
> 
> 
> I tried:
> 
> return preg_split("/", $sPath, $iMax);
> 
> return preg_split("/", $sPath, $iMax, PREG_SPLIT_DELIM_CAPTURE);
> 
> 
> 
> and a few other combinations, in the end always with errors. not sure I get
> the additional aspect.
> 
> I do see ther error log it tells me no ending delimiter
> 
> 
> 
> Any help appreciated.
> 
> 
> 
> 
> 
> Thanks!
> 
> Jack
> 
> 
> 

--- End Message ---
--- Begin Message ---
Hi folks,

Let's say that I have the following array:

  [0]=>
  array(35) {
    ["contact_id"]=>
    string(3) "356"
    ["contact_first_name"]=>
    string(4) "Marc"
 }
  [1]=>
  array(35) {
    ["contact_id"]=>
    string(3) "247"
    ["contact_first_name"]=>
    string(4) "Marc"
  }
  [2]=>
  array(35) {
    ["contact_id"]=>
    string(3) "356"
    ["contact_first_name"]=>
    string(4) "Marc"
 }

And I would like to filter out exact duplicates, such as key 0 and key
2 in this example, leaving me with an array containing only unique
entries.  How would you go about it?

Thanks for any help,
Marc

--- End Message ---
--- Begin Message ---
Holy mother of multidimentionality there are a ton of suggested
solutions to this scenerio on the array_unique manual page..

http://php.net/manual/en/function.array-unique.php

--- End Message ---
--- Begin Message ---
On Tue, 2011-12-13 at 16:15 -0500, Marc Guay wrote:

> Hi folks,
> 
> Let's say that I have the following array:
> 
>   [0]=>
>   array(35) {
>     ["contact_id"]=>
>     string(3) "356"
>     ["contact_first_name"]=>
>     string(4) "Marc"
>  }
>   [1]=>
>   array(35) {
>     ["contact_id"]=>
>     string(3) "247"
>     ["contact_first_name"]=>
>     string(4) "Marc"
>   }
>   [2]=>
>   array(35) {
>     ["contact_id"]=>
>     string(3) "356"
>     ["contact_first_name"]=>
>     string(4) "Marc"
>  }
> 
> And I would like to filter out exact duplicates, such as key 0 and key
> 2 in this example, leaving me with an array containing only unique
> entries.  How would you go about it?
> 
> Thanks for any help,
> Marc
> 


If I knew exactly what each sub-array were to contain (with regards to
keys) and it was small enough, I could compare those. For your example,
I would imagine just looping through and comparing the contact_id value,
as that seems to be what you'd consider the primary key in database
terms. If the array elements were a bit more unknown, and you needed to
check for exact duplicates, because for example, the forename
(contact_first_name) changed, then you could serialise the array
elements and then compare those. I don't know if serialising the arrays
in this context would alter if the natural index of an array differed
though without testing (and I'm too lazy to do that for you ;-p )

-- 
Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
On Tue, Dec 13, 2011 at 10:15 PM, Marc Guay <marc.g...@gmail.com> wrote:
> Hi folks,
>
> Let's say that I have the following array:
>
>  [0]=>
>  array(35) {
>    ["contact_id"]=>
>    string(3) "356"
>    ["contact_first_name"]=>
>    string(4) "Marc"
>  }
>  [1]=>
>  array(35) {
>    ["contact_id"]=>
>    string(3) "247"
>    ["contact_first_name"]=>
>    string(4) "Marc"
>  }
>  [2]=>
>  array(35) {
>    ["contact_id"]=>
>    string(3) "356"
>    ["contact_first_name"]=>
>    string(4) "Marc"
>  }
>
> And I would like to filter out exact duplicates, such as key 0 and key
> 2 in this example, leaving me with an array containing only unique
> entries.  How would you go about it?
>
> Thanks for any help,
> Marc

If the contact_id is the primary key, you could also use that as array
index, which would automatically filter duplicates.
If you want to filter out duplicate names after that, you could use
array_unique for that.

Matijn

--- End Message ---
--- Begin Message ---
> If the contact_id is the primary key, you could also use that as array
> index, which would automatically filter duplicates.

Thanks for this, it's so obvious I didn't see it.  I was adding items
to the array with $array[] = $contact when I could have just as easily
used $array[$contact['id']] = $contact;

Cheerios,
Marc

--- End Message ---
--- Begin Message ---
Marc Guay wrote:
If the contact_id is the primary key, you could also use that as array
index, which would automatically filter duplicates.

Thanks for this, it's so obvious I didn't see it.  I was adding items
to the array with $array[] = $contact when I could have just as easily
used $array[$contact['id']] = $contact;

The other question might be where are you getting the information from ;)
I filter the data in the database before loading the array ...

--
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk//
Firebird - http://www.firebirdsql.org/index.php

--- End Message ---
--- Begin Message ---
Hello,

It appears to me that something is strange with the socket_select function.
>From what I understand the value of the fourth parameter, tv_sec,
should block the execution of the script for that number of seconds.
I tried this code :
<?php

error_reporting(E_ERROR);

$serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

$result = socket_bind($serverSocket, "127.0.0.1", "20668");

$start = time();

while(true)
{
    $reads = array($serverSocket);
    $writes = null;
    $except = null;
    $changes = socket_select($reads, $writes, $except, 5);
    $now = time();
    echo $now - $start;
    echo "\n";
}

and when I run it with php -q server3.php the ouput shows something
like 0 0 0 0 0 1 1 1 1 1 2 2 2 2 etc so the script doesn't pause on
socket_select until it returns.

Cam somebody explain me what's happening ?

Thanks

--- End Message ---
--- Begin Message ---
On Wed, Dec 14, 2011 at 12:11 AM, Mihai Anghel <mihaigrim1...@gmail.com> wrote:
> Hello,
>
> It appears to me that something is strange with the socket_select function.
> From what I understand the value of the fourth parameter, tv_sec,
> should block the execution of the script for that number of seconds.
> I tried this code :
> <?php
>
> error_reporting(E_ERROR);
>
> $serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
>
> $result = socket_bind($serverSocket, "127.0.0.1", "20668");
>
> $start = time();
>
> while(true)
> {
>    $reads = array($serverSocket);
>    $writes = null;
>    $except = null;
>    $changes = socket_select($reads, $writes, $except, 5);
>    $now = time();
>    echo $now - $start;
>    echo "\n";
> }
>
> and when I run it with php -q server3.php the ouput shows something
> like 0 0 0 0 0 1 1 1 1 1 2 2 2 2 etc so the script doesn't pause on
> socket_select until it returns.
>
> Cam somebody explain me what's happening ?
>

It seems to me that your socket_select function is failing, maybe
because earlier code is failing. Check the return of socket_select
like this:
if ($changes === false) {
    echo "socket_select() failed, reason: " .
        socket_strerror(socket_last_error()) . "\n";
}

Cheers,

Matijn

--- End Message ---
--- Begin Message ---
On Wed, Dec 14, 2011 at 1:25 AM, Matijn Woudt <tijn...@gmail.com> wrote:
> On Wed, Dec 14, 2011 at 12:11 AM, Mihai Anghel <mihaigrim1...@gmail.com> 
> wrote:
>> Hello,
>>
>> It appears to me that something is strange with the socket_select function.
>> From what I understand the value of the fourth parameter, tv_sec,
>> should block the execution of the script for that number of seconds.
>> I tried this code :
>> <?php
>>
>> error_reporting(E_ERROR);
>>
>> $serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
>>
>> $result = socket_bind($serverSocket, "127.0.0.1", "20668");
>>
>> $start = time();
>>
>> while(true)
>> {
>>    $reads = array($serverSocket);
>>    $writes = null;
>>    $except = null;
>>    $changes = socket_select($reads, $writes, $except, 5);
>>    $now = time();
>>    echo $now - $start;
>>    echo "\n";
>> }
>>
>> and when I run it with php -q server3.php the ouput shows something
>> like 0 0 0 0 0 1 1 1 1 1 2 2 2 2 etc so the script doesn't pause on
>> socket_select until it returns.
>>
>> Cam somebody explain me what's happening ?
>>
>
> It seems to me that your socket_select function is failing, maybe
> because earlier code is failing. Check the return of socket_select
> like this:
> if ($changes === false) {
>    echo "socket_select() failed, reason: " .
>        socket_strerror(socket_last_error()) . "\n";
> }
>
> Cheers,
>
> Matijn

Thanks for your suggestion, I reviewed the code and I saw that I was
missing :  socket_listen($serverSocket) . After adding this it worked
like expected

--- End Message ---
--- Begin Message ---
Anyone?    :-(

is my description too unclear?

On 11.12.2011, at 11:25, Nils Leideck wrote:

> this is my first post to the PHP general list.
> I have an issue with a variable variable 
> (http://php.net/manual/en/language.variables.variable.php)
> 
> My use case:
> 
> I have an array called $myArray. The structure is as following:
> 
> array(1) {
>  ["user_interface"]=>
>  array(1) {
>    ["design"]=>
>    array(1) {
>      [“my_colors"]=>
>      array(5) {
>        ["item_number_one"]=>
>        string(6) "red"
>        ["item_number_two"]=>
>        string(40) 
> '["user_interface"]["design"]["my_colors"]["item_number_one"]'
>     }
>    }
>  }
> }
> 
> As you can see, the item_number_one has no direct color value assigned but 
> the structure of the path to item_number_one in the $myArray variable. I 
> tried with array_wal_resursive. During this step (the array building is 
> completed) I want to find these values (I use a static value in my example, 
> in the real code I will use regular expressions) and assign the value of the 
> virtually related item to the considered item.
> 
> So in my example above, I want to have the following values after the process 
> is done:
> 
> $myArray[user_interface][design][my_colors][item_mumber_one] = red; // this 
> is item number 1
> $myArray[user_interface][design][my_colors][item_mumber_two] = red; // this 
> should be item number 2
> 
> The second issue here is, how do I evaluate at which point the process is 
> exactly, because the value and the key that is transferred to the function by 
> array_walk_recursive has only the value itself but not array path to the 
> current item.
> 
> Any idea how get this done? Or am I too complicated maybe?
> 
> I tried several combinations of ${$var}, $myArray{$var}, {$myArray}{$var} ... 
> and many more.
> 
> Any help is much much appreciated!

Cheers, Nils
-- 
http://webint.cryptonode.de / a Fractal project


--- End Message ---
--- Begin Message ---


On 12/13/2011 5:43 PM, Nils Leideck wrote:
Anyone?    :-(

is my description too unclear?

On 11.12.2011, at 11:25, Nils Leideck wrote:

this is my first post to the PHP general list.
I have an issue with a variable variable 
(http://php.net/manual/en/language.variables.variable.php)

My use case:

I have an array called $myArray. The structure is as following:

array(1) {
  ["user_interface"]=>
  array(1) {
    ["design"]=>
    array(1) {
      [“my_colors"]=>
      array(5) {
        ["item_number_one"]=>
        string(6) "red"
        ["item_number_two"]=>
        string(40) 
'["user_interface"]["design"]["my_colors"]["item_number_one"]'
     }
    }
  }
}

As you can see, the item_number_one has no direct color value assigned but the 
structure of the path to item_number_one in the $myArray variable. I tried with 
array_wal_resursive. During this step (the array building is completed) I want 
to find these values (I use a static value in my example, in the real code I 
will use regular expressions) and assign the value of the virtually related 
item to the considered item.

So in my example above, I want to have the following values after the process 
is done:

$myArray[user_interface][design][my_colors][item_mumber_one] = red; // this is 
item number 1
$myArray[user_interface][design][my_colors][item_mumber_two] = red; // this 
should be item number 2

The second issue here is, how do I evaluate at which point the process is 
exactly, because the value and the key that is transferred to the function by 
array_walk_recursive has only the value itself but not array path to the 
current item.

Any idea how get this done? Or am I too complicated maybe?

I tried several combinations of ${$var}, $myArray{$var}, {$myArray}{$var} ... 
and many more.

Any help is much much appreciated!

Cheers, Nils

I'm short of time to conjure this in detail; but, on the surface it seems like nested foreach()s would do the trick.

foreach($myArray as $key1 => $userArray)
{
    foreach($userArray as $key2 => $designArray)
    {
        foreach($designArray as $key3 => $colorsArray)
        {
           foreach($colorsArray as $key4=>$itemsArray){
               //do stuff here. All keys are available

                }
            }
        }
    }
}





--- End Message ---
--- Begin Message ---
Hi Al,

many thanks for your feedback. Unfortunately I don’t know the deepness of the 
array so I can’t use the nested foreach() idea :-( Let me try to simplify my 
question (which also helps myself to clarify my process ;-)

I have a variable where the value is a string that is exactly the path to the 
array value:

[code]
    $arrayPath = "['user_interface’][‘design']['my_colors']['item_number_one’]”;
[/code]

And I have an array that has all the details:

[code]
    $myArray = coolFunction(‘getArray’);
[/code]

The question is, how can I use these both informations to return a value from 
the $myArray?

Examples I tried unsuccessfully:

[code]
    [...]
    echo $myArray$arrayPath;
    echo $myArray{$arrayPath};
    echo $myArray${arrayPath};
    echo $myArray${$arrayPath};
    echo $myArray.$arrayPath;
    echo $myArray.$arrayPath;
    [...]
[/code]
    etc...

your feedback is much much much appreciated!!!

Cheers, Nils
-- 
http://webint.cryptonode.de / a Fractal project


--- End Message ---
--- Begin Message ---
On Wed, Dec 14, 2011 at 9:27 AM, Nils Leideck <nils.leid...@leidex.net> wrote:
> Hi Al,
>
> many thanks for your feedback. Unfortunately I don’t know the deepness of the 
> array so I can’t use the nested foreach() idea :-( Let me try to simplify my 
> question (which also helps myself to clarify my process ;-)
>
> I have a variable where the value is a string that is exactly the path to the 
> array value:
>
> [code]
>    $arrayPath = 
> "['user_interface’][‘design']['my_colors']['item_number_one’]”;
> [/code]
>
> And I have an array that has all the details:
>
> [code]
>    $myArray = coolFunction(‘getArray’);
> [/code]
>
> The question is, how can I use these both informations to return a value from 
> the $myArray?
>
> Examples I tried unsuccessfully:
>
> [code]
>    [...]
>    echo $myArray$arrayPath;
>    echo $myArray{$arrayPath};
>    echo $myArray${arrayPath};
>    echo $myArray${$arrayPath};
>    echo $myArray.$arrayPath;
>    echo $myArray.$arrayPath;
>    [...]
> [/code]
>    etc...
>
> your feedback is much much much appreciated!!!
>
> Cheers, Nils
> --
> http://webint.cryptonode.de / a Fractal project

for you quesion, try eval:
<?php
$arr = array("a"=>array("b"=>array("c")));
$key = "['a']'['b]";
$value = eval("return \$arr". $key .";");
?>

ps: your requirement is a little odd :)

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



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

--- End Message ---
--- Begin Message ---
maybe something like this if you are willing to get rid of your weird array
dimension delimiters (you can use a dot for that for example, see below)

$a = array(
    "a" => array(
        "b" => array(
            "c" => array(
                "d" => array(
                    "e" => array(
                        "f" => 1
                    )
                )
            )
        )
    )
);

function coolFunction( array $array, $search, $separator = '.' )
{
    if ( empty( $array ) ) {
        return false;
    }

    if ( strlen( $search ) === 0 ) {
        return false;
    }

    $dimensions = explode( $separator, $search, 2 );
    $dimension = array_shift( $dimensions );

    if ( isset( $array[$dimension] ) ) {
        // no more dimensions to search
        if ( empty( $dimensions ) ) {
            return $array[$dimension];
        }

        // more dimensions to search but we can only go further if
        // the current dimension value is also an array
        if ( is_array( $array[$dimension] ) ) {
            return coolFunction( $array[$dimension], $dimensions[0] );
        } else {
            return false;
        }
    } else {
        return false;
    }
}

var_dump( coolFunction( $a, "a.b" ) );
var_dump( coolFunction( $a, "a.b.d.e.c" ) );
var_dump( coolFunction( $a, "a.b.c.d.e.f" ) );
var_dump( coolFunction( $a, "a.b.c.d.e.f.g" ) );

On Wed, Dec 14, 2011 at 3:27 AM, Nils Leideck <nils.leid...@leidex.net>wrote:

> Hi Al,
>
> many thanks for your feedback. Unfortunately I don’t know the deepness of
> the array so I can’t use the nested foreach() idea :-( Let me try to
> simplify my question (which also helps myself to clarify my process ;-)
>
> I have a variable where the value is a string that is exactly the path to
> the array value:
>
> [code]
>    $arrayPath =
> "['user_interface’][‘design']['my_colors']['item_number_one’]”;
> [/code]
>
> And I have an array that has all the details:
>
> [code]
>    $myArray = coolFunction(‘getArray’);
> [/code]
>
> The question is, how can I use these both informations to return a value
> from the $myArray?
>
> Examples I tried unsuccessfully:
>
> [code]
>    [...]
>    echo $myArray$arrayPath;
>    echo $myArray{$arrayPath};
>    echo $myArray${arrayPath};
>    echo $myArray${$arrayPath};
>    echo $myArray.$arrayPath;
>    echo $myArray.$arrayPath;
>    [...]
> [/code]
>    etc...
>
> your feedback is much much much appreciated!!!
>
> Cheers, Nils
> --
> http://webint.cryptonode.de / a Fractal project
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---

Reply via email to