By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,347 Members | 1,390 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,347 IT Pros & Developers. It's quick & easy.

quick question

P: n/a
Hi,

I've got a multidimensional array and I can loop through it fine but I can't
get the values of first parts of the array - ie:

foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}

I can print $v2 straight out, but if I try to echo $v1 I get 'Array', I
realise that it is an array nested within the original array but it still
has a value which I need to determine.

Thanks
Alex

Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
*** Alex Hopson wrote/escribió (Mon, 21 Jun 2004 17:18:49 +0100):
I can print $v2 straight out, but if I try to echo $v1 I get 'Array'


What would you need to print instead?
--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #2

P: n/a

"Alex Hopson" <al*********************@hotmail.com> wrote in message
news:vN****************@fe09.usenetserver.com...
Hi,

I've got a multidimensional array and I can loop through it fine but I can't get the values of first parts of the array - ie:

foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}

I can print $v2 straight out, but if I try to echo $v1 I get 'Array', I
realise that it is an array nested within the original array but it still
has a value which I need to determine.

Thanks
Alex

That's because $v1 is an Array. If you want to see all the contents of it
try print_r($v1);
Jul 17 '05 #3

P: n/a
On Mon, 21 Jun 2004 17:18:49 +0100, Alex Hopson wrote:
Hi,

I've got a multidimensional array and I can loop through it fine but I can't
get the values of first parts of the array - ie:

foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}

I can print $v2 straight out, but if I try to echo $v1 I get 'Array', I
realise that it is an array nested within the original array but it still
has a value which I need to determine.

Thanks
Alex


You can't echo out an entire array. You just said it yourself that you
realize that $v1 is an array.

You can only use the indexed elements.

You are doing it correctly, above, if you want to iterate through all
items in the second-level array. But if you want to use a specific
element of the second-level array, use their index. For example:

<?php
$a = array(
array( "red", "green", "blue")
,array( "apple", "banana", "cherry")
);
?>

Using your above code, to get the second element in the second-dimension
arrays, you would use $v1[1], e.g.:

<?php
foreach ($a as $v1) {
echo $v1[1] . "<br>\n";
}
?>

That would echo:

green<br>
banana<br>

Got it? Did I answer your question? I hope so.

Later...

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/

Jul 17 '05 #4

P: n/a
Hi,

That's not exactly what I'm looking for (probably shouldn't have been so
vague :( ). Here' my array that I've got:

array(2) { [19]=> array(1) { ["blue"]=> array(1) { [0]=> int(2) } } [20]=>
array(1) { ["green"]=> array(1) { [0]=> int(1) } } }

and the code:

$items=0
if(is_array($cart)) { //check cart is an array
foreach ($cart as $productid) { //loop through products
foreach ($productid as $colour) { //loop through colours
foreach ($colour as $option =>$qty) { //loop through options
$items += $qty; //add qtry to total
}
}
}
}

I'm using an indexed array (fr example the first product id is 19 and the
first (and only) colour for that id is 'blue' which then refrences to
another array with the options in.

What I'm after is the name of the index, ie the '19' and the 'blue'

I hope that's a little clearer :)

Thanks
Alex
"Jeffrey Silverman" <je*****@jhu.edu> wrote in message
news:pa****************************@jhu.edu...
On Mon, 21 Jun 2004 17:18:49 +0100, Alex Hopson wrote:
Hi,

I've got a multidimensional array and I can loop through it fine but I can't get the values of first parts of the array - ie:

foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}

I can print $v2 straight out, but if I try to echo $v1 I get 'Array', I
realise that it is an array nested within the original array but it still has a value which I need to determine.

Thanks
Alex


You can't echo out an entire array. You just said it yourself that you
realize that $v1 is an array.

You can only use the indexed elements.

You are doing it correctly, above, if you want to iterate through all
items in the second-level array. But if you want to use a specific
element of the second-level array, use their index. For example:

<?php
$a = array(
array( "red", "green", "blue")
,array( "apple", "banana", "cherry")
);
?>

Using your above code, to get the second element in the second-dimension
arrays, you would use $v1[1], e.g.:

<?php
foreach ($a as $v1) {
echo $v1[1] . "<br>\n";
}
?>

That would echo:

green<br>
banana<br>

Got it? Did I answer your question? I hope so.

Later...

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/


Jul 17 '05 #5

P: n/a
On Mon, 21 Jun 2004 18:02:00 +0100, Alex Hopson wrote:
I'm using an indexed array (fr example the first product id is 19 and the
first (and only) colour for that id is 'blue' which then refrences to
another array with the options in.

What I'm after is the name of the index, ie the '19' and the 'blue'

I hope that's a little clearer :)

Thanks
Alex


I'm still not completely clear, but the code in your post is not formatted
well in my newsreader. Also, it doesn't appear to be correct PHP code.
Try posting the exact code from your script, not from print_r().

Anyways, I think you need to use the "foreach ($array as $key=>$value)"
syntax:

<?php
$a = array("19"=>"blue", "20"=>"red");

foreach ($a as $k => $v){
echo "KEY is $k\n";
echo "VAL is $v\n";
}
?>

This would set $k to 19 and $v to "blue" for the first item and "20" and
"red" for the second.
--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/

Jul 17 '05 #6

P: n/a
Thanks. That's got it sorted now :D, I had tried that before but had the key
and the val round the wrong way.

Alex

"Jeffrey Silverman" <je*****@jhu.edu> wrote in message
news:pa****************************@jhu.edu...
On Mon, 21 Jun 2004 18:02:00 +0100, Alex Hopson wrote:
I'm using an indexed array (fr example the first product id is 19 and the first (and only) colour for that id is 'blue' which then refrences to
another array with the options in.

What I'm after is the name of the index, ie the '19' and the 'blue'

I hope that's a little clearer :)

Thanks
Alex


I'm still not completely clear, but the code in your post is not formatted
well in my newsreader. Also, it doesn't appear to be correct PHP code.
Try posting the exact code from your script, not from print_r().

Anyways, I think you need to use the "foreach ($array as $key=>$value)"
syntax:

<?php
$a = array("19"=>"blue", "20"=>"red");

foreach ($a as $k => $v){
echo "KEY is $k\n";
echo "VAL is $v\n";
}
?>

This would set $k to 19 and $v to "blue" for the first item and "20" and
"red" for the second.
--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/


Jul 17 '05 #7

P: n/a
Alex Hopson wrote:
Hi,

I've got a multidimensional array and I can loop through it fine but I can't
get the values of first parts of the array - ie:

foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}

I can print $v2 straight out, but if I try to echo $v1 I get 'Array', I
realise that it is an array nested within the original array but it still
has a value which I need to determine.

Thanks
Alex


Write a small function to recursively descend through the array, eg:

function show_array ( $s, $a )
{
echo '<table cellspacing="2" cellpadding="2 border="1" >
<tr><td>======&nbsp;' . $s . '&nbsp;======</td></tr>' ;
if ( ! is_array($a) )
{ echo '<br>Cannot show: not an array!<br>' ; return ; }
foreach ( $a as $k => $v )
{
echo '<tr>' ;
if ( is_array( $v ) )
{
show_array( $k, $v ) ;
}
else
echo '<td>' . $k . '</td><td>' . $v . '</td>' ;
echo '</tr>' ;
}
}
HTH,
Margaret
--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.