Connecting Tech Pros Worldwide Help | Site Map

array problem

point
Guest
 
Posts: n/a
#1: Jul 17 '05
Hi there..

I have the folowing array

[3] => property[created by][0] = hexonet
[4] => property[created date][0] = 2003-12-01 18:46:20.0
[5] => property[updated by][0] = hexonet
[6] => property[updated date][0] = 2003-12-02 02:59:15.0
[7] => property[registrar][0] = hexonet
[8] => property[registration expiration date][0] = 2004-12-01 18:46:20.0
[9] => property[auth][0] = jptf0KZFTR
[10] => property[status][0] = ACTIVE

and would like to be parsed as array, meaning
[property] -> [createdby] -> [0] => hexonet
-> [created date]-> [0] => 2003-12-01 18:46:20.0
-> [updated by] -> [0] => hexonet

and so on...

Any help would be appriciated...

Thanx...



Shawn Wilson
Guest
 
Posts: n/a
#2: Jul 17 '05

re: array problem


point wrote:[color=blue]
>
> Hi there..
>
> I have the folowing array
>
> [3] => property[created by][0] = hexonet
> [4] => property[created date][0] = 2003-12-01 18:46:20.0
> [5] => property[updated by][0] = hexonet
> [6] => property[updated date][0] = 2003-12-02 02:59:15.0
> [7] => property[registrar][0] = hexonet
> [8] => property[registration expiration date][0] = 2004-12-01 18:46:20.0
> [9] => property[auth][0] = jptf0KZFTR
> [10] => property[status][0] = ACTIVE
>
> and would like to be parsed as array, meaning
> [property] -> [createdby] -> [0] => hexonet
> -> [created date]-> [0] => 2003-12-01 18:46:20.0
> -> [updated by] -> [0] => hexonet[/color]

How is your existing array created? It's probably easier to do what you're
talking about when the array is created, rather than convert it later. Also,
could you print_r the array to show us exactly what you've got?

Shawn
--
Shawn Wilson
shawn@glassgiant.com
http://www.glassgiant.com
point
Guest
 
Posts: n/a
#3: Jul 17 '05

re: array problem


Hi Shawn...

Well ..that is the original array...this is the print_r of the array...

I got this array by using file() function on a remote file and that's what
was returned...I guess I could use readfile() if you think that can help
bulding the final array I need...

I tried to explode the values on "=" and then put the left side of = to keys
and right side to values..meaning

[3] => property[created by][0] = hexonet

becomes
[property[created by][0]] => hexonet

and so on...but that's not the "real" array as in this case the
"property[created by][0]" part is treated as string not array...and i would
like to be treated as array so that with print_r I get the real array
structure....and I'm getting headache in the meanwhile :) ...

Thanx...

P.

"Shawn Wilson" <shawn@glassgiant.com> wrote in message
news:3FCF35E5.30FD5752@glassgiant.com...[color=blue]
> point wrote:[color=green]
> >
> > Hi there..
> >
> > I have the folowing array
> >
> > [3] => property[created by][0] = hexonet
> > [4] => property[created date][0] = 2003-12-01 18:46:20.0
> > [5] => property[updated by][0] = hexonet
> > [6] => property[updated date][0] = 2003-12-02 02:59:15.0
> > [7] => property[registrar][0] = hexonet
> > [8] => property[registration expiration date][0] = 2004-12-01[/color][/color]
18:46:20.0[color=blue][color=green]
> > [9] => property[auth][0] = jptf0KZFTR
> > [10] => property[status][0] = ACTIVE
> >
> > and would like to be parsed as array, meaning
> > [property] -> [createdby] -> [0] => hexonet
> > -> [created date]-> [0] => 2003-12-01 18:46:20.0
> > -> [updated by] -> [0] => hexonet[/color]
>
> How is your existing array created? It's probably easier to do what[/color]
you're[color=blue]
> talking about when the array is created, rather than convert it later.[/color]
Also,[color=blue]
> could you print_r the array to show us exactly what you've got?
>
> Shawn
> --
> Shawn Wilson
> shawn@glassgiant.com
> http://www.glassgiant.com[/color]


