im trying to create an array from a loop in PHP.
I want the array to end up something like $array(100,100,100,100), this is my code:
$x = 0;
while($x < 5){
$array[$x] = 100;
$x++;
}
echo $array[0];
It outputs 1 instead of 100.
Can someone tell me where i'm going wrong.
Even though it works perfectly for me, you should be initialising the variable beforehand.
$array = array();
For example, if $array is non-empty string, you will see the output of 1.
You can just use the predefined array_fill function for that:
$array = array_fill(0, 5, 100);
The other answers pretty much cover this. I would, however, recommend you use a for loop for this instead of a while loop if you're going to use a loop rather than a function to do it.
$array = array();
for($x=0; $x<5; $x++) {
$array[$x] = 100;
}
In fact, you could make this even shorter.
$array = array();
for($x=0; $x<5; $array[$x++]=100);
It's perfectly valid to have a for loop statement instead of a block. And blocks can go anywhere too; they don't need an if statement, for loop, while loop, or whatever, before them.
Related
I need to store each value in an array as one single string. Is it possible to do this?
I have tried using a for loop to achieve this:
for($count = 0; $count < $totalArray; $count++) {
$x = $array[$count];
}
I'm not sure why I thought this would work. it will obviously, keep changing the value and end up as the last value in the array. But can anyone tell me if there is a way to do this?
You need to add to the previous total, not set it:
$x .= $array[$count];
Please tell me which is the best way to unset middle element of associative array in PHP?
Suppose I have an array of 10,000 elements and I want to remove middle element of that array, which is efficient way to remove middle element?
$temp = array('name1'=>'value1','name2'=>'value2',...,'name10000'=>'value10000');
$middleElem = ceil(count($temp) / 2);
$i = 0;
foreach ($temp as $key=>$val) {
if ($i == $middleElem) {
unset($temp[$key]);
break;
}
$i++;
}
Is above code efficient way?
Considering $array is your array, this code remove the middle element if it has odd number of elements. If its event it'll remove the first of 2 middle elements.
$i = round(count($array)/2) - 1;
$keys = array_keys($array);
unset ($array[$keys[$i]]);
Test Result: http://ideone.com/wFEM2
The thing you have to figure out is what you want to do when you have an array with an even number of elements. What element do you want to get then?
The above code picks the 'lower' element, the code could easily be edited to make it pick the 'higher' element. The only thing you have to check is (what all others answers failed to do) what happens if you have three elements. It doesn;t pick the middle element, but the last. So you would have to add a check for that then.
$temp = Array("name1"=>"value1","name2"=>"value2",...,"name10000"=>"value10000");
$middleElem = ceil(count($temp)/2);
$keys = array_keys($temp);
$middleKey = $keys[$middleElem];
unset($temp[$middleKey]);
There ^_^
I think it's a proper way to do it. Try this:
array_remove_at( $temp, ceil(count($temp) / 2) - 1);
function array_remove_at(&$array, $index){
if (array_key_exists($index, $array)) {
array_splice($array, $index, 1);
}
}
You can find the size of the array, divide that number by two and then proceed to remove the element. Not sure about the performance isssues about that though
Firstly, I wouldn't worry too much about what is the most efficient way at this point. You're much better off coding for how easy the code is to read, debug and change. Micro-optimisations like this rarely produce great results (as they're often not the biggest bottlenecks).
Having said that, if you want a solution that is easy to read, then how about using array_splice.
$temp = array('name1'=>'value1','name2'=>'value2',...,'name10000'=>'value10000');
$middleElem = ceil(count($temp) / 2);
array_splice( $temp, $middleElem, 1 );
I would take it that the following code is more efficient, because you don't have to execute it in a loop. I generally follow the same pattern as Kolink, but my version checks if there actually is a "middle element". Works on all types of arrays, I think.
<?php
for( $i = 0; $i <= 9; $i ++ ) {
$temp['name'.$i] = 'value'.$i;
}
if( ( $count = count( $temp ) ) % 2 === 0 ) {
/** Only on uneven arrays. */
array_splice( $temp, ( ceil( $count ) / 2 ), 1 );
}
var_dump( $temp );
EDIT: Thavarith seems to be right; array_splice is much faster than simply unsetting the value. Plus, you get the added benefit of not having to use array_keys, as you already now at what $offset the middle is.
Proper way seems to be:
unset(arr[id]);
arr = array_values(arr);
For first removing element at index id and then re-indexing the array arr properly.
unset($myArray[key])
since your array is associative, you can drop any element easily this way
I am not sure if its a good idea, but i just thought it would be less tedious and much easier to declare variables on the fly using a for loop:
$val.$i = $row1[$i];. Now after trying this, this obviously isn't the right thing to do. Is there anyway i can improve this and not declare separate variables.
Maybe this will give a clearer picture:
for($i = 1; $i < 5; $i++) {
$val.$i = $row1[$i];
}
Now i want to achieve $val1 using $val.$i.
As others have posted, using an associative or 0-based array would be a far better implementation, but you can implement the solution just as you have requested using PHP's variable variable names:
for ($i = 1; $i <= 5; $i++)
{
${"val".$i} = "this is value " . $i;
}
echo "$val1<br />$val2<br />$val3<br />$val4<br />$val5";
Will output:
this is value 1
this is value 2
this is value 3
this is value 4
this is value 5
In PHP you can define variables by name.
Example:
$foo = 'bar';
$$foo = 'baz';
echo $bar; // echoes 'baz'
So in your case, it would look like:
$var = 'val'.$i;
$$var = $arr[$i];
Why you would do that, I have no idea.
A better system (imho) is to use list() construct:
list($val1, $val2, $val3) = $arr;
You could create an associative array and then use extract:
$arr = array();
// ...
$arr[$val.$i] = $row1[$i];
// ...
extract($arr);
Mind you, it will probably be better to use the array in the first place.
I believe you're trying to get all the data you are reading from your database into one easy to access place. All you need to do is shove each row into an array:
$allTheThings[] = $row1;
You'll end up with a two dimensional array where the first key is the row number and the second key is the column number.
I am not getting full picture of what you are trying to do, but to convert arrays into objects you would do this:
$val = (object) $row1;
In regard to my question here, Jacob Relkin suggested a great solution of using call_user_func_array. That solved my problem but now I am really curious on how to do this in the absence of this function to achieve what I wanted in my original question which is below for reference:
Original Question:
I am creating an array of arrays in the following way:
$final_array = array();
for($i = 0; $i < count($elements); $i++) {
for($j = 0; $j < count($elements); $j++) {
if($i!=$j)
$final_array[] = array_intersect($elements[$i], $elements[$j]);
}
}
I am trying to find out the list of elements that occur in all the arrays inside the $final_array variable. So I was wondering how to pass this to array_intersect function. Can someone tell me how to construct args using $final_array[0], $final_array[1], ... $final_array[end_value] for array_intersect? Or if there is a better approach for this, that would be great too.
I am looking for a way to construct the following:
array_intersect($final_array[0], $final_array[1], $final_array[2], ...)
Well, the only real way to do this other than call_user_func_array would be to implode the resulting array into comma-separated arguments, then do something really really evil and use eval:
$args_imploded = implode(',', $some_array);
$result = eval('return array_intersect(' . $args_imploded . ')');
Why don't you just avoid the evil eval function and use the 'call_user_func_array' function? From what I understand about your code is that the $final_array parameter is an array of array's.
$result = call_user_func_array('array_intersect', $final_array);
No need for the eval function here.
EDIT: Stupid me. I didn't read your first paragraph properly ;). Please ignore this.
How can you convert the following while loop to a for -loop in PHP?
while( $row2 = pg_fetch_row( $result_tags )
While -loops are a source of errors for me.
I see the while -loop as follows.
for ( $i = 0 ; $i < count(pg_fetch_row( $result_tags )) ; $i++ )
You can't convert that while to a for.
for loops are used for incremental loops. while loops are used to execute code until a condition is false. While most for loops can be easily converted to while loops (for loops are just a syntactical enhancements of while loops), the opposite is not always possible.
Consider the following loop:
while($row = pg_fetch_row($result)) { }
This loop will execute until pg_fetch_row() returns a falsy value.
Note: The proper syntax for such a loop would be:
while(($row = pg_fetch_row($result)) !== FALSE) { }
Unfortunately, the closest you can come to using a for loop is the following:
for(; $row = pg_fetch_row($result) ;) {}
Which will behave exactly the same as the while loop anyway.
Note: Again, the proper syntax for such a loop would be:
for(; ($row = pg_fetch_row($result)) !== FALSE ;) { }
I think you should go back in your code and find exactly the cause of your problem instead of blaming the use of the while loop.
pg_fetch_row() returns an array of columns, so you cannot use count() on it.
The closest you can come to using a for loop would be using pg_num_rows as such:
for($i = 0; $i < pg_num_rows($result); $i++) {
$row = pg_fetch_row($result);
}
But personally I find that unnecessarily verbose and open to more problems.
Going in the other direction, a for loop:
for (init; test; incr) {body;}
is equivalent to:
init;
while (test) {
body;
incr;
}
That is, a for loop is a special kind of while loop. Personally, I don't see how converting the while loop you give to a for loop will reduce errors.
Why do you want to do this? The better way is to declare/instantiate $i = 0 and then increment it at the end of the while loop.