Jump to content

Default key values alignment behaviour of a multidimensional array


sajan

Recommended Posts

can anybody tell me how to change the default alignment of values in a multidimensinal array.i think the default alignment is towards left and if any vacancy arise in array the right that value is pushed to left.how can we stop this and make the values in their exact position.eg:let assume we have an array of 3*33[0][0] has value 3[0][1] has no value and 3[0][2] has value ,the value of 3[0][2] has moved to 3[0][1] position while displaying.this become very annoying especially when we display geneaology tree.can anybody help me?

Link to comment
Share on other sites

the value of 3[0][2] has moved to 3[0][1] position while displaying.
did that move only when displaying or in array aswellmaybe when you're outputting the array you should check for empty positions and echo some or something when they are emptytry printing the array like this and you can see what's going on
echo '<pre>';print_r($array);echo '</pre>';

Link to comment
Share on other sites

i checked empty valuesmy code is

$status="";  $userid="";  $name=""; if(!empty($level3[0]['userid'])){$userid=$level3[0]['userid'];} $filter="userid='$userid'";$data=$objUser->select('useratb',$filter);foreach($data as $val){$status=$val['status'];$name= $val['name'];} $image	=$objUser->getimage($userid,$status); echo $image; echo $userid;if(!empty($name)) {   echo '('.$name.')';  } ?>  </td>	  <td align="center" width="25%"> <?php$status="";  $userid="";  $name="";if(!empty($level3[1]['userid'])){$userid=$level3[1]['userid'];} $filter="userid='$userid'";$data=$objUser->select('useratb',$filter);foreach($data as $val){$status=$val['status'];$name= $val['name'];} $image	=$objUser->getimage($userid,$status); echo $image; echo $userid;if(!empty($name)) {   echo '('.$name.')';  } ?>  </td>	  <td align="center" width="25%"> <?php$status="";  $userid="";  $name="";if(!empty($level3[2]['userid'])){$userid=$level3[2]['userid'];} $filter="userid='$userid'";$data=$objUser->select('useratb',$filter);foreach($data as $val){$status=$val['status'];$name= $val['name'];} $image	=$objUser->getimage($userid,$status); echo $image; echo $userid; if(!empty($name)) {   echo '('.$name.')';  } ?>  </td>	  <td align="center" width="25%"> <?php  $status="";  $userid="";  $name="";if(!empty($level3[3]['userid'])){$userid=$level3[3]['userid'];} $filter="userid='$userid'";$data=$objUser->select('useratb',$filter);foreach($data as $val){$status=$val['status'];$name= $val['name'];} $image	=$objUser->getimage($userid,$status); echo $image; echo $userid;  if(!empty($name)) {   echo '('.$name.')';  } ?>

when i print using print_ri got

Array ( [0] => Array ( [userid] => 1004 ) ) Array ( [0] => Array ( [userid] => 1004 ) [1] => Array ( [userid] => 1007 ) )

and userid 1007 moved to fill the position.Actually 1007 shoulbe displayed on dimension 3[2]['userid']how to rectify it?

Link to comment
Share on other sites

Nobody knows about to rectify this problem?
You might consider pre-loading the array with dummy values (either null or -1 or something along those lines). Then, when you print out the array, the structure should remain as you expect it to and when you see those dummy values, you know they are effectively 'empty'.
Link to comment
Share on other sites

Arrays do not work like this - if you have an array, and you delete an element in the middle, the elements higher up will not be re-indexed. Look at this code:

<?php$array = array('a', 'b', 'c', 'd', 'e', 'f', 'g');print_r($array);unset($array[2]);print_r($array);?>

It populates the array with letters, prints it, deletes (unset) one of the elements, and prints it again. This is the output:

Array(	[0] => a	[1] => b	[2] => c	[3] => d	[4] => e	[5] => f	[6] => g)Array(	[0] => a	[1] => b	[3] => d	[4] => e	[5] => f	[6] => g)

Notice in the second array the elements do not get re-indexed. Arrays do not work that way, that would be a bad thing if they did. So...

i think the default alignment is towards left and if any vacancy arise in array the right that value is pushed to left.how can we stop this and make the values in their exact position.
The values do remain in their exact position, whereever you put them. Arrays do not have an "alignment", an array is a data structure that is essentially a list of elements. Arrays in the C language are implemented where the base element of the array (element at offset 0) is associated with a certain address in memory. If the array is holding 16-bit integers, then the computer knows that element 1 is 16 bits after element 0, element 2 is 32 bits after element 0, element 3 is 48 bits after, etc. That is how arrays got stored, the element offset is a byte offset from the start of the element so that the computer knows where in memory you are talking about.PHP doesn't use that type of architecture to store arrays, but you can't change the order, it wouldn't make sense. It would not make sense to a computer to reference an array based on the end of it, because the computer doesn't know where the end is, it only knows where the beginning is.So, about your question. How are you deleting, or otherwise causing a "vacancy", in the array?
Link to comment
Share on other sites

