On a previous page, I have gotten the names and the number of playeys values from the user.
I wish to fill them into an array. Here is my code so far:
<?php
$numberOfPlayers = $_POST['numberOfPlayers'];
$counter = 1;
$playerName = array();
while($counter<=$numberOfPlayers-1){
$playerName[$counter-1]= $_POST[$counter];
$counter=$counter+1;
}
print_r($playerName);
?>
However it is throwing out an error saying "Undefined offset: 1 in C:\wamp\www\test.php on line 8".
Any help would be much appreciated.
Thanks
Just use array_push(). or [] array short hand. No need to use a counter.
while($numberOfPlayers--){
if(isset($_POST[$numberOfPlayers]))
$playerName[]= $_POST[$numberOfPlayers];
}
Related
what I have to change to this part will work?
$array4Midfeld = array('$LM', '$DMEins', '$DMZwei', '$RM');
$array4Midfield[0] = "Test";
At the moment I get an error, regarding to the second line.
Error is... $LM not defined.
There is spelling mistake in array name on line number 2.
You have created array with $array4Midfeld name and accessing it with
$array4Midfield name on line number 2. That's why you are getting error.
try following code, It is working.
<?php
$array4Midfield = array($LM, $DMEins, $DMZwei, $RM);
$array4Midfield[0] = "Test";
print_r($array4Midfield);
?>
You have two problems with this:
Are you assigning variables as elements of the array? (ie $LM, $DMZwei, etc), or are they text? "$LM", "$DMZwei"
Your variables are not the same. Line 1 and Line 2 are two different vars: array4Midfeld and array4Midfield, are they supposed to be the same?
So are you testing that
`$array4Midfield[0] = "Test"
or
`$array4Midfield[0] = $LM
or
$LM = "Test"
Please assign Values to an array in this form. More over the array name too, am confuse whether they are the same or not. $array4Mittelfeld or $array4Midfield
$array4Mittelfield[0] = $LM;
$array4Mittelfield[1] = $DMEins;
$array4Mittelfield[2] = $DMZwei;
$array4Mittelfield[3] = $RM;
echo $array4Mittelfield[0];
or you can use loop to print all the values
Hope it helps
I'm trying to create an associative array in PHP using an Object. The Object is values from a database, called Category. It only has two values, an id and a name field.
This is what I have:
$category = $em->getRepository('AppBundle:Category')->findAll();
$stuff = array();
foreach($category as $cat) {
$stuff[$cat->getName()] = $stuff[$cat->getId()];
}
But I get this nasty error:
Notice: Undefined offset: 1
I should say that I am using Symfony 3. Any help would be great.
You're on the right track but you need to remove echo since you're not trying to output anything. You're also attempting to assign a value from the array you're trying to enter a value into.
$stuff[ $cat->getName() ] = $cat->getId();
Sorry I just omitted the value and replace with $cat->getId() in stead of $stuff[$cat->getId()] and it worked
You $stuff array has no index 1, so it will cause that issue.
This is caused by $stuff[$cat->getId()];, which should be $cat->getId();
foreach($category as $cat) {
$stuff[$cat->getName()] = $cat->getId();
}
As stated in the title, I encounter a strange problem for the following code.
for($i=0; $i < count($maindata) ; $i++){
$currentId = $maindata[$i]['SubmissionId'];
$output = array_filter($subdata, function ($value) use ($currentId)
{ return $subdata['ParentId'] == $currentId; });
echo 'total sub data '.count($output); //output 86
for($j=0; $j < count($output) ; $j++){
echo $output[$j]['SubmissionId']; //Undefined offset Error
}
}
As you can see, I loop through an array. Inside the loop I filter out another array to get the associate data.
And then I echo the count of the filter array and echo the data with another loop. The code was fine for first item and it show data but starting from the second item, it show
Notice: Undefined offset: 0 in myfile on line 79
inside the second loop but the count still showing correct answer also. line 79 is echo $output[$j]['SubmissionId']; //Undefined offset Error .
Please help me to figure out what is the problem. Thanks in advance.
Solution:-
Inside second iteration use this:-
if(isset($output[$j]['SubmissionId'])){echo $output[$j]['SubmissionId'];}
Explanation:-
$output is a filtered sub-array and due to filtration it's quite possible that some indexes are missing
when you did for($j=0; $j < count($output) ; $j++){ and if count($output) is 86 means loop will go for 0-85 (in sequential order)
Now if $output has missed index 3 (for example) then the code $output[$j]['SubmissionId'] will give error
So check that using above code and problem is solved.
2nd opinion:-
you can do like below:-
$output = array_values($output);
echo 'total sub data '.count($output); //output 86
for($j=0; $j < count($output) ; $j++){
echo $output[$j]['SubmissionId'];
}
Note:- this approach will re-index your array and if some-where you are going to use original indexes then this code will lead you to problems.
3rd option(better than 2nd one)
Since foreach() take care of indexes so instead of for() loop use foreach.
Thanks
In first place , i think that "foreach" loop instead of "for" will be more clear and a better practice for what you need.
http://php.net/manual/fr/control-structures.foreach.php
In a second time, do you try to make a var_dump instead of echo to see what structure you have into your array?
for($i=0; $i < count($maindata) ; $i++){
$currentId = $maindata[$i]['SubmissionId'];
$output = array_filter($subdata, function ($value) use ($currentId)
{ return $subdata['ParentId'] == $currentId; });
var_dump($output);exit();
.....
Maybe you don't have SubmissionId sub-index inside.
I found the problem by printing out the array as #Anant mention. At first I thought $output arrary's index will be starting from zero as normal array. But, since I got this from array_filter, the index starting from the end of last filtered array.
So I change the second loop to foreach and problem solved now.
Thank you all!!!
This seemed simply but i'm running into a problem. I'm building an autocomplete drop down box using jquery ui. It works fine with static information but when i try put dynamic data from mysql in the array it freaks out.
Parse error: syntax error, unexpected T_DO, expecting ')' on line 46
Line 46 is the do in the array
$items = array(do {
$row_Recordset1['ARTIST'];
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); );
I've searched the net but I can't find anything relating to problems with a do in an array.
Sorry if this is really stupid but I can't seem to get it to work
Thanks
You can't execute code inside an array statement. Please read the manual for the array method. It excepts values , not code.
You should initialise your array , and then use your do/while loop to populate data.
$items = array();
do{
$items[] = $row['artist'];
}while('condition');
Your code should be like below, even should not use do ... while loop.
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
$items[] = $row_Recordset1 ['ARTIST'];
}
Why won't this work?
$slidetotal=1;
$slideids = array();
while ($rowcs = mysql_fetch_array($orig_slides_result)) {
$key = $slidetotal;
array_push($slideids[$key], $rowcs['id']);
$slidetotal++;
}
I get this error:
[phpBB Debug] PHP Notice: in file ///*.php on line 161: array_push() [function.array-push]: First argument should be an array
Although someone has commented you can do this on this page:
http://php.net/manual/en/function.array-push.php , (find: "to insert a "$key" => "$value" pair into an array")
What is the next best way to insert a list of single values into a php array? By the way, I really can't believe it's hard to find something on this with google.com. Seriously?
That PHP.net comment is incorrect. That is pushing $rowcs['id'] onto the array $slideids[$key], not the array $slideids.
You should be doing the following, in place of your array_push() call:
$slideids[$key] = $rowcs['id'];
Why don't you do;
$slidetotal=1;
$slideids = array();
while ($rowcs = mysql_fetch_array($orig_slides_result)) {
$slideids[$slidetotal] = $rowcs['id'];
$slidetotal++;
}
Also you can do like below if you don't need the key to start from 1;
$slideids = array();
while ($rowcs = mysql_fetch_array($orig_slides_result)) {
$slideids[] = $rowcs['id'];
}
ummm hard-searching will work for google I think :)
anyway, error tells you everything you need to know. that means first argument of array_push is not an array, you give a single value (string) to array_push ($slideids[$key]).
Also why do you need to use array_push in php? I'd rather use
$slideids[] = $rowcs['id'];
and what you're trying to do is:
$slideids[$key] = $rowcs['id'];
i guess...