Jump to content

[SOLVED] inserting into array


taith

Recommended Posts

anybody know an efficient way of appending to an array?

 

$array[]='test';
$array[]='test';
$array[]='test';
$array[]='test6';
$array[]='test';

 

say... i want to move test6 to the #2 position... and increase the other keys under it... by 1...?

 

i know i could make a function to do this... just didnt know if php had a built in function like that :-)

Link to comment
https://forums.phpfreaks.com/topic/53856-solved-inserting-into-array/
Share on other sites

All credits to frost110 for this, I remember reading this post the other day:

 

<?php
function insertArray($array, $addDataArr, $pos) {
    $newKey = -1;
    foreach ($array as $key => $val) {
          if ($key == $pos) { 
                foreach ($addDataArr as $val) {
                      $newArray[$key++] = $val;
                }
                $newKey = $key;     
          }else {
              if (isset($newKey) && $newKey != -1) {
                    $newArray[$newKey++] = $val;
              }else {
                    $newArray[$key] = $val;
              }
          }
    }
}
?>

 

Probably not the best way but I think it works, I also think there is something like www.php.net/array_merge

 

But yea, I think the above will work, untested.

All credits to frost110 for this, I remember reading this post the other day:

 

<?php
function insertArray($array, $addDataArr, $pos) {
    $newKey = -1;
    foreach ($array as $key => $val) {
          if ($key == $pos) { 
                foreach ($addDataArr as $val) {
                      $newArray[$key++] = $val;
                }
                $newKey = $key;     
          }else {
              if (isset($newKey) && $newKey != -1) {
                    $newArray[$newKey++] = $val;
              }else {
                    $newArray[$key] = $val;
              }
          }
    }
}
?>

 

Probably not the best way but I think it works, I also think there is something like www.php.net/array_merge

 

But yea, I think the above will work, untested.

 

Go me.

aah... so... no prebuilt way...

 

i made this one hwile i was waiting... lol :D

 

$array[]='1';
$array[]='2';
$array[]='3';
$array[]='4';
$array[]='5';
$array[]='6';
$array[]='7';

function array_resort($array, $value, $key){
$out=array();
for($i=0; $i<count($array); $i++){
  if($i<$key) $out[$i]=$array[$i];
  elseif($i==$key&&!$done){
   $done=true;
   $out[$key]=$value;
   $i--;
  }else $out[$i+1]=$array[$i];
}
return $out;
}
$array=array_resort($array,'test','2');
print_r($array);

well just for fun I updated my function to takin a single string, or a whole array. Fun stuff.

 

<?php
function array_insert($array, $add_data, $pos) {
    $new_key = -1;
    foreach ($array as $key => $val) {
          if ($key == $pos) { 
                if (is_array($add_data)) {
                foreach ($add_data as $val2) {
        	              $new_array[$key++] = $val2;
                	}
	}else {
		$new_array[$key++] = $add_data;
	}

                $new_key = $key;     
          }else {
	$index = ($new_key!=-1)?$new_key++:$key;
	$new_array[$index] = $val;
  }
    }
}
?>

 

Remember this is still un-tested.

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.