Mombu the Php Forum sponsored links

Go Back   Mombu the Php Forum > Php > array/iteration issue!!
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 24th May 11:00
bedouglas
External User
 
Posts: 1
Default array/iteration issue!!



hi.

i have the following test multidiminsional array. i'm trying to figure out
how to iterate through the array, to produce something like

foo, physics, sss
foo, physics, sffgg
foo, english, sss
foo, english, sffgg

can't quite seem to get it right!!

thoughts/comments... etc...

thanks

--------------------
$a=array("college"=> "foo",
"dept"=>array("dept"=> "physics",
"class"=>array("class1"=>"sss","class2"=>"sffg g")
),
"dept"=>array("dept"=> "english",
"class"=>array("class1"=>"sss","class2"=>"sffg g")
)
);
  Reply With Quote


  sponsored links


2 24th May 11:00
robert
External User
 
Posts: 1
Default array/iteration issue!!



You can't. You're array is valid but the second 'dept' key overwrites
the first. Thus the physics dept is lost.

Check it for yourself... print_r( $a )

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
  Reply With Quote
3 24th May 11:00
bedouglas
External User
 
Posts: 1
Default array/iteration issue!!


hey robert..

ok.. so if i changed the array to have a dept1, and a dept2

$a=array("college"=> "foo",
"dept1"=>array("dept"=> "physics",
"class"=>array("class1"=>"sss","class2"=>"sffg g")
),
"dept2"=>array("dept"=> "english",
"class"=>array("class1"=>"sss","class2"=>"sffg g")
)
);
how would i iterate through this..??

thanks


-----Original Message-----
From: Robert ***mings [mailto:robert@interjinn.com]
Sent: Thursday, November 27, 2008 6:18 PM
To: bruce
Cc: 'PHP General list'
Subject: Re: [php] array/iteration issue!!

You can't. You're array is valid but the second 'dept' key overwrites
the first. Thus the physics dept is lost.

Check it for yourself... print_r( $a )

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
  Reply With Quote
4 24th May 11:00
robert
External User
 
Posts: 1
Default array/iteration issue!!


Your array is terribly structured. But the following provides traversal
in the way you want:

<?php
$a = array (
"college" => "foo",
"dept1" => array (
"dept" => "physics",
"class" => array (
"class1" => "sss",
"class2" => "sffgg" ) ),
"dept2" => array (
"dept" => "english",
"class" => array (
"class1" => "sss",
"class2" => "sffgg"
)
)
); $college = $a['college'];
foreach( $a as $deptKey => $deptInfo )
{
if( strpos( $deptKey, 'dept' ) === 0 )
{
$dept = $deptInfo['dept'];
foreach( $deptInfo['class'] as $class )
{
echo "$college, $dept, $class\n";
} } }
?>

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
  Reply With Quote
5 24th May 11:00
bedouglas
External User
 
Posts: 1
Default array/iteration issue!!


hey robert!!

thanks. and yeah, you're right, it's not the best.. so tell me, given that
i'm ripping through this on the fly, and i can have the structure in any way
i choose. this is just to simulate/populate some test tbls.. what's a better
way to create an array structure to have a collegename, followed by some
deptnames, followed by some classnames for the depts...

perhaps something like this??

$a = array
(
"college" => "foo",
array
(
"dept" => "physics",
"class" => array
(
"class1" => "sss",
"class2" => "sffgg"
)
),
array
(
"dept" => "english",
"class" => array
(
"class1" => "sss",
"class2" => "sffgg"
)
)
);


-----Original Message-----
From: Robert ***mings [mailto:robert@interjinn.com]
Sent: Thursday, November 27, 2008 7:10 PM
To: bruce
Cc: 'PHP General list'
Subject: RE: [php] array/iteration issue!!

Your array is terribly structured. But the following provides traversal
in the way you want:

<?php
$a = array (
"college" => "foo",
"dept1" => array (
"dept" => "physics",
"class" => array (
"class1" => "sss",
"class2" => "sffgg" ) ),
"dept2" => array (
"dept" => "english",
"class" => array (
"class1" => "sss",
"class2" => "sffgg"
)
)
); $college = $a['college'];
foreach( $a as $deptKey => $deptInfo )
{
if( strpos( $deptKey, 'dept' ) === 0 )
{
$dept = $deptInfo['dept'];
foreach( $deptInfo['class'] as $class )
{
echo "$college, $dept, $class\n";
} } }
?>

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
  Reply With Quote
