9 Replies - 2283 Views - Last Post: 16 May 2009 - 09:24 PM Rate Topic: -----

#1 imtiazmasrur  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 09-November 08

Explode sorted array element

Post icon  Posted 16 May 2009 - 10:32 AM

This is my array
----------------------------------------------
Array ( [18] => 4,3,1 [15] => 2,8,7,6 [17] => 9 [19] => 67,13 [109] => 38,49 )

I need output like this
-------------------------------------------------
[18] = 1
[15] = 2
[18] = 3
[18] = 4
[15] = 6
[15] = 8
[15] = 7
[17] = 9
[19] = 13
[10] = 38
[10] = 49
[19] = 67


Hlp Needed !!! plz anybody hlp me i'm in a trouble.

This post has been edited by imtiazmasrur: 16 May 2009 - 10:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Explode sorted array element

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Explode sorted array element

Posted 16 May 2009 - 11:01 AM

You should look into the array functions in PHP especially the sort()-function.

php.net/array
php.net/sort

Hope it helps! :)

This post has been edited by Wimpy: 16 May 2009 - 11:19 AM

Was This Post Helpful? 0
  • +
  • -

#3 imtiazmasrur  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 09-November 08

Re: Explode sorted array element

Posted 16 May 2009 - 11:41 AM

View PostWimpy, on 16 May, 2009 - 10:01 AM, said:

You should look into the array functions in PHP especially the sort()-function.

php.net/array
php.net/sort

Hope it helps! :)



I've already visited those link bt i can't get any solution. Plz hlpppppppppppp.

Sorry for my bad english. :blink:
Was This Post Helpful? 0
  • +
  • -

#4 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Explode sorted array element

Posted 16 May 2009 - 11:46 AM

What do you got so far? :) Do you need to retain the array keys as they are or that doesn't matter? and also is the posts in the array also arrays or are they strings?

if strings:
$arr	= Array(18=>"4,3,1", 15=>"2,8,7,6", 17=>"9", 19=>"67,13", 109=>"38,49" );
$newArr = Array();
foreach($arr as $a)
{
	$newArr  = array_merge($newArr, split(',', $a));
}

sort($newArr );

foreach($newArr  as $na)
{
	echo $na."<br />";
}


if arrays:
$arr	= Array(18=>Array(4,3,1), 15=>Array(2,8,7,6), 17=>Array(9), 19=>Array(67,13), 109=>Array(38,49));
function Add($array)
{
	static $newArr = Array();
	foreach($array as $a)
	{
		if(is_array($a))
		{
			Add($a);	
		}
		else
		{
			$newArr[] = $a;	
		}
	}
	return $newArr;
}
$newArr = Add($arr);

sort($newArr);

foreach($newArr as $na)
{
	echo $na."<br />";
}

That's just two somewhat ugly solutions that does the trick...

Hope it helps! :)

View Postimtiazmasrur, on 16 May, 2009 - 08:41 PM, said:

View PostWimpy, on 16 May, 2009 - 10:01 AM, said:

You should look into the array functions in PHP especially the sort()-function.

php.net/array
php.net/sort

Hope it helps! :)



I've already visited those link bt i can't get any solution. Plz hlpppppppppppp.

Sorry for my bad english. :blink:

This post has been edited by Wimpy: 16 May 2009 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#5 young.steveo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 14-May 09

Re: Explode sorted array element

Posted 16 May 2009 - 11:51 AM

View Postimtiazmasrur, on 16 May, 2009 - 09:32 AM, said:

Array ( [18] => 4,3,1 [15] => 2,8,7,6 [17] => 9 [19] => 67,13 [109] => 38,49 )

Are those values strings?
Was This Post Helpful? 0
  • +
  • -

#6 imtiazmasrur  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 09-November 08

Re: Explode sorted array element

Posted 16 May 2009 - 11:57 AM

View PostWimpy, on 16 May, 2009 - 10:46 AM, said:

if arrays:
$arr	= Array(18=>Array(4,3,1), 15=>Array(2,8,7,6), 17=>Array(9), 19=>Array(67,13), 109=>Array(38,49));
function Add($array)
{
	static $newArr = Array();
	foreach($array as $a)
	{
		if(is_array($a))
		{
			Add($a);	
		}
		else
		{
			$newArr[] = $a;	
		}
	}
	return $newArr;
}
$newArr = Add($arr);

sort($newArr);

foreach($newArr as $na)
{
	echo $na."<br />";
}



yah thats i make before bt i need, array key value before each value

like this
-----------------
18=1
15=2
18=3
Was This Post Helpful? 0
  • +
  • -

#7 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Explode sorted array element

Posted 16 May 2009 - 12:03 PM

Then you have to do something similar but with the keys! :) Fool around with it a little and I can almost promise that you will find a solution to it!

Check this function out:
array_multisort

This post has been edited by Wimpy: 16 May 2009 - 12:07 PM

Was This Post Helpful? 0
  • +
  • -

#8 imtiazmasrur  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 09-November 08

Re: Explode sorted array element

Posted 16 May 2009 - 12:12 PM

View PostWimpy, on 16 May, 2009 - 11:03 AM, said:

Then you have to do something similar but with the keys! :) Fool around with it a little and I can almost promise that you will find a solution to it!

Check this function out:
array_multisort



HOW, I can't plz hlp :ph34r:
Was This Post Helpful? 0
  • +
  • -

#9 young.steveo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 14-May 09

Re: Explode sorted array element

Posted 16 May 2009 - 12:38 PM

Sounds like homework, but here's my solution:

$arr = Array(18=>Array(4,3,1), 15=>Array(2,8,7,6), 17=>Array(9), 19=>Array(67,13), 109=>Array(38,49));

foreach ($arr as $key => $value) {
	if(is_array($value)){
		foreach($value as $k => $v) {
			$new_array[$v] = $key;
		}   
	} else {
		$new_array[$value] = $key;	
	  }
}
ksort($new_array);
foreach ($new_array as $key => $value) {
	echo '[' . $value . '] => ' . $key . '<br />';
}


Was This Post Helpful? 1
  • +
  • -

#10 imtiazmasrur  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 09-November 08

Re: Explode sorted array element

Posted 16 May 2009 - 09:24 PM

View Postyoung.steveo, on 16 May, 2009 - 11:38 AM, said:

Sounds like homework, but here's my solution:

$arr = Array(18=>Array(4,3,1), 15=>Array(2,8,7,6), 17=>Array(9), 19=>Array(67,13), 109=>Array(38,49));

foreach ($arr as $key => $value) {
	if(is_array($value)){
		foreach($value as $k => $v) {
			$new_array[$v] = $key;
		}   
	} else {
		$new_array[$value] = $key;	
	  }
}
ksort($new_array);
foreach ($new_array as $key => $value) {
	echo '[' . $value . '] => ' . $key . '<br />';
}



thx young.steveo, problem solved :^: .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1