473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

quick question

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
7 1993
*** 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

"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.