Undefined index notice even though i can get the data - php

I'm working with an array that gives me all the info if print_r but it also says that it is an unidentified index.
Code:
foreach ($_SESSION['passo4'] as $key => $value) {
$x = $data_ref[0]['tipo_refeicao']; //gives me the error
echo $x; //echoes 1
print_r($data_ref);
if($key != 'preco'){
//Obter info do tipo de vestuário
$f_r = $dbh->prepare("SELECT tipo_refeicao, preco_acompanhante, preco_participante FROM refeicao WHERE id_extra = '$key'");
$f_r->execute();
$data_ref = $f_r->fetchAll();
echo "<tr><td>".
datasearch($data_tref, 'tipo_refeicao', $x, 'descricao')
."</td>";
echo "<td>". $value ."</td>";
echo "<td>". $data_ext[0]['preco'] * $value ."€</td></tr>";
}
}
Notice
Notice: Undefined offset: 0 in C:\xampp\htdocs\Rot.Aventura\eventos\passo5.php on line 96
Print_r($data_ref):
Array ( [0] => Array ( [id_refeicao] => 4 [id_evento] => 11 [tipo_refeicao] => 1 [preco_participante] => 5 [preco_acompanhante] => 6 [limite_pessoa] => 2 ) )
die($x): 1
Should i hide this notice with # or is there any way to solve this? (Sorry for the portuguese words)