6 24th May 11:00
robert
External User
 
Posts: 1
Default array/iteration issue!!


Not quite. The following is probably what you want:

<?php

$colleges = array
( array (
'name' => 'Blah Blah University',
'depts' => array
( array (
'name' => 'physics',
'classes' => array
(
'sss',
'sffgg',
),
), array (
'name' => 'english',
'classes' => array
(
'sss',
'sffgg',
),
),
),
), array (
'name' => 'Glah Gleh University',
'depts' => array
( array (
'name' => 'physics',
'classes' => array
(
'sss',
'sffgg',
),
), array (
'name' => 'english',
'classes' => array
(
'sss',
'sffgg',
),
),
),
),
);

foreach( $colleges as $college )
{
$collegeName = $college['name'];
foreach( $college['depts'] as $dept )
{
$deptName = $dept['name'];
foreach( $dept['classes'] as $className )
{
echo "$collegeName, $deptName, $className\n";
} } }
?>

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
  Reply With Quote
7 24th May 11:00
micah
External User
 
Posts: 1
Default array/iteration issue!!


This is actually a much smaller data structure.
$colleges = array (
'Blah Blah University' => array (
'physics' => array
(
'sss', 'sffgg', ),
'english' => array
(
'sss',
'sffgg', ) ),
'Glah Gleh University' => array (
'physics' => array
(
'sss', 'sffgg', ),
'english' => array
(
'sss',
'sffgg',
),
) );
foreach( $colleges as $collegeName => $depts ) {
foreach( $depts as $deptName => $classes)
{
foreach( $classes as $className )
{
echo "$collegeName, $deptName, $className\n";
}
}
}


Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com
  Reply With Quote
8 24th May 11:00
bedouglas
External User
 
Posts: 1
Default array/iteration issue!!


much props guys!!!

thanks!!


-----Original Message-----
From: Micah Gersten [mailto:micah@onshore.com]
Sent: Thursday, November 27, 2008 8:23 PM
To: Robert ***mings
Cc: bruce; 'PHP General list'
Subject: Re: [php] array/iteration issue!!

This is actually a much smaller data structure.
$colleges = array (
'Blah Blah University' => array (
'physics' => array
(
'sss', 'sffgg', ),
'english' => array
(
'sss',
'sffgg', ) ),
'Glah Gleh University' => array (
'physics' => array
(
'sss', 'sffgg', ),
'english' => array
(
'sss',
'sffgg',
),
) );
foreach( $colleges as $collegeName => $depts ) {
foreach( $depts as $deptName => $classes)
{
foreach( $classes as $className )
{
echo "$collegeName, $deptName, $className\n";
}
}
}


Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
  Reply With Quote
9 24th May 11:00
robert
External User
 
Posts: 1
Default array/iteration issue!!


Yes, I thought of that one too, but it's less flexible. What if you need
to add other fields to the college, or department. Then you'd need to
redo the whole structure to something more similar to what I did. Since
bruce was having issues, I gave him a flexible format that could handle
other fields if they arose.

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
  Reply With Quote
10 24th May 11:00
bedouglas
External User
 
Posts: 1
Default array/iteration issue!!


both ways work...!!!

this is a really simple/quick issue to populate some test db tbls.. i've
only played with multi php arrays (sp??) in passing... and my days of being
a serious eng/software guy are long over!!

although.. with this economy!

thanks


-----Original Message-----
From: Robert ***mings [mailto:robert@interjinn.com]
Sent: Thursday, November 27, 2008 8:40 PM
To: Micah Gersten
Cc: bruce; 'PHP General list'
Subject: Re: [php] array/iteration issue!!


Yes, I thought of that one too, but it's less flexible. What if you need
to add other fields to the college, or department. Then you'd need to
redo the whole structure to something more similar to what I did. Since
bruce was having issues, I gave him a flexible format that could handle
other fields if they arose.

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP


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


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666