Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.