Jump to content

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


lawless
Go to solution Solved by 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.

Link to comment
Share on other sites

  • Solution

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;
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.