/    Sign up×
Community /Pin to ProfileBookmark

Manipulating/Changing keys in an Multidimensional array.

Hello,

I need a way to change keys in an array. For example if the key of a certain element is “0” I need it to be “look”. Now, I know there is array_flip. But I do not think it will work for a multidimensional array, as I only want to change the keys on the 2nd dimension.

I was wondering if foreach would work for this, as it can use $keys. However when I tried to do it, it didn’t work. At least it did not save they $keys onto array.

There is a certain site that talks about it. However if asks you to pay.

[url]http://ibmdw.safaribooksonline.com/0672327090/ch04lev1sec6[/url]

Any help would be appreciated.?

Thank You

CMCDragonkai

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@bokehMar 15.2008 — I'm not really sure what you are asking.

[code=php]$array['look'] = $array[0];
unset($array[0]);[/code]
Copy linkTweet thisAlerts:
@DragonkaiauthorMar 15.2008 — If theres an array:

$anarray = array(0=> "hol");

Then I want to change the "0" to something else.

Would what you just said work?
Copy linkTweet thisAlerts:
@bokehMar 15.2008 — What do you want to change: the key or the value?
Copy linkTweet thisAlerts:
@DragonkaiauthorMar 19.2008 — The Key, sorry for the late reply. It didn't seem to send a email notification
Copy linkTweet thisAlerts:
@bokehMar 19.2008 — The Key, sorry for the late reply. It didn't seem to send a email notification[/QUOTE]Look at post 2.
Copy linkTweet thisAlerts:
@DragonkaiauthorMar 20.2008 — Ok, I'll try that.
Copy linkTweet thisAlerts:
@DragonkaiauthorMar 22.2008 — It works thanks, but I had to first get my head around it.

If anyone else needs information for this.

Basically:

Example:

$endtypearray[$tensecolumnarray[$key]] = $endtypearray[$key];

unset ($endtypearray[$key]);

If I had an array with the values : look,see,hah. This is $tensecolumnarray.

And I had another array that I wanted to replace the keys with the values in $tensecolumn array which is the $endtypearray;

I foreached the $endtypearray with $key => $value and then used the "Example" to change the keys in the $endtypearray to the values in the $tensecolumnarray.

So what's happened is it has added a new element to $endtypearray which uses a different name specified by $tensecolumnarray which is equal to the original key's element. Then it unsets the one that I wanted to replace.

Very smart.
×

Success!

Help @Dragonkai spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...