Welcome Guest, Not a member yet? Register   Sign In
PHP Array Question
#1

[eluser]CodeIgniterNewbie[/eluser]
I have a method that returns an array. Is there a way to access a specific element of that returned array? For example:

Code:
// returns the array, but I only want, say, the value of the second element
$MyObject->GetMyArray();

// I'd like to do something like this, but it doesn't work:
$MyObject->GetMyArray[1];
#2

[eluser]usmc[/eluser]
[quote author="CodeIgniterNewbie" date="1213686773"]I have a method that returns an array. Is there a way to access a specific element of that returned array? For example:

Code:
// returns the array, but I only want, say, the value of the second element
$MyObject->GetMyArray();

// I'd like to do something like this, but it doesn't work:
$MyObject->GetMyArray[1];
[/quote]

you can do any one of these (and so much more)
Code:
$array = $MyObject->GetMyArray();

$you_are_killing_me_smalls = $array[1];

//or

//(inside class/method)

function GetMyArray()
{
  //form your array however you do it
  $getmearraysomeway = array();
  $this->arrayContainer = $getmearraysomeway;
}

$MyObject->GetMyArray();
$array = $MyObject->arrayContainer[1];
#3

[eluser]wiredesignz[/eluser]
Or simply ask the method to return the array element.
Code:
function GetMyArray($index = FALSE)
{
    $arr = array(1,2,3,4);

    return ($index === FALSE) ? $arr : $arr[$index];  
}

$value = GetMyArray(1);   // returns only item 1
$array = GetMyArray();    // returns entire array
#4

[eluser]CodeIgniterNewbie[/eluser]
usmc:

1. Your first approach declares a new variable, $you_are_killing_me_smalls. I am trying to avoid declaring a new variable. Instead, I want to access the individual elements of a method that returns an array directly.

2. I'm not too sure what your second approach does. It seems that $this->arrayContainer will always contain an empty array. Also, the call $MyObject->arrayContainer[1]; breaks encapsulation.

wiredesignz:

1. Changing the method signature is not an option.


Isn't there a way to access element values directly from a method that returns an array? It's possible to apply other array functions to such a result (e.g. count($MyObject->GetMyArray()), etc.). So, why not a way to access particular elements?
#5

[eluser]gtech[/eluser]
Quote:Your first approach declares a new variable, $you_are_killing_me_smalls. I am trying to avoid declaring a new variable. Instead, I want to access the individual elements of a method that returns an array directly.

why? you are not adding any performance hit.
#6

[eluser]John_Betong[/eluser]
 
Try this:
Code:
// if the result is an array
   $tmp_array = $MyObject->GetMyArray();

   // this works on my computer
   echo $tmp_array[1];

   // why does this not work?
   echo $MyObject->GetMyArray()[1];

 
 
#7

[eluser]CodeIgniterNewbie[/eluser]
gtech,

Assuming that the contents of each element are not memory hogs, then you can say there isn't a performance hit. My elements aren't memory hogs, but my interest in keeping the code from unnecessarily declaring variables (if possible).

John_Betong,

Your second snippet is what I would like to do. It doesn't work for me either.




Theme © iAndrew 2016 - Forum software by © MyBB