Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multidimensional array issue

Status
Not open for further replies.

DreamerZ

Programmer
Jul 11, 2001
254
0
0
US
I have a multidimensional array. When I try to extract values from the array, the first array, 3 deep (0, bolded) gets skipped everytime. Here's a sampling of the array:

Array
(
[1104566400] => Array
(
[sat] => 1104566400
[strSat] => 01/01/2005
[group] => A
[0] => Array
(
[opOff] => CR2956
[opODay] => 1104220800
[strODay] => Tue
)


[1] => Array
(
[opOff] => DR1464
[opODay] => 1104480000
[strODay] => Fri
)

[2] => Array
(
[opOff] => NT2412
[opODay] => 1104220800
[strODay] => Tue
)

And here's the code to extract the data:
Code:
  foreach($sch as $key)
  {
    $tmpSch = "";
    $tmpSch .= $key["sat"]."^".$key["group"]."^";
    foreach ($key as $data=>$value)
    {
	if ($data != "sat" && $data != "strSat" && $data != "group")
	{
	  $tmpSch .= $value["opOff"]."_".$value["opODay"]."^";
	}
    }
    $writeSch[] = $tmpSch;
  }
  echo "writeSch:<pre>"; print_r($writeSch); echo "</pre>";

The $writeSch results in the following:
writeSch:
Array
(
[0] => 1104566400^A^DR1464_1104480000^NT2412_1104220800^
)

Notice the first value in the 3rd level, CR2956, is not present. WHY?? Running through the whole array duplicates the problem. Each value in the writeSch array shows the first value in the sch array skipped.

I changed the code to this:
Code:
  foreach($sch as $key)
  {
    $tmpSch = "";
//    $tmpSch .= $key["sat"]."^".$key["group"]."^";
    foreach ($key as $data=>$value)
    {
	if ($data == "sat") { $tmpSch .= $value."^"; }
	elseif ($data == "group") { $tmpSch .= $value."^"; }
	elseif($data == "strSat") { }
	else //if ($data != "sat" && $data != "strSat" && $data != "group")
	{
	  $tmpSch .= $value["opOff"]."_".$value["opODay"]."^";
	}
    }
    $writeSch[] = $tmpSch;
  }
  echo "writeSch:<pre>"; print_r($writeSch); echo "</pre>";
And this is the response:
Array
(
[0] => 1104566400^A^Array^DR1464_1104480000^NT2412_1104220800^
)
Notice now that the first value is listed as an array. Huh?? It is an array, but should be extracted like the other values.

What am I doing wrong? TIA

DreamerZ
 
Alright, it looks like I fixed it. Here's the new code:
Code:
  foreach($sch as $key=>$data)
  {
    $tmpSch = "";
    $tmpSch .= $data["sat"]."^".$data["group"]."^";
    for ($i=0; $i<count($data)-3; $i++)
    {
	$tmpSch .= $data[$i]["opOff"]."_".$data[$i]["opODay"]."^";
    }
    $writeSch[] = $tmpSch;
  }
  echo "writeSch:<pre>"; print_r($writeSch); echo "</pre>";
And here's the result:
Array
(
[0] => 1104566400^A^CR2956_1104220800^DR1464_1104480000^NT2412_1104220800^
)
Looks like the first value is now listed. Anything wrong with the new code? Let me know. Thanks,


DreamerZ
 
I recommend that you use a slightly different data structure for your data. Instead of having the numeric array elements (the [0], [1] and [2] from your post) in the same dimension as the other elements (the [sat], [strSat] and [group] from your post), I would have had the numeric elements inside an array in that dimension.

Like:
[tt]Array
(
[1104566400] => Array
(
[sat] => 1104566400
[strSat] => 01/01/2005
[group] => A
[red][some_other_data] => Array[/red]
(
[0] => Array
(
[opOff] => CR2956
[opODay] => 1104220800
[strODay] => Tue
)

[1] => Array
(
[opOff] => DR1464
[opODay] => 1104480000
[strODay] => Fri
)

[2] => Array
(
[opOff] => NT2412
[opODay] => 1104220800
[strODay] => Tue
)
[red])[/red][/tt]

That way, foreach() loops work on different dimensions of teh array.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top