Since you are in a foreach loop you don't have to use index 0. foreach auto increment the index, so on next index 0 is undefined
Please try without index or put $x out of loop
foreach ($_SESSION['passo4'] as $key => $value) {
...
$data_ref = $f_r->fetchAll();
$x = $data_ref['tipo_refeicao']; //gives me the error
or
foreach ($_SESSION['passo4'] as $key => $value) {
...
$x = $data_ref['tipo_refeicao']; //gives me the error
Give it a try

The problem was an error with my code:
$f_r = $dbh->prepare("SELECT tipo_refeicao, preco_acompanhante, preco_participante FROM refeicao WHERE id_extra = '$key'");
Where is id_extra it should be id_refeicao. Thank you for your time and help

Related

Calling index number location using PHP

I've got a bit of a problem, I have 2 inputs, one to type words, the other to find a keyword in the word array,
My code looks like this
if (isset($_POST['ord'])){
$name = $_POST['ord'];
$searchWord = $_POST['sök'];
$nameArray = (explode(" ", $name));
foreach ($nameArray as $key => $value) {
if ($value === $searchWord) {
echo substr_count($name, $value) ;
echo "<br>";
echo array_keys($value, $name);
break;
}
}
}
The problem I have is with this line
echo array_keys($value, $name);
I can only get it to call out the position of the index once before it stops, I want it to call out all the index positions, In this cause it would be 2 and 4
It might also have to do with the break; but then the problem remains that the break prevents the first line to repeat which is why I added it
This is what the output looks like (Can't post image yet)
Array ( [0] => hi [1] => Hey [2] => hi [3] => Hey )
The word Hey was found 2 times
Warning: array_keys() expects parameter 1 to be array, string given in C:\wamp64\www\labb-1a-php-sidor\sida3.php on line 39
You don't need a loop. Just call array_keys() correctly -- the first argument should be the array.
if (isset($_POST['ord'])){
$name = $_POST['ord'];
$searchWord = $_POST['sök'];
$nameArray = (explode(" ", $name));
$result = array_keys($nameArray, $searchWord);
echo "The word $searchWord was found " . count($result) . " times<br>";
var_dump($result);
}

Drupal - get value from array

Maybe it's me but I can't get the values from the following array:
http://picpaste.com/pics/Untitled-2YV5V2Im.1427134235.png
What I want is to create a table where the headers are like this:
HomeTeam name | AwayTeam name | Match home_goals | Match away_goals
and then I have 9 rows with values.
My code so far:
$json = json_decode($server_output, true);
$days= $json['Calendar']['championship']['StageRound'][0]['matches'];
$header = ['HomeTeam name', 'AwayTeam name', 'Match home_goals', 'Match away_goals'];
$row = array();
foreach ($days as $key => $value) {
... here, I get always an error saying 'HomeTeam' is not an index...
}
$table = theme('table', array('header' => $header, 'rows' => $rows));
return $table;
Any help?
Thanks!
EDIT:
Added this code:
foreach ($days as $key => $value) {
$hometeam = $days[0]['HomeTeam']['name'];
$awayteam= $days[0]['AwayTeam']['name'];
dpm($hometeam . ' - ' . $awayteam);
}
I have the index [0] in both lines inside the for cycle but I need it to be from 0 to 9 (the lenght of the array. That would solve my problem.
Foreach is already iterating trough that matches array, so you don't need that [0]. Go with:
$hometeam = $value['HomeTeam']['name'];
$awayteam= $value['AwayTeam']['name'];
or
$hometeam = $days[$key]['HomeTeam']['name'];
$awayteam= $days[$key]['AwayTeam']['name'];
if you like to access array over key...

Undefined offset 0

I have problem with this error script.
private function setCentroidCluster(){
for ($i=0;$i<count($this->centroidCluster);$i++){
$countObj = 0;
$x = array();
for ($j=0;$j<count($this->objek);$j++){
if ($this->objek[$j]->getCluster()==$i){
for ($k=0;$k<count($this->objek[$j]->data);$k++){ // Error
$x[$k] += $this->objek[$j]->data[$k];
The error is:
Notice: Undefined offset: 0
Notice: Undefined offset: 1
The error in line:
$x[$k] += $this->objek[$j]->data[$k];
First:
$x is an empty array. You want to add something at index $k. This is undefined. You need to define at least something there. There's a diff between auto-assigning array values and incrementing an existing array element:
for ($k=0;$k<count($this->objek[$j]->data);$k++){
if ( !isset($x[$k]) )
$x[$k] = 0; // depending on the type of data[$k] !!!
$x[$k] += $this->objek[$j]->data[$k];
}
should do the trick.
And as a recommendation, make yourself familiar with foreach:
foreach ($this->objek as $obj => $dat )
{
if ( $obj->getCluster() == $i )
{
foreach ( $dat as $datelem )
....
etc.

PHP array not extracting as expected - why is this?

When using an array in a foreach loop
$stdlist = rtrim(trim($_POST['stdlist'], '/'), '/');
$stdlist = explode('/' , $stdlist);
print_r($stdlist);
foreach($stdlist as $value)
{
echo "<br>";
echo $_POST[$value];
}
the array $stdlist is clearly working; when printed this returns:
Array ( [0] => 1 [1] => 6 [2] => 7 [3] => 8 )
My problem is that when I use the foreach loop to extract out of the array one value at a time, the following gets printed to the page:
4
4
Notice: Undefined offset: 7 in C:\Program Files\wamp\www...on line 35
Notice: Undefined offset: 7 in C:\Program Files\wamp\www...on line 35
I know this isn't functioning as intended as I am expecting the following:
1
6
7
8
Could somebody please explain why this is happening and how to fix this issue? Thanks :-)
You have to print the $value bacause $value have original array value not index.
And you are getting array in $stdlist from exploding this post variable $_POST['stdlist'].
foreach($stdlist as $value)
{
echo "<br>";
echo $value;
}
Now you will get your required result.
instead of using echo $_POST[$value]; just use echo $value when you use foreach loop for an array, the values on each nodes are automatically extracted.
foreach ($array as $index=>$value){
echo "index is $index and value associated with it is $value.";
}
Hope this helps.
foreach($stdlist as $value)
{
echo "<br>";
echo $value;
}
when you use foreach the $value is not the position in the array, if you want to use the position you need to do
for($pos=0; $pos<sizeof($stdlist); $pos++)
{
echo "<br>";
echo $stdlist[$pos];
}
When using the foreach() loop, I'd recommend assigning both the position and value to their own respective variables and then print them to the screen to see how the foreach loop has assigned the values.
foreach( $stdlist as $position => $value ) {
echo "The current position is $position, and the value of \$stdlist[$position] is
$value";
}

Print array with for loop

I want to print this array to all indexes upto 21, but in this code this is printing only to array length, what i should i do the print whole array in for loop?
<?php
$array=array(0=>"hello",
1=>"world",
2=>"this",
3=>"is",
4=>"an",
20=>"array",
21=>"code" );
$length=count($array);
for($i=0;$i<$length;$i++){
echo "$i=>".$array[$i];
echo "<br />";
}
?>
Your difficulty is the way you're defining your array:
$array=array(0=>"hello",
1=>"world",
2=>"this",
3=>"is",
4=>"an",
20=>"array",
21=>"code" );
Arrays in php are really hashmaps; when you call index 5 on the above array, it is undefined. No index item up to 20 will be defined, and these will Notice out:
PHP Notice: Undefined offset: 5
Because you're using array length as your iterating variable, and calling exactly that variable, you will never get positions 20 and 21 in your code.
This is what your array looks like to the computer:
0 => "hello"
1 => "world"
2 => "this"
3 => "is"
4 => "an"
5 => NULL
6 => NULL
7 => NULL
... //elided for succinctness
19 => NULL
20 => "array"
21 => "code"
When you call $array[7] it can't return anything. When you call $array[20] it will return "array".
What you really want is a foreach loop:
foreach($array as $key => $val) {
//key will be one of { 0..4, 20..21}
echo "$key is $value\n";
}
Resulting in:
$ php test.php
0 is hello
1 is world
2 is this
3 is is
4 is an
20 is array
21 is code
If you must use a for loop:
$key_array = array_keys($array);
for($i=0;$i<count($key_array);$i++){
$key = $key_array[$i];
echo "$key => ".$array[$key]."\n";
}
Note this is not a clean solution.
Solution with a for loop:
$array=array(0=>"hello",
1=>"world",
2=>"this",
3=>"is",
4=>"an",
20=>"array",
21=>"code" );
$max = max(array_flip($array)); // What if max array key is 10^5 ?
for($i=0;$i<=$max;$i++){
if(isset($array[$i])){
echo "$i=>".$array[$i]."<br>";
}
}
foreach($array as $key=>$value){
echo $key."=>".$value;
echo "<br />";
}
You want to start your loop with $i=0 as PHP uses zero indexing. also in your loop you want to cap your max value for interation at $i
<?php
$array=array(0=>"hello",
1=>"world",
2=>"this",
3=>"is",
4=>"an",
20=>"array",
21=>"code" );
$length=count($array);
for($i=0;$i<$length;$i++){
echo "$i=>".$array[$i];
echo "<br />";
}
?>

Categories