Jump to content
php.lv forumi

jautajums par arrayiem


Recommended Posts

Jautājums par aarajiem

 

Tatda ir:

 

$a1=array(‘a’=>”AAA1”, ‘b’=>”BBB1”, ‘c’=>”CCC1”);
$a2=array(‘a’=>”AAA2”, ‘b’=>”BBB2”, ‘c’=>”CCC2”);
$a3=array(‘a’=>”AAA3”, ‘b’=>”BBB3”, ‘c’=>”CCC3”);

 

to visu man vajag salikt taa, lai butu massivs:

 

$masivs:

[1] => Array
{
	[a] => AAA1
	[b] => BBB1
	[c] => CCC1
}
[2] => Array
{
	[a] => AAA2
	[b] => BBB2
	[c] => CCC2
}
[3] => Array
{
	[a] => AAA3
	[b] => BBB3
	[c] => CCC3
}

 

ar sadu risinajumu:

$masivs[1]=array_values($a1);
$masivs[2]=array_values($a1);
$masivs[3]=array_values($a1);

 

iegustam so:

[1] => Array
{
	[1] => AAA1
	[2] => BBB1
	[3] => CCC1
}
[2] => Array
{
	[1] => AAA2
	[2] => BBB2
	[3] => CCC2
}
[3] => Array
{
	[1] => AAA3
	[2] => BBB3
	[3] => CCC3
}

 

bet vajag iegut pirmo variantu...kaa to dabut? skaidrs, ka array_values visus indexus saliek pa jaunam un tie ir integeri, bet man vajag saglabat stringus esosos! Paldies par jebkadam idejam :)

Link to post
Share on other sites

$masivs = array($a1, $a2, $a3);

 

ok, sitais der....

 

bet kaa man darit, lai apvienotu arrays

 

teksim man $masivs = array($a1);

 

kuram pec tam vajag pielikt klat $masivs = array($a2) bet ne lai neuzrakstas pa virsu!

 

 

proti, kaa apvoenoy $masivs = array($a1, $a2) ar array($a3) .... ar array(aN))

Edited by Alnis
Link to post
Share on other sites

kaa ar sort() funkciju var sasortet sitadu masiu

[1] => Array
       {
               [a] => AAA1
               [b] => BBB1
               [c] => 2
       }
[2] => Array
       {
               [a] => AAA2
               [b] => BBB2
               [c] => 3
       }
[3] => Array
       {
               [a] => AAA3
               [b] => BBB3
               [c] => 1
       }

 

lai vins izskatas:

 

[1] => Array
       {
               [a] => AAA3
               [b] => BBB3
               [c] => 1

       }
[2] => Array
       {
               [a] => AAA1
               [b] => BBB1
               [c] => 2
       }
[3] => Array
       {
               [a] => AAA2
               [b] => BBB2
               [c] => 3
       }

 

Proti es velos, lai sortesana notiek pec otra indexa arrajaa $masivs[][c] - pec "c" :)

Link to post
Share on other sites

lai kko tādu varētu izdarīt ar sort() funkciju, laikam nāktos mainīt php source un pēc tam to pārkompilēt. vienkāršāk būtu, ja tu uztaisītu savu sortēšanas funkciju speciāli šim gadījumam (array salīdzināšana pēc konkrēta key), izmantojot kādu no tiem sortēšanas algoritmiem, kurus esi mācījies skolā/augstskolā. starp citu vai tu gadījumā necenties uztaisīt pats savu db ar šiem arrayiem? :D

Link to post
Share on other sites

haha tomēr nevajadzēs pašam taisīt sortēšanu. php tiešām par visu ir padomāts :P

array_multisort() ftw

 

$a[] = array('a' => 'AAA1', 'b' => 'BBB1', 'c' => 2);
$a[] = array('a' => 'AAA2', 'b' => 'BBB2', 'c' => 3);
$a[] = array('a' => 'AAA3', 'b' => 'BBB3', 'c' => 1);

for ($i = 0; $i < count($a); $i++) $aSort[] = $a[$i]['c'];

array_multisort($aSort, SORT_ASC, $a);

echo '<textarea rows="25" cols="80">';
print_r($a);
echo '</textarea>';

/*
Array
(
   [0] => Array
       (
           [a] => AAA3
           [b] => BBB3
           [c] => 1
       )

   [1] => Array
       (
           [a] => AAA1
           [b] => BBB1
           [c] => 2
       )

   [2] => Array
       (
           [a] => AAA2
           [b] => BBB2
           [c] => 3
       )
)
*/

c'mon tauta lasām taču manuāli!!!

Link to post
Share on other sites

mana izglitiba nav saistita ar datoriem, es sava valasprieka del meginu/ macos kaut ko uzkodet :) skaidrs, ka netaisos php source mainit :))

 

Griibu panakt vienu lietinu bez db palidzibas :), lidz sim ar masiviem bija maz saskarsme, nekad nebiju lietojis masivus, kuros ir apakmasivi un nekad neesmu nemies ar datu dazadu sortesanu, kartosanu saskaitisanu pasa masivaa ieksaa :) Pamazam apgustu massivus :)

 

Paaldies :) 2easy, ju ar da best :)

Edited by Alnis
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...