Ask a Question related to PHP Notes, Design and Development.
-
cwahl@prisma.com #1
note 33609 added to function.array-splice
I couldn't get array_splice() to insert into a multidimensional array, so I cribbed this from the notes for array_push() (a thousand thanks to jhall at jadeinternet dot net). I'm dealing with a spreadsheet-looking dataset, and it lets me add "rows" where the row data is the array that I have assigned to $val, and the row number is $ky:
function insert_into_array($array,$ky,$val)
{
$n = $ky;
foreach($array as $key => $value)
{
$backup_array[$key] = $array[$key];
}
$upper_limit = count($array);
while($n <= $upper_limit)
{
if($n == $ky)
{
$array[$n] = $val;
echo $n;
}
else
{
$i = $n - "1";
$array[$n] = $backup_array[$i];
}
$n++;
}
return $array;
}
So that:
$list = array( "0" => "zero",
"1" => "one",
"2" => "two",
"3" => "three",
"4" => "four",
"5" => "five",
"6" => "six");
$value = "New Number Three";
$key = "3";
$new = insert_into_array($list,$key, $value);
Will Return:
$list =
Array
(
[0] => zero
[1] => one
[2] => two
[3] => three
[4] => four
[5] => five
[6] => six
)
$new=
Array
(
[0] => zero
[1] => one
[2] => two
[3] => New Number Three
[4] => three
[5] => four
[6] => five
[7] => six
)
----
Manual Page -- [url]http://www.php.net/manual/en/function.array-splice.php[/url]
Edit Note -- [url]http://master.php.net/manage/user-notes.php?action=edit+33609[/url]
Delete Note -- [url]http://master.php.net/manage/user-notes.php?action=delete+33609&report=yes[/url]
Reject Note -- [url]http://master.php.net/manage/user-notes.php?action=reject+33609&report=yes[/url]
cwahl@prisma.com Guest
-
note 33944 added to function.array-combine
// Here's another pre PHP 5 alternative. function array_combine($keys, $vals) { $i = 0; foreach ($keys as $key) { $newarray = $vals; } return... -
note 33879 added to function.array-combine
array_combine for PHP 4.0.6+ function array_combine($keys, $vals) { $retour= FALSE; $limite= count($keys); if (0!=$limite &&... -
note 33865 added to function.array
I found a slightly better way to create large 2d arrays: // this will simply output a 2d array-- it isn't very robust but it //suits my needs... -
note 33861 added to function.array-reverse
u all suck dick u fancy bastards ---- Manual Page -- http://www.php.net/manual/en/function.array-reverse.php Edit Note --... -
note 33742 added to function.in-array
Be careful... This script was designed to check for required fields: $required = array('put','required','field','names','here'); foreach...



Reply With Quote