Shawn Wilson
Guest
 
Posts: n/a
#4: Jul 17 '05

re: array problem


point wrote:[color=blue]
>
> Hi Shawn...
>
> Well ..that is the original array...this is the print_r of the array...
>
> I got this array by using file() function on a remote file and that's what
> was returned...I guess I could use readfile() if you think that can help
> bulding the final array I need...
>
> I tried to explode the values on "=" and then put the left side of = to keys
> and right side to values..meaning
>
> [3] => property[created by][0] = hexonet
>
> becomes
> [property[created by][0]] => hexonet
>
> and so on...but that's not the "real" array as in this case the
> "property[created by][0]" part is treated as string not array...and i would
> like to be treated as array so that with print_r I get the real array
> structure....and I'm getting headache in the meanwhile :) ...[/color]

Well, you could do this (I'm not sure if I got the regular expression correct -
you may have to tinker with it):

$newarray = array();

foreach($yourarray as $key=>$val) {
newarray[preg_replace("/^property\[([^\]]+)\].*$/", "\$1", $key)] = $val;
}

Regards,
Shawn
--
Shawn Wilson
shawn@glassgiant.com
http://www.glassgiant.com
point
Guest
 
Posts: n/a
#5: Jul 17 '05

re: array problem


Hm....

You are a genious....I knew that this could be done with regexp and I played
with it but now I see I'll have to do a harder studying on that subject :)

The array is now ok but the problem is that the indexes [0] have been
removed and some properties have [0] and [1] like "nameserver" property so
now it shows

[nameserver] => NS2.DOMAINNAME.COM

becuse it overrided the [0] and NS1.DOMAINNAME.COM...

I played with this all evening but I just can't come up with a
solution...the soultion has to be intuitive not hardcoded b/c "we" don't
know which property will have more indexes (which one will be array)....

Can you help here!??

I really appriciate your help and your time....

p.

"Shawn Wilson" <shawn@glassgiant.com> wrote in message
news:3FCF42B7.78E199B0@glassgiant.com...[color=blue]
> point wrote:[color=green]
> >
> > Hi Shawn...
> >
> > Well ..that is the original array...this is the print_r of the array...
> >
> > I got this array by using file() function on a remote file and that's[/color][/color]
what[color=blue][color=green]
> > was returned...I guess I could use readfile() if you think that can help
> > bulding the final array I need...
> >
> > I tried to explode the values on "=" and then put the left side of = to[/color][/color]
keys[color=blue][color=green]
> > and right side to values..meaning
> >
> > [3] => property[created by][0] = hexonet
> >
> > becomes
> > [property[created by][0]] => hexonet
> >
> > and so on...but that's not the "real" array as in this case the
> > "property[created by][0]" part is treated as string not array...and i[/color][/color]
would[color=blue][color=green]
> > like to be treated as array so that with print_r I get the real array
> > structure....and I'm getting headache in the meanwhile :) ...[/color]
>
> Well, you could do this (I'm not sure if I got the regular expression[/color]
correct -[color=blue]
> you may have to tinker with it):
>
> $newarray = array();
>
> foreach($yourarray as $key=>$val) {
> newarray[preg_replace("/^property\[([^\]]+)\].*$/", "\$1", $key)] = $val;
> }
>
> Regards,
> Shawn
> --
> Shawn Wilson
> shawn@glassgiant.com
> http://www.glassgiant.com[/color]


Rahul Anand
Guest
 
Posts: n/a
#6: Jul 17 '05

re: array problem


"point" <point@caanNOproductionSPAM.com> wrote in message news:<bqn7d702r07@enews3.newsguy.com>...[color=blue]
> Hi there..
>
> I have the folowing array
>
> [3] => property[created by][0] = hexonet
> [4] => property[created date][0] = 2003-12-01 18:46:20.0
> [5] => property[updated by][0] = hexonet
> [6] => property[updated date][0] = 2003-12-02 02:59:15.0
> [7] => property[registrar][0] = hexonet
> [8] => property[registration expiration date][0] = 2004-12-01 18:46:20.0
> [9] => property[auth][0] = jptf0KZFTR
> [10] => property[status][0] = ACTIVE
>
> and would like to be parsed as array, meaning
> [property] -> [createdby] -> [0] => hexonet
> -> [created date]-> [0] => 2003-12-01 18:46:20.0
> -> [updated by] -> [0] => hexonet
>
> and so on...
>
> Any help would be appriciated...
>
> Thanx...[/color]

