|
What is the difference, if any, between these methods of indexing into a php array:
$array[$index]
$array["$index"]
$array["{$index}"]
Thanks!
Started by svec on
, 10 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
@Jeremy: I'm not sure that'....
If $index is a number, for example 10, the first line will evaluate to $array[10] and the other two lines will evaluate to $array["10"] which is a different element than $array[10].
To the same string.
|
|
Eg:
$array= array(array(141,151,161),2,3,array(101,202,array(303,606)));
output :606
Started by Srinivas Tamada on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Foreach( $array as $value ) { // check if $value is an array....
What you need is to recursively go through your array ; which means the max function, which multimax( $array ) { // use foreach to iterate over our input array.
|
|
What is differences between Multidimensional array and Array of Arrays in C#? if there a difference.
What is the best use for each one?
Started by ecleel on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Consider following....
Array of arrays (jagged arrays) is faster than multi-dimensional arrays and can be used more will see that storage and retrieval from jagged (or single dimensional array) is simple IL instructions.
|
Ask your Facebook Friends
|
I need to create an array of arrays.
I have been using array_map(null,$a,$b,$c) to do this and it works fine, however, it doesn't work if one of the mapped arrays doesn't exist.
To get around this problem I have used:
$myArray= array(); if (isset($a))...
Started by ticallian on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
array(); foreach($arrays as $array){ if(!empty($array)) array_push($output, $array); } return $output; }
call like: joinArrays($a, $b, $c, etc..);
$myArray = array_filter(array($a, $b, $c));
....
|
|
I have an array whose values are all arrays of a specific format that looks like this:
Array ( [0] => Array ( [username] => John ) [1] => Array ( [username] => Joe ) [2] => Array ( [username] => Jake ) )
and I would like to have this...
Started by jcmoney on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Why complicate things?
foreach($array as $k=>$v) { $new[$k] = $v['username']; }
If you're using PHP 5.3 you can make use of array_walk_recursive and a closure (the closure can be replaced by a normal function if your PHP version <....
|
|
Pointless Dribble
Okay This is another weird one from me, i want to thank OIS for helping me out on my last question... which deals with this same kind of funky array manipulation... i studied that code in depth and i feel it has helped me become better...
Started by youdontmeanmuch on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Now it finds the ....
Edit: Changed the function now that you've removed the 'depth' keys from your example array.
I wasn't able to test on your example array but it seems to work on a smaller array I made.
I think this should work...
|
|
Per the example array at the very bottom, i want to be able to append the depth of each embedded array inside of the array. for example:
array ( 53 => array ( 'title' => 'Home', 'path' => '', 'type' => '118', 'pid' => 52, 'hasChildren' ...
Started by youdontmeanmuch on
, 8 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Note that we also use']) { setdepth(&$arr[$key]['children'],....
Foreach($a as $key=>$value) { if (is_array($value)) setDepth($a[$key], $depth+1); } }
The thing to note is that the array is passed by reference, so that we can modify it.
|
|
My class has a member variable array, items. Periodically I reassign the array to be the value of another, temporary array, like this:
$temp = array(); $temp[] = new Object(); $temp[] = new Object(); $temp[] = new Object(); ... etc. $this->items = ...
Started by Chad Johnson on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Since PHP is a garbage collected language, the array will be deleted (garbage collected) when there are no more references to the array..
To the same array.
|
|
If I write
int *columns[32];
am I defining an array with 32 pointers to int s?
Or is it a pointer to an array of 32 int s?
How do I differentiate between the two? Is there a difference?
Started by Carson Myers on
, 9 posts
by 9 people.
Answer Snippets (Read the full thread at stackoverflow):
To define a pointer to an array of 32 ints you have to....
You are defining an array of 32 pointers.
Declare columns as array 32 of pointer to int
EDIT In response to comments: I found cdecl source to be an array of pointers.
|
|
Title says it all.
Though the manual says you're better off to avoid a function call, I've also read $array[] is much slower than array_push(). Does anyone have any clarifications or benchmarks?
Thank you!
Started by alex on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Edit : Ran this code:
$t = microtime(true); $array = array(); for($i = 0; $i < 10000; $i++) { $....
No benchmarks, but I personally feel like $array[] is cleaner to look at, and honestly splitting of strings to your array.
|