Jump to content

Numerate array by one of it's array's values


lawless

Recommended Posts

Hello there again!

 

I have the following construct of array now (schematics):

array[0]
	array[id] = "1"
	array[title] = "title"
array[1]
	array[id] = "3"
	array[title] = "another title"

Now what i want to do, is to assign the parent-array keys to the IDs of it's contents.

 

This is what it should look like:

array[1]
	array[id] = "1"
	array[title] = "title"
array[3]
	array[id] = "3"
	array[title] = "another title"

How do I do that again? I know I did this some years ago, but I can not remember how.

Just after sending I realized the solution :)

 

I can simply name the array I am using directly at the source, when it first gets written.

foreach ( $array_dbtable as $key => $value )
	{
		$this->array_dbtable[$value['id']] = $value;
	}

My old approach:

$this->array_dbtable = $array_dbtable;

If you know that the array key will always be the id, why couldn't/wouldn't you do:

 

$this->array_dbtable[$value['id']] = $value['title'];

 

making it

 

array(
  1 => 'title',
  3 => 'another_title
)

Thus making it a simple array, instead of multidem.

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.