Help needed with Multi-Dimensional Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonathantheron
Forum Newbie
Posts: 2
Joined: Thu Oct 13, 2011 12:22 pm

Help needed with Multi-Dimensional Array

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help needed with Multi-Dimensional Array

Post 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.
jonathantheron
Forum Newbie
Posts: 2
Joined: Thu Oct 13, 2011 12:22 pm

Re: Help needed with Multi-Dimensional Array

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help needed with Multi-Dimensional Array

Post by Celauran »

Sounds like joins are what you want, then.
Post Reply