0

Hello everyone, here is what I'm trying to do:

I want to create a multidimensional array of this form:

Array([0] => a, [1] => Array([0] => Array([0] => d, [1] => e)), [1] => c, [2] => d))

Where for every element in the array, another element or array is inserted after it (pushing the others one index) based on a database query.

How can I accomplish this? help! :)

In the end I want to make a nested list, but I need an array of that shape.

2
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by diafol
0

Could you give an example of your datasource and how you're extracting it? Building the array will depend on whether the data is in partial array form already, or whether it has to be taken via loop, etc.

0

The information in the database is like:

id name parent
1 food NULL
2 meat 1
3 fruit 1
4 ham 2

----- etc

I currently have one function which returns an array of 'child' elements based on their parent name.

This is it:

public function getArray($element) {
		$parent = $this -> _getID($element);
		
		$this -> db -> select('name');
		$this -> db -> from('table_X');
		$this -> db -> where('parent = ' .'\'' .$parent .'\'');
		
		$query = $this -> db -> get();
		
		$result = array();
		
		$count = 0;
		
		foreach($query -> result() as $row) {
			$result[$count] = $row -> name;
			$count++; 
		}
		
		return $result;
	}

* The code looks like this because it is made in the codeigniter framework.
* The $this -> _getID($element) function simply returns the parent elements' ID

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.