thanks for describing the structure of array clearly.But my problem remains .how should that userid 1007 displayed on the place 3[1]['userid'] instead of 3[2]['userid']. Let me put my question clearly3[0]['userid'] and 3[1]['userid'] are children of 2[0]['userid'] 3[2]['userid'] and 3[3]['userid'] are children of 2[1]['userid']here 3[1]['userid'] has no value because 2[0]['userid'] has only one child.1007 is the child of 2[1]['userid'] and so it should be in the place of 3[2]['userid'].since 3[1]['userid'] has no value 1007 (that is 3[2]['userid']) is displayed on the place of 3[1]['userid'].that is 1007 displayed as the child of 2[0]['userid'] instead of 2[1]['userid'].Now the geneaology tree display(remember only display problem,the parent id of concerned is correct)become wrong.how's that happening? Can u pls check my code and help me find the solution?

Link to comment
Share on other sites

I think i am a step closer to sove the problem.I think it actually happening in the sql query used to pull the data from database to level3 array.There lies the problem.can anybody give me the correct query to get the values from table.(same table used to build the tree)

Link to comment
Share on other sites

If you're building a tree, you might want to consider using a tree structure instead of an array. This would require some knowledge of object-oriented programming and data structures, but it would almost certainly be far more efficient. In order to do that, you would need a "node" object, which would represent any node on the tree. The node should have a reference to the parent node (null if the node is the root node), and an array of references to child nodes. A basic class declaration might look something like this:

<?phpclass TreeNode{  public $parent = null;  public $children = array();  public $name = "";  function __construct($name = "")  {	$this->name = $name;  }  function set_parent(&$node)  {	$this->parent =& $node;  }  function add_child(&$node)  {	$this->children[] =& $node;  }    function count_children()  {	return count($this->children);  }}$root = new TreeNode("root");$node1 = new TreeNode("node 1");$root->add_child($node1);$node2 = new TreeNode("node 2");$node1->add_child($node2);?>

So, if I were trying to build a tree, I would use a tree structure. I'm trying to understand your problem with the arrays, but it's not very clear to me. Like this:

3[0]['userid'] and 3[1]['userid'] are children of 2[0]['userid'] 3[2]['userid'] and 3[3]['userid'] are children of 2[1]['userid']here 3[1]['userid'] has no value because 2[0]['userid'] has only one child.
How is 3[2] a child of 2[1], what indicates that? Does 2[1] contain any information indicating that 3[2] is a child? How does the software know? You also just said that 3[1] is a child of 2[0], but then you say it has no value because 2[0] only has one child. I'm confused by how you chose to do this, it doesn't make a lot of sense to me. You have the array indices hard-coded, so I'm not sure what you're trying to do. A family tree would not have a limit on how many descendents or siblings anything can have, so I don't understand what you are doing with the hard-coded array elements.Maybe if you could display the results of print_r on a large array it would become clearer how you set this up. When using print_r, make sure to look at the page source, not what the browser shows.
Link to comment
Share on other sites

hi This is my codr in pear

 'geneology'	=>array(										'select'   =>'userprofile.userid AS lev1_userid,t2.userid AS lev2_userid,t3.userid as lev3_userid,t4.userid as lev4_userid',										'join'	=> 'LEFT JOIN userprofile AS t2 ON t2.uplineid = userprofile.userid														LEFT  JOIN userprofile AS t3 ON t3.uplineid = t2.userid														LEFT JOIN userprofile AS t4 ON t4.uplineid = t3.userid														LEFT JOIN userprofile AS t5 ON t5.uplineid = t4.userid',																						'get'		=> 'all'											)

and this is i access it

