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

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.