4

I have an array:

$array['text6'] = array(
    'elem2' => 'text2',
    'elem3' => 'text3',
    'elem4' => 'text4',
    'elem5' => 'text5'
    'elem6' => 'text6'
);

And I would like to change, for example text6 key with other key in that way:

$name_key = 'elem4';

// something action here
// and final array:

    $array['text4'] = array(
        'elem2' => 'text2',
        'elem3' => 'text3',
        'elem5' => 'text5'
        'elem6' => 'text6'
    );

How I can do it? I have 105 arrays and I need to change every array in the same way, so when array looks that:

$array['text6'] = array(
    'elem2' => 'text2',
    'elem3' => 'text3',
    'elem4' => 'text4',
    'elem5' => 'text5'
    'elem6' => 'text6'
);

$array['othertext6'] = array(
    'elem2' => 'othertext2',
    'elem3' => 'othertext3',
    'elem4' => 'othertext4',
    'elem5' => 'othertext5'
    'elem6' => 'othertext6'
);

And I would like to change main key with key number three (key -> 'elem4'), it should make in every array (different beetwen arrays is only in value, keys are always the same):

$name_key = 'elem4';

// action...

$array['text4'] = array(
    'elem2' => 'text2',
    'elem3' => 'text3',
    'elem5' => 'text5'
    'elem6' => 'text6'
);

$array['othertext4'] = array(
    'elem2' => 'othertext2',
    'elem3' => 'othertext3',
    'elem5' => 'othertext5'
    'elem6' => 'othertext6'
);

How I can do it?

3
  • This is wrong way. You need nested list (children of nodes). Commented Dec 24, 2015 at 20:03
  • Look now, I edited my question. The main key is always in an array ('elem6' => 'text6'). I would like to remove main key and change for another key from an array and then - remove this key from an array. Commented Dec 24, 2015 at 20:09
  • Please mark which answer solved your problem, @NewbieUser Commented Nov 1, 2016 at 21:52

1 Answer 1

2

If I'm understanding correctly, you'd like to set the key of an element in an array to a value of an element in a second array which is itself the value of the first key in the top-level array. Also, you want to remove that element from the second-level array.

Will the key of the top-level array always be changed to the value of the second-level with the same key?

That is, will 'elem4' always be the key for the value that should become the top-level array's key?

If so, you can do this:

First get all your arrays into one big array so you can loop through them.

$list = array($array['text4'], $array['text5'], ... (all your other arrays here));

Then,

$name_key = 'elem4';
foreach ($list as $k => $v) {
   $new_key =  $v[$name_key];
   unset ($v[$name_key]);
   $list[$new_key] = $v;
   unset ($list[$k]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Instead of hard-coding 'elem4', use $name_key
Are you sure, it's a good solution? The names of an values in main array is very important for me. I think, in your solution you removed all keys, yes?
@NewbieUser, In my solution, I am removing the key 'elem4' from each second-level array and setting the key for that array in the top-level array to the value of 'elem4' (or $name_key). If you want to keep that element ('elem4') in the second-level array, you can remove the line: unset ($v[$name_key]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.