ambil array indeks terurut

Calon Juragan · Jun 30, 2012
kk,..om,.smuanya mw nanya,..galau array nech:(
gmana cara mengambil nilai array yang indeks array terurut saja >=4 dari array dibawah ini,..
<?php
$array = Array(
    '2' => '50',
    '3' => '60',
    '4' => '12',
    '5' => '23',
    '12' => '43',
    '13' => '43',
    '28' => '54',
    '29' => '34',
    '30' => '54',
    '31' => '23',
    '32' => '43',
    '53' => '23',
    "90" => "43",
    "91" => "43",
    "92" => "43",
    "93" => "54",
    "94" => "89",
    "95" => "12",
    "96" => "11",
);

echo '<pre>';
print_r($array);
echo '</pre>';
?>

hasil yg diinginkan:

Array
(
[0] => Array
(
[2] => 50
[3] => 60
[4] => 12
[5] => 23
)

[1] => Array
(
[28] => 54
[29] => 34
[30] => 54
[31] => 23
[32] => 43
)

[2] => Array
(
[90] => 43
[91] => 43
[92] => 43
[93] => 54
[94] => 89
[95] => 12
[96] => 11
)

)
mhon bntuanya :)
Silahkan login untuk menjawab!
0
Loading...
Ellyx Christian · Jul 3, 2012 · 0 Suka · 0 Tidak Suka
pertanyaannya ga jelas!
0
Loading...
Calon Juragan · Jul 3, 2012 · 0 Suka · 0 Tidak Suka
gni om,..gmna cara mengambil array yang indeksnya terurut >= 4 saja dari array diatas dan sprti hasil yang diinginkan,..misal indeksnya 2,3,4,5,8,9,45,46,47,48,67,68,90,91,92,93,94,95
jadi,..yang diambil indeks ===>> 2,3,4,5 dan 45,46,47,48 dan 90,91,92,93,94,95,..sperti hasil diatas,..gmna tuh om???
0
Loading...
Ellyx Christian · Jul 7, 2012 · 0 Suka · 0 Tidak Suka
<?php
$array = Array(
    '2' => '50',
    '3' => '60',
    '4' => '12',
    '5' => '23',
    '12' => '43',
    '13' => '43',
    '28' => '54',
    '29' => '34',
    '30' => '54',
    '31' => '23',
    '32' => '43',
    '53' => '23',
    "90" => "43",
    "91" => "43",
    "92" => "43",
    "93" => "54",
    "94" => "89",
    "95" => "12",
    "96" => "11",
);
$array2 = array();
$k = key($array);
$i = 0;
$array2[$i][$k] = current($array);
unset($array[$k]);
foreach($array as $key => $value){
	if(($key - $k) == 1){
		$array2[$i][$key] = $value;
	}else{
		if(count($array2[$i]) < 4){
			unset($array2[$i]);
			$i--;
		}
		$i++;
		$array2[$i][$key] = $value;
	}
	$k = $key;
}
var_dump($array2);
0
Loading...
Calon Juragan · Jul 9, 2012 · 0 Suka · 0 Tidak Suka
om,..seblumnya terima kasih,..telah menjawab,..:),..
tapi masih ada indeks array yang tidak memenuhi syarat terurut >=4 terambil,..yaitu jika ada penambahan array pada akhir array diatas yang tidak terurut >= 4,..
misal "103" => "154", "104" => "154"
sperti kode dibawah ini,..
<?php
$arr = Array(
'2' => '50',
'3' => '60',
'4' => '12',
'5' => '23',
'12' => '43',
'13' => '43',
'28' => '54',
'29' => '34',
'30' => '54',
'31' => '23',
'32' => '43',
'53' => '23',
"90" => "43",
"91" => "43",
"92" => "43",
"93" => "54",
"94" => "89",
"95" => "12",
"96" => "11",
"103" => "154",
"104" => "154"
);
function terurut($array){	    
	$array2 = array();
    $k = key($array);
    $i = 0;
    $array2[$i][$k] = current($array);
    unset($array[$k]);
    foreach($array as $key => $value){
    if(($key - $k) == 1){
    $array2[$i][$key] = $value;
    }else{
    if(count($array2[$i]) < 4){
    unset($array2[$i]);
    $i--;
    }
    $i++;
    $array2[$i][$key] = $value;
    }
    $k = $key;
    }
	return $array2;
}
    $hasil_terurut = terurut($arr);
	echo "<pre>";
	print_r($hasil_terurut);
	echo "</pre>";
?>	

GMNA TUH OM,..TOLONG DUNKKKKK,..
0
Loading...
Ellyx Christian · Jul 10, 2012 · 0 Suka · 0 Tidak Suka
<?php
$array = Array(
	'2' => '50',
	'3' => '60',
	'4' => '12',
	'5' => '23',
	'12' => '43',
	'13' => '43',
	'28' => '54',
	'29' => '34',
	'30' => '54',
	'31' => '23',
	'32' => '43',
	'53' => '23',
	"90" => "43",
	"91" => "43",
	"92" => "43",
	"93" => "54",
	"94" => "89",
	"95" => "12",
	"96" => "11",
	"103" => "154",
	"104" => "154",
);
$array2 = array();
$k = key($array);
$i = 0;
$array2[$i][$k] = current($array);
unset($array[$k]);
foreach($array as $key => $value){
	if(($key - $k) == 1){
		$array2[$i][$key] = $value;
	}else{
		if(count($array2[$i]) < 4){
			unset($array2[$i]);
			$i--;
		}
		$i++;
		$array2[$i][$key] = $value;
	}
	$k = $key;
}
if(count(end($array2)) < 4){
	array_pop($array2);
}
var_dump($array2);