Hello all,
kinda confused on insertion with arrays, hope you can help.
the meat of it is this summary below (or detailed code if thats floats your boat at bottom of page )...
An array i have will look like this when i want to insert an element somewhere inside of it.
$array[17][0] = $someobject;
$array[17][1] = $someobject;//etc
now, i want to insert an object in the middle but for simplicity sake lets take the most basic example that i will be put a object in the front of the array (i dont use shift/push fucntions since this needs to insert in the middle as well which splice can do).
array_splice($array[$day], 0, 0, $object);//2nd parameter is the insertion point
so this should put the object at index 0, and not delete the 1st item since there is no offset length. but it doesnt append it the the $array[17] array. it seems appends it to the object itself, bascially looks like...
Array
(
[17] =>
Array(
[0] => "meeting"
[1] => Array(Array([0]=>"8:00 am",[1]=>"9:00 am"))
[2] => "meeting"
[3] => Array(Array([0]=>"9:00 am",[1]=>"10:00 am"))
)
)
when it should look like this.
Array
(
[17] =>
Array(
[0] => object("meeting", Array([0]=>"8:00 am",[1]=>"9:00 am") )
[1] => object("meeting", Array([0]=>"9:00 am",[1]=>"10:00 am") )
)
)
hopefully someone can help, as my english sucks at explaining things.
thanx everyone.
Ari
<?php
class DayInfo
{
public $dayInfoType;
public $dayInfoArray;
public function setType($theType)
{
$this->dayInfoType = $theType;
}
public function setArray($theArray)
{
$this->dayInfoArray = $theArray;
}
public function getType()
{
return $this->dayInfoType;
}
public function getArray()
{
return $this->dayInfoArray;
}
}
function insertObjectSort( &$array, $object, $day )
{
if( !isset($array[$day]) )
$array[$day] = array(); //if the 17th isnt set, create a new array for it to hold meetings, does it 1st time for every 'day'
if( $object->getType() == "meeting" )
{
array_splice($array[$day], 0, 0, $object); //$array[$day] is an array istself, so it should insert it in this array but doesnt do so correctly and was wondering why
}
}
function printR($array) //recursively prints out the array preformatted nicely on webpage
{
echo "<pre>";
print_r( $array);
echo "</pre>";
}
?>
<html>
<head>
<title>Daily View</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$monthList = array();
$day=17; //the day will be done dynamically later on a page with dynamic meeting data but simple example for now
$newObject = new DayInfo();
$newObject->setType("meeting");
$times = array("8:00 am","9:00 am");
$newObject->setArray( $times );
insertObjectSort( $monthList, $newObject, $day ) ;
echo('<br/><br/>');
printR($monthList);
$newObject = new DayInfo();
$newObject->setType("meeting");
$times = array("9:00 am","10:00 am");
$newObject->setArray( $times );
insertObjectSort( &$monthList, $newObject, $day ) ;
echo('<br/><br/>');
printR($monthList);
//So the structure of the array for the 17th should look like this now...
//$monthList[17][0] = object{ [0]=>"meeting" [1]=>array("8:00 am","9:00 am") } ; //1st meeting
//$monthList[17][1] = object{ [0]=>"meeting" [1]=>array("9:00 am","10:00 am") } ; //second meeting
?>
</body>
</html>