$Geneology=$objUser->select('geneology',"userprofile.userid='$url_userid'");foreach($Geneology as $data){		$row1=array('userid'=>$data['lev1_userid']);				if(!in_array($row1,$level1))		{			 $level1[]=array('userid'=>$data['lev1_userid']);						} 		$row2=array('userid'=>$data['lev2_userid']);		if(!in_array($row2,$level2))		{			 $level2[]=array('userid'=>$data['lev2_userid']);						} 		$row3=array('userid'=>$data['lev3_userid']);		if(!in_array($row3,$level3))		{			 $level3[]=array('userid'=>$data['lev3_userid']);						}

from there i access the data.Is there any correction required in this code?

Link to comment
Share on other sites

What do level1, level2, and level3 represent? What does the join condition mean, what does it mean when uplineid is a certain userid? What information does the query return? I know it's a list of userids, but how are they related?

Link to comment
Share on other sites

What do level1, level2, and level3 represent? What does the join condition mean, what does it mean when uplineid is a certain userid? What information does the query return? I know it's a list of userids, but how are they related?
uplineid is the parent of userid.eg:1005 has a child node with userid 1007.Then 1007's uplineid is 1005.1007 has a child node userid 1009.then uplineid of 1009 is 1007.here $level1 is 1005,$level2 is 1007 and $level3 is 1009.1005 may have another child say 1006.(so as 1005,1007,1009).All working perfectly and correct genealogy display i am getting except when there is an above mentioned problem.Table is userprofile and leftjoins on same table,so we can get the children upto 3 level deep.(condition in JOIN,upo t4 joined).thanks
Link to comment
Share on other sites

The problem might be because the query is only returning one "set" per row, so any one row would not contain all of the children, it would only contain one child per row. So the first row might say that user 1 has child 5, and the next row says that user 1 has child 6. They wouldn't both be part of the same row, because any one row can only have 1 immediate descendent.This is screaming for a recursive function. You will have to change your array structure to something a little more complete instead of just an array for parents, children, and grandchildren, but a recursive function to get each person's descendents would be the correct way to handle this.What is the purpose of this, what are you doing with the data? Are you displaying the entire tree, or displaying information about a certain person, or what are you doing?

Link to comment
Share on other sites

Hi Thanks for your reply.I used this for displaying tree for multilevel marketing.it is necessary for the parent to know who are the children,grand children of him.so it is a must to display the correct tree.This code return all children of each parent.But problem is if a parent has only one child and the child of another parent in the same level displayed under the first parent(This in no way affected the parent child relations in calculations.).i would like to know what causes this problem?

Link to comment
Share on other sites

I sort of know what you're talking about, but it would clear things up to see a big example. If you have enough data in the database for a good example, then print the $Geneology, $level1, $level2, and $level3 arrays. That would help to clarify how the information comes back from the database and how it gets stored in the other arrays, since you already posted the code that does that part. Make sure if you're using print_r or var_dump to copy the browser source, not what the browser shows, since the browser ignores the line breaks that print_r outputs.

Link to comment
Share on other sites

hi This the print_r report in source for genealogy ,$level1,$level2, $level3.

Array(	[0] => Array		(			[lev1_userid] => 1001			[lev2_userid] => 1002			[lev3_userid] => 1004			[lev4_userid] => 		)	[1] => Array		(			[lev1_userid] => 1001			[lev2_userid] => 1003			[lev3_userid] => 1007			[lev4_userid] => 		)	[2] => Array		(			[lev1_userid] => 1001			[lev2_userid] => 1003			[lev3_userid] => 1008			[lev4_userid] => 		))Array(	[0] => Array		(			[userid] => 1001		))Array(	[0] => Array		(			[userid] => 1002		))Array(	[0] => Array		(			[userid] => 1004		))Array(	[0] => Array		(			[userid] => 1002		)	[1] => Array		(			[userid] => 1003		))Array(	[0] => Array		(			[userid] => 1004		)	[1] => Array		(			[userid] => 1007		))Array(	[0] => Array		(			[userid] => 1004		)	[1] => Array		(			[userid] => 1007		)	[2] => Array		(			[userid] => 1008		))

From its clear that id 1007 belongs to 1003.But it displayed under 1002.

Link to comment
Share on other sites

This is a really confusing way to do this stuff. Instead of having a bunch of arrays that all try and represent a tree, it would be more logical to just make a tree structure. The way it is now, you just printed 7 arrays and they mean something somehow (the IDs in array 5 are children of the IDs in array 2, I think, but maybe not), but there is no link between anything.If you're dead-set on using arrays for this, then I would suggest you have a single array for everything, with the user IDs as the array keys. Each element would be a nested array with the list of children or anything else you want to list. Based on the array you printed here:

Array(	[0] => Array		(			[lev1_userid] => 1001			[lev2_userid] => 1002			[lev3_userid] => 1004			[lev4_userid] => 		)	[1] => Array		(			[lev1_userid] => 1001			[lev2_userid] => 1003			[lev3_userid] => 1007			[lev4_userid] => 		)	[2] => Array		(			[lev1_userid] => 1001			[lev2_userid] => 1003			[lev3_userid] => 1008			[lev4_userid] => 		))

it would be set up like this:

Array(  [1001] => Array  (	[name] => "user name"	[parent] => 0	[children] => Array	(	  [0] => 1002	  [1] => 1003	)  )    [1002] => Array  (	[name] => "user name"	[parent] => 1001	[children] => Array	(	  [0] => 1004	)  )  [1003] => Array  (	[name] => "user name"	[parent] => 1001	[children] => Array	(	  [0] => 1007	  [1] => 1008	)  ))

That way everything is related, you can tell for any person who their parents and children are very easily, there is no confusion. The way you're doing it now I'm not real sure what to say, the problem might be caused by people who have more then one child only being listed once, so the next child of another parent takes the next space in the parent list and then 2 children who should go to the same parent are going to different parents. You may be able to mess around with your code and figure out how to make it do what you want to do, but I would recommend just doing something that is less confusing to start with. All the extra arrays just complicate things. For example, I asked you to post the output for the $Geneology, $level1, $level2, and $level3 arrays, and you posted seven arrays. I don't know which they are or what they represent, I can only guess, and that's part of the problem. So I recommend getting rid of the confusion altogether and redesigning the thing to be less confusing to begin with, which would have the side effect of eliminating your current problem, since it's based on confusion anyway.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...