Hi ,

Try the code below: -

// Start
// $oldarr is the original array

$oldarr = array();
$oldarr[3] = "property[created by][0] = hexonet";
$oldarr[4] = "property[created date][0] = 2003-12-01 18:46:20.0";
$oldarr[5] = "property[updated by][0] = hexonet";
$oldarr[6] = "property[updated date][0] = 2003-12-02 02:59:15.0";
$oldarr[7] = "property[registrar][0] = hexonet";
$oldarr[8] = "property[registration expiration date][0] = 2004-12-01 18:46:20.0";
$oldarr[9] = "property[auth][0] = jptf0KZFTR";
$oldarr[10] = "property[status][0] = ACTIVE";

$newarr = array();
foreach($oldarr as $v)
{
list($key,$value) = explode(" = ",$v);
$key = str_replace(array("[","]"),array("['","']"),$key);
$str = '$'.$key.' = "'.$value.'";';
eval( $str);
}
print_r($property);

// $property is the new final array
// End

This code will show you the array $Property, as you want it to be.

Regards,

Rahul
point
Guest
 
Posts: n/a
#7: Jul 17 '05

re: array problem


Thanx Rahul and thanx Shawn...

you guys have been great...than for the help...

Respect...

p
"Rahul Anand" <rahulanand_bis@rediffmail.com> wrote in message
news:628e2f7b.0312042138.70a0f3ec@posting.google.c om...[color=blue]
> "point" <point@caanNOproductionSPAM.com> wrote in message[/color]
news:<bqn7d702r07@enews3.newsguy.com>...[color=blue][color=green]
> > Hi there..
> >
> > I have the folowing array
> >
> > [3] => property[created by][0] = hexonet
> > [4] => property[created date][0] = 2003-12-01 18:46:20.0
> > [5] => property[updated by][0] = hexonet
> > [6] => property[updated date][0] = 2003-12-02 02:59:15.0
> > [7] => property[registrar][0] = hexonet
> > [8] => property[registration expiration date][0] = 2004-12-01[/color][/color]
18:46:20.0[color=blue][color=green]
> > [9] => property[auth][0] = jptf0KZFTR
> > [10] => property[status][0] = ACTIVE
> >
> > and would like to be parsed as array, meaning
> > [property] -> [createdby] -> [0] => hexonet
> > -> [created date]-> [0] => 2003-12-01 18:46:20.0
> > -> [updated by] -> [0] => hexonet
> >
> > and so on...
> >
> > Any help would be appriciated...
> >
> > Thanx...[/color]
>
> Hi ,
>
> Try the code below: -
>
> // Start
> // $oldarr is the original array
>
> $oldarr = array();
> $oldarr[3] = "property[created by][0] = hexonet";
> $oldarr[4] = "property[created date][0] = 2003-12-01 18:46:20.0";
> $oldarr[5] = "property[updated by][0] = hexonet";
> $oldarr[6] = "property[updated date][0] = 2003-12-02 02:59:15.0";
> $oldarr[7] = "property[registrar][0] = hexonet";
> $oldarr[8] = "property[registration expiration date][0] = 2004-12-01[/color]
18:46:20.0";[color=blue]
> $oldarr[9] = "property[auth][0] = jptf0KZFTR";
> $oldarr[10] = "property[status][0] = ACTIVE";
>
> $newarr = array();
> foreach($oldarr as $v)
> {
> list($key,$value) = explode(" = ",$v);
> $key = str_replace(array("[","]"),array("['","']"),$key);
> $str = '$'.$key.' = "'.$value.'";';
> eval( $str);
> }
> print_r($property);
>
> // $property is the new final array
> // End
>
> This code will show you the array $Property, as you want it to be.
>
> Regards,
>
> Rahul[/color]


point
Guest
 
Posts: n/a
#8: Jul 17 '05

