Connecting Tech Pros Worldwide Help | Site Map

using next() on multidimensional arrays

  #1  
Old June 19th, 2009, 03:04 PM
Member
 
Join Date: Jun 2009
Posts: 52
So I'm trying to use the next() function on my array within my multidimensional array. It looks like this:
Expand|Select|Wrap|Line Numbers
  1. Array(
  2.    testA=> Array(
  3.                  phaseA=>5,
  4.                  phaseB=>4,
  5.                  total=>9
  6.               ),
  7.   testB=>Array(
  8.                  phaseA=>3,
  9.                  phaseB=>3,
  10.                  total=>6
  11.               )
  12.       )
  13.  
so I'm trying to use the next function in the array with the phases to see if the next one is "total". What I currently have so far is this
Expand|Select|Wrap|Line Numbers
  1. if(next($sub2)=='total'){
  2.         echo "next";
  3.         }
  4.  
But for some reason, it never goes into this loop.=/ Please help me find out where I went wrong. Thanks in advance.
  #2  
Old June 19th, 2009, 05:04 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: using next() on multidimensional arrays


Hi.

The next() function returns the value of the element, not the key.

If you want to match against the key, you need to use the key() function.
Like:
Expand|Select|Wrap|Line Numbers
  1. do {
  2.     if(key($array) == 'total') {
  3.         echo "Total = " . current($array);
  4.     }
  5. } while(next());
  6.  
In that scenario, however, you could just as well do:
Expand|Select|Wrap|Line Numbers
  1. if(isset($array['total']) {
  2.     echo 'Total = '. $array['total'];
  3. }
  4.  
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
loading multidimensional control arrays vfiroiu answers 8 August 8th, 2007 03:38 PM
multidimensional array declaration in php vito answers 21 July 18th, 2006 05:35 PM
multidimensional arrays and cute tricks Charles Banas answers 9 November 14th, 2005 02:46 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM