Php Dynamic Variable - php

$X['high'] = 1234;
$var = array("X","high");
This is working:
$temp = $$var[0];
$temp = $temp[$var[1]];
echo $temp;
But this isn't working:
echo $$var[0][$var[1]];
Why? How can i make it works?

You should explain to php parser how you want this statement to be parsed:
echo ${$var[0]}[$var[1]];
Without brackets you will have:
php7
Notice: Array to string conversion in /in/cvZqc on line 5
Notice: Undefined variable: Array in /in/cvZqc on line 5
php5
Warning: Illegal string offset 'high' in /in/cvZqc on line 5
Notice: Array to string conversion in /in/cvZqc on line 5
Sample link.

Related

Undefined offset: 2 and Undefined offset: 1 and Uninitialized string offset: 0

getting the notice error for array how to resolve this unintialized string and undefined offset.
for($i=0;$i<count($exp_company);$i++)
{
$exp_fd2 = explode("/",$exp_fd1 [$i]);
$exp_td2 = explode("/",$exp_td1 [$i]);
$exp_fd = $exp_fd2[2]."-".$exp_fd2[1]."-".$exp_fd2[0];
$exp_td = $exp_td2[2]."-".$exp_td2[1]."-".$exp_td2[0];
mysql_query("insert into empexp(employee_id,company_name,designation,from_date,to_date,exp_description)values('$insert_id','$exp_company[$i]','$exp_designation[$i]','$exp_fd','$exp_td','$exp_description[$i]')");
}
Here You are Trying to Access the index which does not exist.
undefined offset means that an array has run out of bounds as simple as that.
let's assume your array is:
[0]==> 'a',
[1]==> 'b',
From above I cannot access array[2] as it does not exist.
the array size is smaller than the index that you are trying to fetch.

How to work (create and access) empty 2D array in PHP [duplicate]

This question already has answers here:
How to solve PHP error 'Notice: Array to string conversion in...'
(6 answers)
Closed 5 years ago.
I want to define in PHP a new 2D array (to fill and access it later in cycle) but I have problem to deal with it. I went through some articles (e.g. here), but it still does not work for me.
My code is:
$part = array(array());
for ($i=0; $i<4; $i++) {
for ($j=0; $j<3; $j++) {
$part[$i][$j]=3;
}
}
for ($i=0; $i<4; $i++) {
for ($j=0; $j<3; $j++) {
echo "values i=$i, j=$j: $part[$i][$j]\n<br>";
}
}
Result of code above is:
Notice: Array to string conversion in /var/www/html/ftth1/sandbox.php on line 43
values i=0, j=0: Array[0]
Notice: Array to string conversion in /var/www/html/ftth1/sandbox.php on line 43
values i=0, j=1: Array[1]
Notice: Array to string conversion in /var/www/html/ftth1/sandbox.php on line 43
values i=0, j=2: Array[2]
Notice: Array to string conversion in /var/www/html/ftth1/sandbox.php on line 43
values i=1, j=0: Array[0]
...
Line 43 mentioned in output is:
echo "values i=$i, j=$j: $part[$i][$j]\n<br>";
I have tried to use also just a little bit modified version of code from above mentioned article, however result was same:
Code:
$a = array(); // array of columns
for($c=0; $c<5; $c++){
$a[$c] = array(); // array of cells for column $c
for($r=0; $r<3; $r++){
$a[$c][$r] = rand();
echo "$a[$c][$r] \n<br>";
}
}
Result:
Notice: Array to string conversion in /var/www/html/ftth1/sandbox.php on line 55
Array[0]
Notice: Array to string conversion in /var/www/html/ftth1/sandbox.php on line 55
Array[1]
Notice: Array to string conversion in /var/www/html/ftth1/sandbox.php on line 55
Array[2]
Notice: Array to string conversion in /var/www/html/ftth1/sandbox.php on line 55
Array[0]
...
Line 55 mentioned above is:
echo "$a[$c][$r] \n<br>";
Could someone help me with this problem? Thanks.
You need to use braces in you use a "complex" expression in the string interpolation, i.e.
echo "values i=$i, j=$j: {$part[$i][$j]}\n<br>";
Note the { ... } around the $part[$i][$j] part.
An expression becomes "complex" as soon as it's more than a plain variable name.
See PHP manual on simple string interpolation syntax
and complex syntax.
Note that the complex syntax can be used for plain variables as well, i.e. you could use {$i} ... {$j} for consistency.

PHP: Array to string conversion

We're running PHP 5.3.10-1ubuntu3.15 with Suhosin-Patch, and I just ran across the weirdest thing. I keep getting an Array to string conversion error.
Here is some code with line numbers:
115 $report['report'][$key]['report'] = array();
116 watchdog('ranking_report_field', 'key is a: ' . gettype($key), array(), WATCHDOG_NOTICE);
117 $report['report'][$key]['report'] = array(
'#markup' => "<p>No information available.</p><p>For questions, <a href='mailto:$emailAddr'>email</a> your account executive ($emailAddr).</p>",
);
Here are Drupal's (sequential) logs for those line numbers:
Notice: Array to string conversion in foo() (line 115 of /var/www/...
key is a: string
Notice: Array to string conversion in foo() (line 117 of /var/www/...
So far as I can tell there's no array to string conversion that should be taking place. Someone help me out with a second pair of eyes, please - or is this some kind of bug that just hit PHP?
One of the array keys is mapped to a string not an array. Here is a program for how such an error could occur.
<?php
$key = 0;
$report = array();
$report['report'] = array();
$report['report'][$key] = 'report';
// Array to string conversion error
$report['report'][$key]['report'] = array();
// what I assume you are expecting is
$report['report'][$key] = array();
$report['report'][$key]['report'] = array(); // no more notices
NOTE: at his time the OP has not included info for how the array is created

Notice: Undefined offset: 0 using Array in [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
Not sure how to fix this error
Notice: Undefined offset: 0 in C:\xampp\htdocs\streams.php on line 50
Notice: Undefined offset: 0 in C:\xampp\htdocs\streams.php on line 53
Notice: Undefined offset: 0 in C:\xampp\htdocs\streams.php on line 54
Code its referring to:
<?php
$members = array("hawkmyg");
$userGrab = "http://api.justin.tv/api/stream/list.json?channel=";
$checkedOnline = array ();
foreach($members as $i =>$value){
$userGrab .= ",";
$userGrab .= $value;
}
unset($value);
//grabs the channel data from twitch.tv streams
$json_file = file_get_contents($userGrab, 0, null, null);
$json_array = json_decode($json_file, true);
//get's member names from stream url's and checks for online members
foreach($members as $i =>$value){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
Cannot figure out how to fix
The notice of an undefined offset occurs when one calls an array element with the specific index, for example, echo $array[$index], but the index is not defined within the array.
In your code, the array $members has one element (with index 0). So we're walking exactly one time through your foreach loop.
You're calling $json_array[$i]['channel']['channel_url'] where $i = 0, but $json_array[0] does not exist.
You should check the contents of $json_array using print_r() or var_dump().
I tested the script myself, and when I read the contents of the link http://api.justin.tv/api/stream/list.json?channel=,hawkmyg, it returned an empty JSON array. The channel 'hawkmyg' does not exist.
I tried the channel 'hatoyatv', and it just worked.

PHP Unserialize Offset Error

I have this code:
$serialized = $_POST['cartSer'];
echo $serialized;
Which prints this:
a:1:{s:15:\"test\";s:3:\"999\";}
I then add this code:
echo unserialize($serialized);
And end up with this error:
Notice: unserialize() [function.unserialize]: Error at offset 5 of 43 bytes in /mypage.php on line 5
What am I doing wrong with the unserialize?
Sounds like you have magic quotes enabled. Either disable them, or run your value through stripslashes
$serialized = stripslashes($_POST['cartSer']);

Categories