Store Values into array PHP - php

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

Related

Issues creating an associative array with another array in PHP

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();
}

how can i have dynamic name of a array variable in php?

I used this but my array show blank.
foreach($page_data->result() as $text){ $i++;
echo $name="text".$i; //this line print ok.
$$name=array();
echo $$name['content']=$text->$content; //this line print ok.
print_r($text1);
} print_r($text1);
here i am tying to name the array dynamically as text1,text2,text3.......
but when i print $text1 it shows me a blank array.
can any one help me out with this.
It's simply a syntax ambiguity. $$name['content'] is understood as:
${$name['content']}
I.e. the name of the variable is supposed to be the value of $name['content'], which obviously doesn't exist, which actually leads to an error if you'd enable error reporting. You can solve this with:
${$name}['content'] = $text->$content;
However, you really should solve this by using an array instead of variable variables:
$texts[] = array('content' => $text->$content);
You're incrementing the index before you echo the contents of the variable, so if you only have 1 result, it will try and access an undefined index, you are also re initialising the name array every time you pass through the loop, you should not do this
Results
------------------
Num Content
0 I am a text post
If these were the results returned and you wanted to point to index i, then when your loop goes through this would happen:
i = 0;
i = 1;
assign values to name array
As there is only one result this would then happen
Results
------------------
Num Content
0 I am a text post
- - <----- i = 1 (null pointer exception?)
This would be why no values are showing in your array, try change your code to this:
// Declare i to point at the 0 index
$i = 0;
$name=array();
foreach($page_data->result() as $text){
$name[i]['content']=$text;
$i++;
}
Your name array will no add the text content to a new index with each pass of the loop, i.e. if you had 2 results
$name[0]['content'] = "This is a text post";
$name[1]['content'] = "Here is another post";
Hope this helps.

PHP: xPath - write first element in variable in 1 line

This is what I have:
$namex = $xml->xpath("//a[#b=foo]/c");
$name = $namex[0];
echo $name;
It works, as the first line creates an array and the second line reads the first entry. Is there a way to combine the two lines to get the intended result right away?
You can use list:
list($name) = $xml->xpath("//a[#b=foo]/c");
Is there a way to combine the two lines to get the intended result right away?
Yes and no...
PHP 5.4+:
echo $xml->xpath("//a[#b=foo]/c")[0]; // array dereferencing, yay!
PHP < 5.4:
$names = $xml->xpath("//a[#b=foo]/c");
echo $names[0];

Trying to fill a PHP array

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];
}

PHP arrays. inserting "$key" => "$value" pair into array with array_push();

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...

Categories