re: array problem


Hy guys...again me...

hm...

this works fine but it "eats" the first two array members(index 1 and 2)...

this is the start of origin array

[1] => code = 200
[2] => description = Command completed successfully
[3] => property[created by][0] = hexonet
[4] => property[created date][0] = 2003-12-01 18:46:20.0

and after parsing it I got a new array consisting ONLY from keys and values
which are under the "property" in original array..in this case "code" and
"description" are lost....


[created by] => Array
(
[0] => hexonet
)

[created date] => Array
(
[0] => 2003-12-01 18:46:20.0
)

[updated by] => Array
(
[0] => hexonet
)

[updated date] => Array
(
[0] => 2003-12-02 02:59:15.0
)

[registrar] => Array
(
[0] => hexonet
)

huh...this is getting depressing...I already changed my whole class around
that piece of code....

once again thanx for the help...

p

"point" <point@caanNOproductionSPAM.com> wrote in message
news:bqpm5f02264@enews4.newsguy.com...[color=blue]
> Thanx Rahul and thanx Shawn...
>
> you guys have been great...than for the help...
>
> Respect...
>
> p
> "Rahul Anand" <rahulanand_bis@rediffmail.com> wrote in message
> news:628e2f7b.0312042138.70a0f3ec@posting.google.c om...[color=green]
> > "point" <point@caanNOproductionSPAM.com> wrote in message[/color]
> news:<bqn7d702r07@enews3.newsguy.com>...[color=green][color=darkred]
> > > Hi there..
> > >
> > > I have the folowing array
> > >
> > > [3] => property[created by][0] = hexonet
> > > [4] => property[created date][0] = 2003-12-01 18:46:20.0
> > > [5] => property[updated by][0] = hexonet
> > > [6] => property[updated date][0] = 2003-12-02 02:59:15.0
> > > [7] => property[registrar][0] = hexonet
> > > [8] => property[registration expiration date][0] = 2004-12-01[/color][/color]
> 18:46:20.0[color=green][color=darkred]
> > > [9] => property[auth][0] = jptf0KZFTR
> > > [10] => property[status][0] = ACTIVE
> > >
> > > and would like to be parsed as array, meaning
> > > [property] -> [createdby] -> [0] => hexonet
> > > -> [created date]-> [0] => 2003-12-01 18:46:20.0
> > > -> [updated by] -> [0] => hexonet
> > >
> > > and so on...
> > >
> > > Any help would be appriciated...
> > >
> > > Thanx...[/color]
> >
> > Hi ,
> >
> > Try the code below: -
> >
> > // Start
> > // $oldarr is the original array
> >
> > $oldarr = array();
> > $oldarr[3] = "property[created by][0] = hexonet";
> > $oldarr[4] = "property[created date][0] = 2003-12-01 18:46:20.0";
> > $oldarr[5] = "property[updated by][0] = hexonet";
> > $oldarr[6] = "property[updated date][0] = 2003-12-02 02:59:15.0";
> > $oldarr[7] = "property[registrar][0] = hexonet";
> > $oldarr[8] = "property[registration expiration date][0] = 2004-12-01[/color]
> 18:46:20.0";[color=green]
> > $oldarr[9] = "property[auth][0] = jptf0KZFTR";
> > $oldarr[10] = "property[status][0] = ACTIVE";
> >
> > $newarr = array();
> > foreach($oldarr as $v)
> > {
> > list($key,$value) = explode(" = ",$v);
> > $key = str_replace(array("[","]"),array("['","']"),$key);
> > $str = '$'.$key.' = "'.$value.'";';
> > eval( $str);
> > }
> > print_r($property);
> >
> > // $property is the new final array
> > // End
> >
> > This code will show you the array $Property, as you want it to be.
> >
> > Regards,
> >
> > Rahul[/color]
>
>[/color]


point
Guest
 
Posts: n/a
#9: Jul 17 '05

re: array problem


I got it....

:)


