Jump to content

array output - order in the index structure


felito

Recommended Posts

I have this code

$nArr = array('A', 'B', 'C', 'D', 'E', 'F');

$counter = 3;
while ($counter > 0) {
    $chunkedValues[$counter][0] = 1;
    for ($j = 0 ; $j < $counter ; $j++) {
        $chunkedValues[$counter][$j + 1] = $nArr[$j];
    }
    $nArr = array_slice($nArr, $counter--);
}
var_dump($chunkedValues);

that outputs:

array
  3 => 
    array
      0 => int 1
      1 => string 'A' (length=1)
      2 => string 'B' (length=1)
      3 => string 'C' (length=1)
  2 => 
    array
      0 => int 1
      1 => string 'D' (length=1)
      2 => string 'E' (length=1)
  1 => 
    array
      0 => int 1
      1 => string 'F' (length=1)

 

But i need this index structure:

array
  0 => 
    array
      0 => int 1
      1 => string 'A' (length=1)
      2 => string 'B' (length=1)
      3 => string 'C' (length=1)
  1 => 
    array
      1 => int 1
      2 => string 'D' (length=1)
      3 => string 'E' (length=1)
  2 => 
    array
      2 => int 1
      3 => string 'F' (length=1)

 

I want to avoid loops with ceil.

 

Any idea? thanks for your time.

 

Well, as i said, i need this structure:

 

array
  0 => 
    array
      0 => int 1
      1 => string 'A' (length=1)
      2 => string 'B' (length=1)
      3 => string 'C' (length=1)
  1 => 
    array
      1 => int 1
      2 => string 'D' (length=1)
      3 => string 'E' (length=1)
  2 => 
    array
      2 => int 1
      3 => string 'F' (length=1)

 

How ksort can be useful?

my bad, should read more carefully. here this looks like what you want,  I lifted this directly from the php manual :

I was trying to figure out how to normalize an array with numerical keys.  Since I was doing for() for a lot of things, but only replacing it if the conditions were right, I wound up with off ball arrays I couldn't access.  That being said, I looked for a method of normalizing the array and couldn't find one, so I built my own.  I'm not sure how to go about making it recursive, but I didn't need that feature for my own, so I just went without recursion.

 

//This will take array([5] => "test1", [4] => "test2", [9] => "test3") into array([0] => "test1", [1] => "test2", [2] => "test3") so you can access it easier.

        function normalize_array($array){

          $newarray = array();

          $array_keys = array_keys($array);

          $i=0;

          foreach($array_keys as $key){

          $newarray[$i] = $array[$key];

         

          $i++;

          }

          return $newarray;

        }

I did it the following way. It does comes out fine the other end. I'm almost certain there will be an easier way to do it though as this seems a little long winded. Don't have time to do any research though.

 

$nArr = array('A', 'B', 'C', 'D', 'E', 'F');

// Set some vars
$length = 1;
$offset = 0;
$total = 0;

// Reverse the array
$nArr = array_reverse($nArr);

// Create the array
$chunkedValues = array();
while($total < count($nArr)){
    $chunkedValues[] = array_slice($nArr, $total, $length);
    $total+= $length;
    $length++;
}

// Revers stuff.
foreach($chunkedValues as $cK => $cV){
    $chunkedValues[$cK] = array_reverse($chunkedValues[$cK]);
}
$chunkedValues = array_reverse($chunkedValues);

var_dump($chunkedValues);

This?

 

<?php

$nArr = array('A', 'B', 'C', 'D', 'E', 'F');

$counter = 3;
$chunkedValues = array();
for( $i = 0; $i < 3; $i++ ) {
    $chunkedValues[$i] = array(1);
    for ($j = $counter-$i, $k = 0; $j > 0; $j--, $k++) {
        $chunkedValues[$i][$k] = $nArr[$k];
    }
	$nArr = array_slice($nArr, $counter-$i);
    
}
var_dump($chunkedValues);

?>

thanks to all.

 

The main problem is that the order of the index is not the natural.

array
  0 => 
    array
      0 => int 1
      1 => string 'A' (length=1)
      2 => string 'B' (length=1)
      3 => string 'C' (length=1)
  1 => 
    array
      1 => int 1
      2 => string 'D' (length=1)
      3 => string 'E' (length=1)
  2 => 
    array
      2 => int 1
      3 => string 'F' (length=1)

 

is different of:

array
  0 => 
    array
      0 => string 'A' (length=1)
      1 => string 'B' (length=1)
      2 => string 'C' (length=1)
  1 => 
    array
      0 => string 'D' (length=1)
      1 => string 'E' (length=1)
  2 => 
    array
      0 => string 'F' (length=1)

 

this solves  the issue, but i have problems with the ceil in some cases.

 

$nArr = array('A', 'B', 'C', 'D', 'E', 'F');
$lp = 3;                
for ($i = 0; $i < $lp; $i++) {
     $m[$i][$i] = 1;            
    for ($x = $i; $x < $lp; $x++) {                
        $v = $i+ceil($i/2);
        $m[$i][$x+1] = $nArr[$x+$v];            
    }        
}

 

That's a fairly easy change

 

<?php

$nArr = array('A', 'B', 'C', 'D', 'E', 'F');

$counter = 3;
$chunkedValues = array();
for( $i = 0; $i < 3; $i++ ) {
    $chunkedValues[$i][$i] = 1;
    for ($j = $counter-$i, $k = 0; $j > 0; $j--, $k++) {
        $chunkedValues[$i][$k+$i+1] = $nArr[$k];
    }
	$nArr = array_slice($nArr, $counter-$i);
    
}
var_dump($chunkedValues);

?>

 

Sorry I didn't notice that before

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.