Page 1 of 1

implementing class passing array

Posted: Thu Aug 26, 2004 6:50 am
by dalyraptor
feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


im having trouble implementing a class in php4. right now the code is very ugly and there may be more wrong with it than my current problem.

i want to pass and array into the class, but when I do, i dont seem to be able to access the values inside the array, when i echo (as you will see in the code) i get ArrayArray.

the layout of the array is:

Code: Select all

Array (
    Array (v1, v2)
    Array (v1, v2)
    .....
    )
code:

Code: Select all

<?php

$arr = Array ();
$arr[] = Array ("START", 0);
$arr[] = Array ("ONE", 1);
$arr[] = Array ("TWO", 2);
$arr[] = Array ("FINISHED TWO", 3);
$arr[] = Array("FINISHED ONE", 4);

$arr[] = Array ("ONE", 5);
$arr[] = Array ("TWO", 6);
$arr[] = Array ("FINISHED TWO", 7);
$arr[] = Array("FINISHED ONE", ;
$arr[] = Array ("END", 9);

class TimeFunct
{
	var $arr;
	var $arr_totals = Array(Array());
	var $total_time;
	var $i;
	var $strng = "";

	function giveTimeArray($theArray)
	{
		(array) $this->$arr = (array) $theArray;
		return $this->$arr[2][0];
	}

	function timeLabelFound($time_array, $timeLable)
	{
		for ($i=0; $i<count($time_array); $i++)
		{
			if ($time_array[$i][0]==$timeLable) return $i;
		}
		return false;
	}

	function outputTimes()
	{
		for ($i=0; $i<count($this->$arr)-1; $i++)
		{
			$timeLable = $this->$arr[$i][0] . " -to- " . $this->$arr[$i+1][0];
			return $this->$arr[4][1];
			$time = $this->$arr[$i+1][1] - $this->$arr[$i][1];

			return $timeLable;

			if ($j = timeLabelFound($arr_totals, $timeLable))
			{
				$arr_totals[$j][1] += $time;
			} else {
				$arr_totals[] = Array ($timeLable, $time);
			}
		}

		$this->$total_time = $arr[count($arr)-1][1] - $arr[0][1];

		for ($i=1; $i<count($arr_totals); $i++)
		{
			$time = $arr_totals[$i][1];
			$time_precent = $time / $total_time * 100;

			$strng .= 'Time: '.number_format( $time, 6 ).' seconds ( '.number_format( $time_precent, 2 ).'% ) for '.$arr_totals[$i][0]. '<br>';
		}

		return $strng;
	}
}

$tme = new TimeFunct;
echo ($tme->giveTimeArray($arr) );
echo ( $tme->outputTimes() );

?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Aug 26, 2004 6:58 am
by delorian
Try to use print_r instead of echo. echo() cannot be used for displaing arrays.

Posted: Thu Aug 26, 2004 8:06 am
by dalyraptor
the type being returned ($arr[2][0]) should be a number, but it actually the whole array $arr.

Posted: Thu Aug 26, 2004 8:34 am
by dalyraptor
feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


i've taken out all the unnecessary stuff:

Code: Select all

<?php

$arr = Array ();
$arr[] = Array ("START", 0);
$arr[] = Array ("ONE", 1);
$arr[] = Array ("TWO", 2);
$arr[] = Array ("FINISHED TWO", 3);
$arr[] = Array("FINISHED ONE", 4);

$arr[] = Array ("ONE", 5);
$arr[] = Array ("TWO", 6);
$arr[] = Array ("FINISHED TWO", 7);
$arr[] = Array("FINISHED ONE", ;
$arr[] = Array ("END", 9);

class TimeFunct
{
	var $arr;
	var $arr_totals = Array(Array());
	var $total_time;
	var $i;
	var $strng = "";

	function &giveTimeArray($theArray)
	{
		$this->$arr = $theArray;
		return $this->$arr[2][0];
	}
}

$tme = new TimeFunct;
echo ($tme->giveTimeArray($arr) );

?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Aug 26, 2004 9:50 am
by feyd
why not use associative arrays?

Posted: Thu Aug 26, 2004 10:20 am
by dalyraptor
Sorry about not using the code tags.

Well the array comes from a different piece of code written by someone else. Im trying to expand on it without modifying the other code. I didnt know that associatve arrays would work and dont see why they should work over the current way. Is that not a bug?

Posted: Thu Aug 26, 2004 10:33 am
by feyd

Code: Select all

function &giveTimeArray($theArray)
   {
      $this->$arr = $theArray;
      return $this->$arr[2][0];
   }
is actually the likely problem.. $this->arr is the proper access hook.

Posted: Thu Aug 26, 2004 11:16 am
by dalyraptor
doh! that was it. im new to php, this is my second day!

Posted: Thu Aug 26, 2004 11:23 am
by feyd
Welcome to the world of PHP :) We hope you stay a while. Try the bangers, they're delicious.