View Full Version : PHP Looping Arrays
Goldfiles 08-12-2008, 10:27 PM I'm trying to add some items to an array using a loop, but it isn't quite working. For example:
This Works:
$array[]['test'] = 0;
$array[]['test'] = 1;
$array[]['test'] = 2;
echo $array[0]['test']; //outputs 0
echo $array[1]['test']; //outputs 1
echo $array[2]['test']; //outputs 2
This Doesn't Work:
for($i=0; $i<3; $i++){
$array[]['test'] = $i;
}
I know I left the first set of brackets empty [], that was on purpose. I just want php to auto increment that. I want to be able to just pop values onto this multidimensional array. It works just great if I type it all out manually like in my first example, but nothing gets added to the array when I use a loop. Anyone know how I can get this to work?
foobic 08-12-2008, 10:45 PM Works for me. :stickout:
# cat phparray.php
<?php
$array[]['test'] = 0;
$array[]['test'] = 1;
$array[]['test'] = 2;
print_r($array);
for($i=0; $i<3; $i++){
$array2[]['test'] = $i;
}
print_r($array2);
?>
# php -f phparray.php
Array
(
[0] => Array
(
[test] => 0
)
[1] => Array
(
[test] => 1
)
[2] => Array
(
[test] => 2
)
)
Array
(
[0] => Array
(
[test] => 0
)
[1] => Array
(
[test] => 1
)
[2] => Array
(
[test] => 2
)
)
BTW, using ['test'] like that you're creating an array of arrays - is that what you really want to do? For a simple array just use
$testarray[] = 0;
alphapatrol 08-13-2008, 06:50 AM Your code works for me. Let PHP to output the results (I used the iteration for clearer output only, but it's not necessary):
for($i=0; $i<3; $i++){
$array[]['test'] = $i;
}
foreach ($array as $item) {
echo var_dump($item) . "<br />";
}
// you should get this:
// array(1) { ["test"]=> int(0) }
// array(1) { ["test"]=> int(1) }
// array(1) { ["test"]=> int(2) }
// or simply use just: var_dump($array);
Goldfiles 08-13-2008, 10:56 AM Oh, I see where my problem is now.
I'm trying to do something like this:
for($i=0; $i<3; $i++){
$array[]['test'] = $i;
$array[]['blah'] = $i;
}
Once I introduce the second value like that, it creates incrementing issues.
I want it to read
$array[0]['test'] = 0;
$array[0]['blah'] = 0;
$array[1]['test'] = 1;
$array[1]['blah'] = 1;
And so on, but that is not the case.
anton_1980 08-13-2008, 03:50 PM here's how to do it:
for($i=0; $i<3; $i++){
$newarray['test'] = $i;
$newarray['blah'] = $i;
$array[] = $newarray;
}
foobic 08-13-2008, 06:17 PM Or you could simply rearrange your keys:
for($i=0; $i<3; $i++){
$array['test'][] = $i;
$array['blah'][] = $i;
}
Saeven 08-14-2008, 01:39 PM That a very unusual application of an array though, I can't think of any case in my career where I've resorted to auto-indexing to generate a keyvalue.
Why wouldn't something like this work for you?
// this recreates your initial structure
$array = array(
array( 'test' => 1 ),
array( 'test' => 2 ),
array( 'test' => 3 )
);
// you can iterate over it like this
foreach( $array as $inner_array ){
foreach( $inner_array as $k => $v ){
echo "{$k} --> {$v}<br>";
}
}
Helps?
Alex
foobic 08-14-2008, 06:55 PM I can't think of any case in my career where I've resorted to auto-indexing to generate a keyvalue.
I find that quite surprising. Coming from other languages I see lists and associative arrays (hashes) as completely different animals, so I mentally label any PHP array as one or the other. For a variable I consider to be a list I'd have no problems using auto-indexing (or array_push, which seems like better list syntax).
Saeven 08-14-2008, 06:59 PM I think that you misconstrued what I wrote. Yes we often use associative arrays, or simple lists. What one rarely does though, is rely on the magic subscript operator to create an index for associative arrays, and then rely on that index as an accessor down the road.
$array[]['test'] = 0;
$array[]['test'] = 1;
$array[]['test'] = 2;
echo $array[0]['test']; //outputs 0
echo $array[1]['test']; //outputs 1
echo $array[2]['test']; //outputs 2
Bad code.
foobic 08-14-2008, 08:08 PM Generally though (ie. outside of PHP), is it really bad code to push values to a list and then access one element by its index? If it's really a list, you know for certain that its indexes range from 0 to length-1 and you might want to know the value of an element somewhere in the middle. Probably not the best example but suppose you were looking for a median value?
I think (may be wrong?) that PHP's magic subscript is the direct equivalent of the push command you'd use elsewhere to add a single value to a list, so provided you're disciplined about constructing the list then it shouldn't be a problem. Sure, you're working without a safety net but that's a limitation of PHP's horribly blurry list / array variable type.
Saeven 08-14-2008, 08:31 PM Generally though (ie. outside of PHP), is it really bad code to push values to a list and then access one element by its index?Not at all, subscript index access is often required. What is unnecessary though, is pushing arrays onto a parent array (that serves no purpose) for later iteration.
When I post here, I try to post in context of the op's thread though:
Title: Looping Arrays
Using subscript-access on array to push hashes onto an array, for later iteration-type reading (loop?), is bad. The code posted here: http://www.webhostingtalk.com/showpost.php?p=5263331&postcount=7
Will have much better results over iteration; foreach is highly optimized with PHP5 (compare it with subscript access, you'll find that benchmarks are clear!)
foobic 08-14-2008, 08:47 PM Ah, ok - that makes sense. :agree: I was just taking your first remark in a wider context than intended.
|