473,387 Members | 1,569 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,387 software developers and data experts.

Forech question (Sorry if duplicate)

I'm passing an array which contains 2 arrays as subarrays to a function
that uses foreach to iterate through each sub-array. Each subarray has
exactly 7 items.
function listitems($anArray){

// $anArray is 2-dim array
foreach($anArray as $item=>$arr){
var_dump($arr);

}

array(2) {
[0]=>
array(1) {
["X"]=>
string(4) "this"
}
[1]=>
array(1){
["y"]=>
string(3) "xyz"
}
Instead the items show up as scalars. Like this:
string[5]='string'
string[3]='xyz'

In short, I would expect the foreach to create an array containing 1
item from subarray[0] and subarray[1] and display it as such. So clearly
I don't understand something, but how do I reference each item?

May 11 '07 #1
3 1319
On May 11, 5:38 pm, Lorenzo Thurman <lore...@diespammerhethurmans.com>
wrote:
I'm passing an array which contains 2 arrays as subarrays to a function
that uses foreach to iterate through each sub-array. Each subarray has
exactly 7 items.
function listitems($anArray){

// $anArray is 2-dim array
foreach($anArray as $item=>$arr){
var_dump($arr);

}

array(2) {
[0]=>
array(1) {
["X"]=>
string(4) "this"
}
[1]=>
array(1){
["y"]=>
string(3) "xyz"}

Instead the items show up as scalars. Like this:
string[5]='string'
string[3]='xyz'

In short, I would expect the foreach to create an array containing 1
item from subarray[0] and subarray[1] and display it as such. So clearly
I don't understand something, but how do I reference each item?
I think you may be doing something wrong. It works for me. Or maybe I
don't understand your question. Here's what I tested:
<?php
$t = array(
array( 'a1', 'b1' ),
array( 'a2', 'b2' )
);

foreach( $t as $i =$v )
var_dump( $v );
?>

And here's what I get:
array(2) {
[0]=>
string(2) "a1"
[1]=>
string(2) "b1"
}
array(2) {
[0]=>
string(2) "a2"
[1]=>
string(2) "b2"
}

Is this test similar to yours?

-Mike PII

May 13 '07 #2
Mike P2 wrote:
On May 11, 5:38 pm, Lorenzo Thurman <lore...@diespammerhethurmans.com>
wrote:
>I'm passing an array which contains 2 arrays as subarrays to a function
that uses foreach to iterate through each sub-array. Each subarray has
exactly 7 items.
function listitems($anArray){

// $anArray is 2-dim array
foreach($anArray as $item=>$arr){
var_dump($arr);

}

array(2) {
[0]=>
array(1) {
["X"]=>
string(4) "this"
}
[1]=>
array(1){
["y"]=>
string(3) "xyz"}

Instead the items show up as scalars. Like this:
string[5]='string'
string[3]='xyz'

In short, I would expect the foreach to create an array containing 1
item from subarray[0] and subarray[1] and display it as such. So clearly
I don't understand something, but how do I reference each item?

I think you may be doing something wrong. It works for me. Or maybe I
don't understand your question. Here's what I tested:
<?php
$t = array(
array( 'a1', 'b1' ),
array( 'a2', 'b2' )
);

foreach( $t as $i =$v )
var_dump( $v );
?>

And here's what I get:
array(2) {
[0]=>
string(2) "a1"
[1]=>
string(2) "b1"
}
array(2) {
[0]=>
string(2) "a2"
[1]=>
string(2) "b2"
}

Is this test similar to yours?

-Mike PII
Your setup is the same, but the result is different. Its what I would
expect. I just get those unreferenced scalars.
May 14 '07 #3
On May 14, 8:02 am, Lorenzo Thurman <lore...@diespammerhethurmans.com>
wrote:
Your setup is the same, but the result is different. Its what I would
expect. I just get those unreferenced scalars.
Verify that the array you are passing to your function is like mine.

-Mike PII

May 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Giloosh | last post by:
Hello, i need some help if possible... i have a payments table with over 500 records i want to run a query that searches through the table spotting out any duplicate ID#'s and Dates. So basically...
3
by: Mohammed Mazid | last post by:
Hi, Basically I have a problem with registering to my quiz system. I had borrowed some code from an existing program but I just do not know why it doesn't work. If (txtUsername = "" Or...
44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
1
by: Gary Lundquest | last post by:
It appears to me that MySQL version 4 returns an error messge when doing an Insert that results in duplicate entries. Version 3 did NOT return an error - it dropped the duplicate entries and ran...
9
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
2
by: news | last post by:
I just upgraded to PHP 4.4.2 on my Slackware 10.2 system. And Apache/mySQL/PHP all work great through a browser. No errors. But when I try to run a PHP script through the command line, which I...
7
by: ucfcpegirl06 | last post by:
Hello, I have a dilemma. I am trying to flag duplicate messages received off of a com port. I have a software tool that is supposed to detect dup messages and flag and write the text "DUP" on...
4
by: FangQ | last post by:
hi I am very new to mysql. I have a question about using the "on duplicate update" clause with insert command. my table "data" has two columns, field1 and field2, where field1 is the index...
3
by: rajeshkrsingh | last post by:
Hi friends, Step1- create table duplicate ( intId int, varName varchar(50) ) insert into duplicate(intId,varName) values(1,'rajesh') insert into duplicate(intId,varName) values(2,'raj12')...
1
by: xraive | last post by:
I have a problem with this. Currently I am trying Allen's code and i am not successful. Current Design Table1 (Main Form) TravelID (PK) ApprovedBY EntreredBy BudgetCode ExpenseCode
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.