LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2006, 08:06 AM   #1
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Rep: Reputation: 52
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.

Your help is much appreciated.

Last edited by rblampain; 03-31-2006 at 08:10 AM.
 
Old 03-31-2006, 08:15 AM   #2
cupubboy
Member
 
Registered: May 2003
Location: Bucharest,Romania
Distribution: Fedora Core 7
Posts: 109

Rep: Reputation: 15
Quote:
Originally Posted by rblampain
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
 
Old 03-31-2006, 11:00 PM   #3
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Original Poster
Rep: Reputation: 52
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.
 
Old 04-02-2006, 09:07 AM   #4
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Original Poster
Rep: Reputation: 52
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]="";
}
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
index of an element in the array ? thelonius Programming 1 09-24-2005 12:41 PM
number of element in a array of pointer? os2 Programming 7 03-09-2005 11:50 AM
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 08:07 AM
return index of element in java multidimensional array dave bean Programming 8 11-28-2003 11:00 AM
C++ Six Element Array petercool Programming 2 08-20-2003 11:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration