exchange value and key per element in associative array
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
exchange value and key per element in associative array
Casual php user.
I can't find out how to accomplish this.
I have an associative array, some elements have a value, some don't but all have a "string key". When an element is found to have a value, the key and the value must be exchanged leading to a subsequent sort for presentation.
For example in a foreach loop
foreach ($array as $key => $value)
$key must be assigned the content of $value and $value must be assigned the content of $key if both $key and $value have a content.
Casual php user.
I can't find out how to accomplish this.
I have an associative array, some elements have a value, some don't but all have a "string key". When an element is found to have a value, the key and the value must be exchanged leading to a subsequent sort for presentation.
For example in a foreach loop
foreach ($array as $key => $value)
$key must be assigned the content of $value and $value must be assigned the content of $key if both $key and $value have a content.
Your help is much appreciated.
well you could do inside the loop if $value > '' .. or if $value != null
$array[$value] = $key; // This creates a new element ... and then you should extract the the $key key from the array and rebuild it ... Probably php has a function for this .. but it's also trivial to implement
Thank you for your answer.
I have tried a number of variations of your solution without success. The PHP documentation gives the following example:
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
?>
but the comment is wrong, the element in the array is unchanged after this foreach loop and what I need to know is how to change it. It may be trivial but I have no idea how to do this.
There is a function to do this on a whole array but no function to do it on individual elements of an array.
For anyone interested, I found the only way is to create a new array as described below and then use this new array for sorting and display.
The original array was $fils
foreach ($fils as $path => $name) {
if ($name) {
$fils2[$name]=$path;
}
else {
$fils2[$path]="";
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.