"point" <point@caanNOproductionSPAM.com> wrote in message
news:bqpp1h0274v@enews4.newsguy.com...[color=blue]
> Hy guys...again me...
>
> hm...
>
> this works fine but it "eats" the first two array members(index 1 and[/color]
2)...[color=blue]
>
> this is the start of origin array
>
> [1] => code = 200
> [2] => description = Command completed successfully
> [3] => property[created by][0] = hexonet
> [4] => property[created date][0] = 2003-12-01 18:46:20.0
>
> and after parsing it I got a new array consisting ONLY from keys and[/color]
values[color=blue]
> which are under the "property" in original array..in this case "code" and
> "description" are lost....
>
>
> [created by] => Array
> (
> [0] => hexonet
> )
>
> [created date] => Array
> (
> [0] => 2003-12-01 18:46:20.0
> )
>
> [updated by] => Array
> (
> [0] => hexonet
> )
>
> [updated date] => Array
> (
> [0] => 2003-12-02 02:59:15.0
> )
>
> [registrar] => Array
> (
> [0] => hexonet
> )
>
> huh...this is getting depressing...I already changed my whole class around
> that piece of code....
>
> once again thanx for the help...
>
> p
>
> "point" <point@caanNOproductionSPAM.com> wrote in message
> news:bqpm5f02264@enews4.newsguy.com...[color=green]
> > Thanx Rahul and thanx Shawn...
> >
> > you guys have been great...than for the help...
> >
> > Respect...
> >
> > p
> > "Rahul Anand" <rahulanand_bis@rediffmail.com> wrote in message
> > news:628e2f7b.0312042138.70a0f3ec@posting.google.c om...[color=darkred]
> > > "point" <point@caanNOproductionSPAM.com> wrote in message[/color]
> > news:<bqn7d702r07@enews3.newsguy.com>...[color=darkred]
> > > > Hi there..
> > > >
> > > > I have the folowing array
> > > >
> > > > [3] => property[created by][0] = hexonet
> > > > [4] => property[created date][0] = 2003-12-01 18:46:20.0
> > > > [5] => property[updated by][0] = hexonet
> > > > [6] => property[updated date][0] = 2003-12-02 02:59:15.0
> > > > [7] => property[registrar][0] = hexonet
> > > > [8] => property[registration expiration date][0] = 2004-12-01[/color]
> > 18:46:20.0[color=darkred]
> > > > [9] => property[auth][0] = jptf0KZFTR
> > > > [10] => property[status][0] = ACTIVE
> > > >
> > > > and would like to be parsed as array, meaning
> > > > [property] -> [createdby] -> [0] => hexonet
> > > > -> [created date]-> [0] => 2003-12-01 18:46:20.0
> > > > -> [updated by] -> [0] => hexonet
> > > >
> > > > and so on...
> > > >
> > > > Any help would be appriciated...
> > > >
> > > > Thanx...
> > >
> > > Hi ,
> > >
> > > Try the code below: -
> > >
> > > // Start
> > > // $oldarr is the original array
> > >
> > > $oldarr = array();
> > > $oldarr[3] = "property[created by][0] = hexonet";
> > > $oldarr[4] = "property[created date][0] = 2003-12-01 18:46:20.0";
> > > $oldarr[5] = "property[updated by][0] = hexonet";
> > > $oldarr[6] = "property[updated date][0] = 2003-12-02 02:59:15.0";
> > > $oldarr[7] = "property[registrar][0] = hexonet";
> > > $oldarr[8] = "property[registration expiration date][0] = 2004-12-01[/color]
> > 18:46:20.0";[color=darkred]
> > > $oldarr[9] = "property[auth][0] = jptf0KZFTR";
> > > $oldarr[10] = "property[status][0] = ACTIVE";
> > >
> > > $newarr = array();
> > > foreach($oldarr as $v)
> > > {
> > > list($key,$value) = explode(" = ",$v);
> > > $key = str_replace(array("[","]"),array("['","']"),$key);
> > > $str = '$'.$key.' = "'.$value.'";';
> > > eval( $str);
> > > }
> > > print_r($property);
> > >
> > > // $property is the new final array
> > > // End
> > >
> > > This code will show you the array $Property, as you want it to be.
> > >
> > > Regards,
> > >
> > > Rahul[/color]
> >
> >[/color]
>
>[/color]


Closed Thread