Jump to content

Help with syntax for looking in an array


annihilate

Recommended Posts

Hi there,

I have just switched over to php5 and so have started making the variables in my classes private and then greating a public function which gets them.

 

Previously, I had this code in a page:

 

echo $movies_base->cert_array[$movie['cert']];

 

cert_array is a var in the movies base class, and it is an array.

 

Under php5, I have made cert_array private, and added a getter for it.

 

So now I have

 

echo $movies_base->get_cert_array()[$movie['cert']];

 

The above line is not valid code. My question is: is it possible to do what I need in one line or do I have to something like the following:

 

$cert_array = $movies_base->get_cert_array();
echo $cert_array[$movie['cert']];

 

Thanks

Colin

in the cert array function you could do

 

function get_cert_array($index = null) {

  if ($index) {

    return $theArray[$index];

  }

  else {

    return $theArray;

  }

}

 

thaut will make the arguement optional

 

and then do something like

 

echo $movies_base->get_cert_array('cert');

in the cert array function you could do

 

function get_cert_array($index = null) {

  if ($index) {

    return $theArray[$index];

  }

  else {

    return $theArray;

  }

}

 

thaut will make the arguement optional

 

and then do something like

 

echo $movies_base->get_cert_array('cert');

 

The [ code] and [ /code] tags are your friends, not enemy's.

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.