LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-22-2006, 12:14 AM   #1
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Rep: Reputation: 52
Can't use my PHP array.


I have a test array that displays as it was intended when using 'print_r()' but any 'foreach' or nested 'foreach' I use prints repeatedly from element 0 as follows:
0,0,1,0,1,2,0,1,2,3 etc.

Here is the result of 'print_r()':
Array ( [Aeroplane.] => Array ( [0] => .Ac8a2c300 ) [Air.] => Array ( [0] => .A113abc00 ) [Bus.] => Array ( [0] => .A63a3c300 ) [Car.] => Array ( [0] => .A52a3c300 [1] => .Aab757300 ) [Cargo ship.] => Array ( [0] => .Af8a3c300 ) [Cruise ship.] => Array ( [0] => .A0ba4c300 ) [Ferry.] => Array ( [0] => .A26a4c300 ) [Motor bike.] => Array ( [0] => .Abea3c300 ) [Push bike.] => Array ( [0] => .Adda3c300 ) [Rail.] => Array ( [0] => .A91a2c300 ) )

Each element has a name and a number of values, multiple values have to be sorted in reverse order, for example:
[Car.] => Array ( [0] => .A52a3c300 [1] => .Aab757300 )
[0] and [1] have to be sorted in this example.

Can anyone suggest how to access elements of this array and how to sort multiple values within each key?

The purpose is to present checkboxes in a form to the user with values checked when there is only 1 value for a key but ask wich one to select when there are multiple values. Any hint on this is also most welcome.

Thank you for your help.
 
Old 06-22-2006, 02:49 AM   #2
cdhgee
Member
 
Registered: Oct 2003
Location: St Paul, MN
Distribution: Fedora 8, Fedora 9
Posts: 513

Rep: Reputation: 30
Something like:

Code:
<?php

foreach($myarray as $k=>$v)
{
	if(is_array($v)
	{
		//child is an array, so sort and print multiple values
		
		//reverse sort the child values based on the key
		$newChildArray = krsort($v);
		
		foreach($newChildArray as $val)
		{
			echo "<input type=\"radio\" name=\"$k\" value=\"$vals\"/>$val<br />";
		}
	}
	else
	{
		//child is not array, just print it
		echo "<input type=\"radio\" name=\"$k\" value=\"$v\"/>$v<br />";
	}
}
?>
 
Old 06-22-2006, 08:52 AM   #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. It works well. I had an error in the script that produced the odd printing I mentioned in the first few lines.
 
Old 06-22-2006, 10:08 AM   #4
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Rep: Reputation: 15
if i read your post properly, you want to sort a multidimensional array, right?
In that case, you could use array_multisort()

cdhgee's method would work fine as well, the link I posted above is just another way to do it
 
Old 06-23-2006, 07:19 AM   #5
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Original Poster
Rep: Reputation: 52
Thank you koobi, I'll remember that.
 
  


Reply

Tags
solution



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
php: array with functions ldp Programming 7 09-22-2004 04:55 PM
PHP Array Trouble Gerardoj Programming 2 05-30-2004 09:53 PM
php search array spoody_goon Programming 2 04-30-2004 07:20 AM
PHP array help? HappyDude Programming 1 10-13-2003 06:48 PM
php array gui10 Programming 2 03-26-2002 02:07 AM

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

All times are GMT -5. The time now is 12:46 PM.

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