PHP - Variable Variable Array
Code: [Select]
$word = 'numbers'; $numbers= array('1', '2', '3', '4'); echo $$word[0]; I expected the output to be '1'. It ended up being nothing... Why does this not work? Is it not possible to have a variable variable array? And if not, is there a workaround? Cheers, Joe Similar Tutorialshi all, I have an language pack for example: languages/en.php: Code: [Select] $en['mail']['letter closing'] = "regards,\n your friend!"; and in my config: Code: [Select] $language = "en"; $include_language = @include("languages/".$language.".php"); if(!($include_language)) { $try_default_language = @include("languages/nl.php"); if(!($try_default_language)) { echo "kan de taalpakket niet vinden<br>"; echo "Could not find the language pack.<br>"; echo "example on error: ".$test." shows nothing"; exit; } } In my function I want to include the language pack for example i have $language = 'en' so I want to include $en['general']['letter closing'] I will do this: Code: [Select] global $language,${$language}['general']; But that gives an error unexpected '[' blah blah. How can i call the variable variable array in the valid php way? Hi guys im in the middle of optimizing code.. Code: [Select] $sql = "SELECT * FROM sa_enemystats WHERE username='$username'"; $res = mysql_query($sql); while ($row = mysql_fetch_assoc($res)) { $enemy['current_health'] = $row['current_health']; $enemy['current_skill'] = $row['current_skill']; $enemy['level'] = $row['level']; $enemy['damage'] = $row['damage']; $enemy['evade'] = $row['evade']; $enemy['accuracy'] = $row['accuracy']; $enemy['speed'] = $row['speed']; $enemy['luck'] = $row['luck']; echo 'debug: variables synced with db table'; }I know that the setting of these variables are messy and can be done in a better way... but how? I have tried foreach and copying other's code for an array copy but it lead no where. Then again my syntax could be wrong... I used something like Code: [Select] foreach ( $enemy[$value] as $row => $value) { $enemy[$value] = $row[$value]; } I need a help in the following : I have an admin page from where the admin attaches an gif image.This attached image should be shown to the user as a scrolling image. The code for the scrolling image is done through javascript. So my actual problem is getting the name of the attached gif image to the javascript array which is used for scrolling horizontally. Hello all. I am very new to PHP, and I am not sure where to look or what I'm looking for in my current assignment. My task is to take in two numbers between 0-100. Once I take in that number, it should state beside it "The __ was accepted." The program should not accept any numbers greater than 100 or any characters. Once I do this, I must take a second number and do a similar thing. Finally, I must have a statement show up at the bottom stating which number is greater. Essentially, I need help in determining what I should use to place parameters, and how I can keep the program from echo ing any statement until input has been taken and tested for parameters. Any help you can provide will be greatly appreciated! I am trying to allow the user to update a variable he chooses by radio buttons, which they will then input text into a box, and submit, to change some attributes. I really need some help here. It works just fine until I add the second layer of variables on top of it, and I can't find the answer to this question anywhere. <?PHP require('connect.php'); ?> <form action ='' method='post'> <select name="id"> <?php $extract = mysql_query("SELECT * FROM cars"); while($row=mysql_fetch_assoc($extract)){ $id = $row['id']; $make= $row['make']; $model= $row['model']; $year= $row['year']; $color= $row['color']; echo "<option value=$id>$color $year $make $model</option> ";}?> </select> Which attribute would you like to change?<br /> <input type="radio" name="getchanged" value="make"/>Make<br /> <input type="radio" name="getchanged" value="model"/>Model<br /> <input type="radio" name="getchanged" value="year" />Year<br /> <input type="radio" name="getchanged" value="color" />Color<br /><br /> <br /><input type='text' value='' name='tochange'> <input type='submit' value='Change' name='submit'> </form> //This is where I need help... <?PHP if(isset($_POST['submit'])&&($_POST['tochange'])){ mysql_query(" UPDATE cars SET '$_POST[getchanged]'='$_POST[tochange]' where id = '$_POST[id]' ");}?> I have just re-installed Xampp and suddenly my sites are now displaying lots of: Notice: Use of undefined constant name - assumed 'name' in ... Notice: Use of undefined constant price - assumed 'price' in ... this is an example of the line its refering too: $defineProducts[1001] = array(name=>'This is a product', price=>123); My login script stores the user's login name as $_SESSION[ 'name'] on login. For some unapparent reason, i'm getting errors stating that $user and $priv are undefined variables, though I've attempted to define $user as being equal to $_SESSION['name'], using $user to look up the the user's privilege level (stored as the su column ) in the SQL table, and then where the result of the sql query is $priv which is then evaluated in an if statement. I can't seem to figure out why this might not be working. The code I'm using: <?php session_start(); function verify() { //verify that the user is logged in via the login page. Session_start has already been called. if (!isset($_SESSION['loggedin'])) { header('Location: /index.html'); exit; } //if user is logged in, we then lookup necessary privleges. $_SESSION['name'] was written with the login name upon login. Privleges // are written in db as a single-digit integer of of 0 for users, 1 for administrators, and 2 for special users. $user === $_SESSION['name']; //Connect to Databse $link = mysqli_connect("127.0.0.1", "database user", "password", "database"); if (!$link) { echo "Error: Unable to connect to MySQL." . PHP_EOL; echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; exit; } //SQL Statement to lookup privlege information. if ($result = mysqli_query($link, "SELECT su FROM accounts WHERE username = $user", MYSQLI_STORE_RESULT)) { //LOOP TO CYCLE THROUGH SQL RESULTS AND STORE Privlege information as vairable $priv. while ($row = $result->fetch_assoc()) { $priv === $row["su"]; } } // close SQL connection. mysqli_close($link); // Verify privleges and take action. Only a privlege of "1" is allowed to view this page. A privlege of "2" indicates special //accounts used in other scripts that have certain indermediate additional functions, but are not trusted administrators. if ($priv !== 1) { echo $_SESSION['name']; echo "you have privlege level of $priv"; echo "<br>"; echo 'Your account does not have the privleges necessary to view this page'; exit; } } verify(); ?>
hello all, I have an array value in my post array ($_POST['check0'] ) that I am trying to break up into php variables. For some reason if I do this: $var1 = $_POST['check0'][0]; all I get is the first letter of the string from the first value of the array? Hey, im struggeling with a small piece of code.... Thats my code: Global $user_ID; $user_ID = get_current_user_id(); echo"User number $user_ID is loggedin";
Echo gives me this:
Now i want to add it to my array: $atts['href'] .= $user_ID . 'abc'; 'href' is set to "https://test.com/" What im getting now ist https://test.com/abc
Can someone tell my why the '2' is missing??
Hi Everyone
Please checkout the below code:
foreach ($joiners as $joiner) { $town = explode(',', $joiner['address']); if (isset($town[2])) { $town = $town[2]; $get_available_towns[] = $town; $get_available_towns = array(); }
foreach ($joiners as $joiner) { $town = explode(',', $joiner['address']); if (isset($town[2])) { $town = $town[2]; $get_available_towns[] = $town; $get_available_towns = array(); }I don't really understand what is happening. I know the code and everything is correct and it works but I really don't understand how the loop, arrray and variable is working. I wanted to print_r the $get_available_towns outside the loop but it would not work without the square brackets but also I did not even know the square brackets were required. The point was to loop through the data and print all the values. I understand how loops, array's and variables work but I've not seen them used like this. This makes me think I am missing some basic knowledge but I have looked around and I can't see any examples of how loops and arrays are used like this. I have also checked the php manual but still no joy. Can you guys tell me where I could find the information that would have examples on how something like the above code would work? Hello. Can someone show the syntax to echo the variable from the array below that is showing "2010-08-31 23:19"? I need to grab that date and use it elsewhere in my script. Thanks. Code: [Select] array(19) { ["name"]=> string(8) "com_jumi" ["params"]=> array(0) { } ["mainframe"]=> &object(JSite)#2 (6) { ["_clientId"]=> int(0) ["_messageQueue"]=> array(0) { } ["_name"]=> string(4) "site" ["scope"]=> string(8) "com_jumi" ["_errors"]=> array(0) { } ["requestTime"]=> string(16) "2010-08-31 23:19" } Is this right? $admin = array('24.68.200.235','24.68.214.97'); $visitor_ip = $_SERVER['REMOTE_ADDR']; if($visitor_ip == $admin) { echo "k"; } else { die("dead"); Hi, I am basically trying to apply a different amount to $defineMe for certain numbers of an array .. in this case it's array numbers 11 - 16 Code: [Select] <?php if ($defineNumber > 11 and $defineNumber < 16) { $defineMe = 35.00; } else { $defineMe = 50.00; } $defineNumber[01] = array('name'=>'Product 1'); $defineNumber[02] = array('name'=>'Product 2'); $defineNumber[03] = array('name'=>'Product 3'); $defineNumber[04] = array('name'=>'Product 4'); $defineNumber[05] = array('name'=>'Product 5'); $defineNumber[06] = array('name'=>'Product 6'); $defineNumber[11] = array('name'=>'Product 11'); $defineNumber[12] = array('name'=>'Product 12'); $defineNumber[13] = array('name'=>'Product 13'); $defineNumber[14] = array('name'=>'Product 14'); $defineNumber[15] = array('name'=>'Product 15'); $defineNumber[16] = array('name'=>'Product 16'); ?> Hi, I need help with this, I am quite new to arrays. This one works fine: echo array_sum(array(34,19,22,17,7,22,8,23,17,31,21,32,7,29,13,7))/count(array(34,19,22,17,7,22,8,23,17,31,21,32,7,29,13,7)); which gives "19..." it gets average number. What I want to put the values in the array in a variable so I do this: $t = "34,19,22,17,7,22,8,23,17,31,21,32,7,29,13,7"; so why can't I use array like this: echo array_sum(array($t))/count(array($t)); I need to use variable in array, please someone help. Hello Guys. I have a question here.. I have a photo upload script that uploads photos to the server and then sends the photo name to another page via POST. I only have one var name called $image_all when I echo it this is what I get (When uploading 2 files) Code: [Select] This is the list 2011-12-05_0751.png,2011-12-05_0747.png I also tested with "explode" Code: [Select] print_r(explode(',', $image_all, 10)); and I get this Code: [Select] Array ( [0] => 2011-12-05_0751.png [1] => 2011-12-05_0747.png [2] => What I would like for it to do is assign an incremental var name to each image uploaded and passed in the POST. Like this $image1 = 2011-xx.png $image2 = 2011-xx.png $image3 = 2011-xx.png It should created the variable names dynamically based on the number of images (Up to 10) Thanks for your help! Hi everybody, im reading up on arrays and im still stuck... Is it possible to assign an array to a variable? I need something like... Code: [Select] &needvariable = $row->User Hopefully this makes sense... I use a CURL script API with the header "$headers[] = 'Content-Type: application/json';" The script ends with "$result = curl_exec($ch);" If I simply echo the $result to my browser, I get an array that looks like this:
stdClass Object
[toAddress] => stdClass Object Do I really need to "json_decode" this array? I've spent 2 1/2 days trying every which way, with every permutation to access that one value:
echo $result["fromAddress"]["phone"]; Unfortunately I must have fallen asleep during the array lesson in PHP class ? Thank you. hello, um i got a form that stored an array of variables upon users clicks. it submits to another page fine as an array and i can print them using if(!empty($_POST['langSpoken'])) { echo "\n<p><strong>You selected the following LangSpoken:</strong></p>\n<ul>"; foreach($langSpoken as $lang) { // exclude any items with chars we don't want, just in case someone is playing if(!preg_match('/^[-A-Z0-9\., ]+$/iD', $lang)) continue; // print the lang echo "\n\t<li>" . htmlspecialchars($lang) . "</li>"; } echo "\n</ul>"; } else { echo "\n<p>No items selected</p>"; } it prints a list a of array like english spanish but i need to store each one in one variable so i can store in a database is there a way? thanks Hi ! I'd like to count all occurences in "$thiscodestring" into a "dynamic?" array and print it out after the end of the loop, how do I assign the variablenames dynamically ? $query = "SELECT codes, status FROM table"; while ($row = mysql_fetch_assoc($result)) { $thiscodestring = $row[codes]; // looks like "a1 c2 d3 10 15 1 a1 a1"; $singlecodes = explode(" ", $thiscodestring); foreach($singlecodes as $thiscode) { $$thiscode[count] = $$thiscode[count] + 1; } } After all Mysql-Stuff is done I'd like to print out the counted codes, for example the code "a1" was found 100 times in all querys then $$a1[count] should be 100, c2 was found 15 times, then $$c2[count] should be 15 and so on. Question: How do I tell php to assign a "dynamic" variable with the name of the found string which was exploded (in this example a1, c2, d3... ) before ?! $$ does not seem to work. And how can I then print_r() the value if I dont know the name ? Thanks ! Hi all, I can't seem to designate an array key by using a variable and I was wondering if this is possible. I'm looking to do something like this: Code: [Select] <?php $key = "apple"; $arr = array($key => "fruit"); ?> Any suggestions would be appreciated! |