Page 1 of 1

Help needed with Multi-Dimensional Array

Posted: Thu Oct 13, 2011 12:38 pm
by jonathantheron
I have the following array of data:
Array ( [0] => Array ( [uid] => 1850867519 [name] => Anton Johannes Engelbrecht ) [1] => Array ( [uid] => 615856900 [name] => Jaco Venter ) [2] => Array ( [uid] => 100000136860374 [name] => Abrie Marais ) )

I am needing to get the data out of this array, so that I can display it, but I need to be able to access the “uid” and “name” elements separately.

I have used the following code:

Code: Select all

$myArray   =   array( array( uid => '1850867519',  name => 'Anton Johannes Engelbrecht'  ), array( uid => '615856900',  name => 'Jaco Venter' ), array( uid => '100000136860374',  name => 'Abrie Marais' )  );
	  
		foreach ($myArray as $k => $v) {
  
  			if(is_array($v)) {
    			    foreach ($v as $key => $val) {
      				echo '<br />'. $key .' :: '. $val .'';
    			    }
  			}
  			else echo '<br />'. $k. ' - '. $v;
		}
I currently get the following displayed:
uid :: 1850867519
name :: 'Anton Johannes Engelbrecht
uid :: 615856900
name :: 'Jaco Venter
uid :: 100000136860374
name :: Abrie Marais

How do I get the individual elements data - ie. name, so that I can use it in another query, while the loop is running?

Re: Help needed with Multi-Dimensional Array

Posted: Thu Oct 13, 2011 12:42 pm
by Celauran
jonathantheron wrote:How do I get the individual elements data - ie. name, so that I can use it in another query, while the loop is running?
You could execute the query inside the loop, you could store the names in a separate array to build a query later. Depending on where the original data is coming from, you could possibly also rework your original query using joins. All depends what you're trying to do.

Re: Help needed with Multi-Dimensional Array

Posted: Thu Oct 13, 2011 12:48 pm
by jonathantheron
I am building an app for Facebook - an I have been pulling my hair out with this one... I am checking to see which friends have subscribed to the app, and who hasn't.

I need to get the UID out so that I can run a query to get other data about the person, and I need to retrieve the NAME so that I can display it.

Re: Help needed with Multi-Dimensional Array

Posted: Thu Oct 13, 2011 1:37 pm
by Celauran
Sounds like joins are what you want, then.