Array Reference Notation
when referencing an array the notation is array[arrayindex] so if you have a two dimensional array shouldn't array[index1][index2] work? I'm trying to get $_FILES[myfile][name] and when I echo it I get Array[name]? If I do
$temp=$_FILES[myfile];
echo("$temp[name]");
View Complete Forum Thread with Replies
Sponsored Links:
Related Messages:
How To Reference An Array In Multidimensional Array?
How do I reference an array within an array? $mapped_array = array_map(null, $data_array, $counter_array); // okay, so now I have an array of 2 arrays - but how do I reference each array? // the next two lines fail to give an accurate count echo "Starting with ".count($mapped_array[0])." elements in data_array<br>"; echo "Starting with ".count($mapped_array[1])." elements in counter_array<br><br>"; // now I want to start at the bottom and look for an IP in each line of data_array // which I assume is mapped_array[0] for ( $i=count($mapped_array[0]); $i > 0; $i-- ) { $data_line = $mapped_array[0][$i]; $data = explode("|", $data_line); if( in_array($ip, $data) ) { // if there's a match, element[$i] of both arrays needs to be spliced out array_splice($mapped_array,$i,1); } } // again, these next two lines fail to return an accurate count echo "Ending with ".count($mapped_array[0])." elements in data_array<br>"; echo "Ending with ".count($mapped_array[1])." elements in counter_array<br><br>"; Is there something about array_map that prevents me from referencing the arrays within the array?
View Replies !
View Related
Passing An Array As A Reference To A Function
Noobie here (C++/C/Java experience though) ... Recently picked up PHP ... I want to pass an array to a function and then to use count on the passed variable - is this the way to do it (its an object method) : public function foo(array& $arr) { if (count($arr) < 1) throw new MyException("Blah blah ...") ; .... } I have two questions for the pros: 1). Is the function signature correct (valid syntax?) 2). Can count accept a reference type variable ?
View Replies !
View Related
Array Creation As Reference, Or Always Copied?
Throughout the PHP manual, and normal code, one finds this construct: $var = array( ... ); However, as far as I can tell this is quite inefficient, since it always means two arrays are being created. The right-side array is created, and then copied to the left-side variable. Thus one would expect the normal syntax should be: $var =& array( ... ); But this is not used in the manual very often. So, is there some kind of magic on the first array assignment such that it is done by reference (the first time)? Or are the vast majority of examples of array creation in the manual (and most PHP code) inefficient?
View Replies !
View Related
How To Reference Array Returned By MySql?
The code below dynamically builds hyperlinks using two queries - query A and query B. I want to optimize the code so I can omit query B and replace query A with this (let's call it query C): $link_cats = $wpdb->get_results("SELECT wp_linkcategories.cat_id, wp_linkcategories.cat_name, wp_links.link_category FROM wp_linkcategories INNER JOIN wp_links ON wp_linkcategories.cat_id = wp_links.link_category WHERE wp_links.link_category 1); Code:
View Replies !
View Related
Reference Array Inside Fucntion
I have the folllowing code snippet: PHP Code: $data_from_flash = $_POST[ "xmlStringData" ];     $nodeNames = array();         for( $i = 0; $i < strlen( $nodeNames ); $i++ )     {         fputs( $myLogFile, $nodeNames[$i] );     }     fclose( $myLogFile ); // $nodeNames cannot be accessed form inside this function     function startElementHandler( $parser, $name, $attributes )     {         array_push( $nodeNames, $name );     }
View Replies !
View Related
Sigma Notation In PHP
I searched php.net and other sites for information regarding Sigma Notation (Calculus function) and the ability to create it, but with no luck. Are there any alternate methods to pulling this off?
View Replies !
View Related
Arrays : Returning A Reference To An Array Item ??
I am currently storing a set of objects inside an array, $itemlist = array(); $itemlist[] = new item("myitem"); //... and I am looking to develop a search function, which returns a reference to the found item. function &search_item($itemlist,"myitem") { //... } I am trying to figure out how to obtain a reference to a given item in an array. Indeed, foreach() and each() seem to work on copies of the data in the array. For example: $array = array("a","b","c","d"); print_r($array); foreach($array as $v) $v="xxx"; print_r($array); // $array is unchanged reset($array) ; while(list(,$v)=each($array)) $v="xxx"; print_r($array); // $array is unchanged, again
View Replies !
View Related
Converting Scientific Notation
I have a script that generates numbers beyond the range of it, and so it's throwing them out as scientfic notation (e.g. 5E+10 instead of 50,000,000,000). The only way I've found of avoiding this is by using bc*() functions in place of normal operators (e.g. bcmul() instead of '*'). Surely there's a way to simply convert a number in scientific notation into a normal integer ?
View Replies !
View Related
Isnt ^ Common Regex Notation?
isnt ^ common regex notation? keep getting this error: Parse error: syntax error, unexpected '^' Code: $goalsheet.=preg_replace("/^[0-9]+ Year Career Goal #[0-9]+: *$", "/^<span class=hd2>[0-9]+ Year Career Goal #[0-9]+: *</span><br>$/", $goallist); aside from the error, i think the regex is off, but i hav had a hard time finding a tutorial that explains how I can ignore text in the middle. Here are some examples of what i'm trying to do: "1 Year Goal #1: Web Developer" -> "<span>1 Year Goal #1: Web Developer</span><br>" "10 Year Goal #2: IT Consultant" -> "<span>10 Year Goal #2: IT Consultant</span><br>" suggestions?
View Replies !
View Related
Reference Stays Reference Even If Passed By Value
I started porting a huge PHP application to PHP5 and encountered a problem with references. I don't know whether the behavior I see is a bug or a feature. The following nonsens test program should make clear what's the problem: class test { public $_foo = null; public function __construct($foo) { $this->_foo = $foo; } } function fct1(&$p) { print "before call to fct2(): $p->_foo "; fct2($p); print "after call to fct2(): $p->_foo "; } function fct2($p) { print "in fct2(): $p->_foo "; $p->_foo = "bla bla bla"; print "after modifying value parameter fct2(): $p->_foo "; } $t = new test("Hello World!"); fct1($t); If I evaluate this code with php v5.1.2 I get the following output: before call to fct2(): Hello World! in fct2(): Hello World! after modifying value parameter fct2(): bla bla bla after call to fct2(): bla bla bla I don't understand this: fct1() takes a parameter as reference, so fct1() should be able to change the argument I pass to it, but it does not do this. Instead it call fct2(), a function that takes a "value parameter". In my understanding, fct2() can change its argument, but changes to not affect the caller, ie. changing the parameter should not affect the variable passed to it in the callers scope. Am I wrong or is this a (known) bug? BTW: The PHP4 version works fine with php4.
View Replies !
View Related
Reference Key
I want to know how to find whether a given field in mysql table is a reference key or not. If so what is the table it is referencing to. Let me show a sample query. PHP Code:
View Replies !
View Related
Reference To Reference?
Today I discovered that I don't understand how references work in php. I create object, then I store reference to this object into array. Then I do so into another array. And suddenly I have two vars taken by reference which should point to single object, but they are different. Is there possibility of "reference to reference" in php? I'm just going crazy, can't figure out everything...
View Replies !
View Related
#reference In A URL
I've a frame witch 2 sites. One of them calls: site.php?Var1=xxx&Var2=xxx#reference The site generate a code with: <HEAD><META HTTP-EQUIV="refresh" CONTENT="15"></HEAD> and at the end of the site: <A NAME="reference"></A> . First time, it works correctly. The reference does work and I can view the last textline. But after the reload, the reference doesn't work correctly and the page displays the upperside and I must scroll-down to view the last textline. Does anyone know a solution for this problem ?
View Replies !
View Related
Row Reference
I have a Database with two text columns "title, audioFile". The title column is the title of a piece of music i.e. "Breathe". The audioFile column is the filename i.e. "Breathe_14.aiff". What I would like to happen is for the title field only to be displayed in a webpage. It should be a link to lanch a player with the appropriate audiofile i.e. click on the "Breathe" link launch a player with the "Breath_14.aiff" audio file playing.
View Replies !
View Related
Way Of Reference
I've seen this odd (just for me I think !) code in a class as constructor: Quote: function Authentication(&$db) { $this->_DB =& $db; } I know the meaning of & (value by reference)but I don't know the meaning of '=&'.
View Replies !
View Related
Reference Assignment
If I do something like $r = &$something; $r = array(); does it refer to $something or does the reference get remove first? if so(which it seems it is) can I for $r always ot point to $something? Essentially what I'm trying to do is simplfy the a variable name but I need to create it if it doesn't exist after the reference. What I have is a class that contains a variable that points to a session variable and I want to use that class variable throughout the class as a reference to the session variable but I have to create the session variable if it does exist in some cases which seems to be removing the reference and creating a new variable instead.
View Replies !
View Related
Reference Question
If I write $someobject = new SomeObject; will I have 2 instances in memory, one of which I cannot actually access? Will $someobject =& new SomeObject; remedy that, or didn't I understand it correctly?
View Replies !
View Related
Reference Passing
I have a function I use to remove all the whitespace from the beginning and end of all the $_REQUEST variables. A $_REQUEST variable may also be an array so I call it recursively. I have used as a formal parameter a referenced passed variable. So any changes to the array inside the function should also occur to the array outside the function. The function I use is shown below... //the array pointer must be reset to the start before this function is called // for the first time. It would be a safe idea to call resset($inputVariable) after last use as well. function removeWhitespaceRecursively(&$anArray){ while (list($key,$val) = each ($anArray)){ if (is_array($val)){ removeWhitespaceRecursively($val); $anArray[$key] = $val; //unknown why required. }else{ $anArray[$key] = trim($val); } } } In testing this function. It worked well for simple $_REQUEST variables that had no arrays in them but when an array was in the $_REQUEST array then the line marked "//unknown why required" was found to be needed otherwise the changes to the recursively altered array did not propagate to the original array. eg... given the input... $_REQUEST['name'] = " noel "; $_REQUEST['phone'][0] = " 1234 "; $_REQUEST['phone'][1] = " 5678 "; The output without the marked line was... $_REQUEST['name'] = "noel"; $_REQUEST['phone'][0] = " 1234 "; $_REQUEST['phone'][1] = " 5678 "; The output with the marked line was... $_REQUEST['name'] = "noel"; $_REQUEST['phone'][0] = "1234"; $_REQUEST['phone'][1] = "5678";
View Replies !
View Related
How Can I Reference ID During An INSERT?
I think I read some where on how to do this, maybe I'm wrong. I'm working on someone elses PHP code which updates an existing database - One of the programmers used to generate a unique key (a transaction code) for every purchase using a hash - A second programmer thought the idea of using the unique numeric ID field, prefixed with the two letters 'Tx' would be more sensible. He did this by first making an insert to the database with whatever transaction details he had, then getting the mysql_insert_id() and then going back and updateing the exact record he just inserted except to modify one field/column with the value he got from his mysql_insert_id() prefixed with the letters "Tx". I am pretty sure I can do this all in one go and I'd appreciate if anyone can help direct me here... basically, I want to do something like mysql> insert myTable set firstname="elvis",txcode='tx'+ID; Where ID is the ID of the new/active record being inserted. The insert works, except txcode equals zero - when the unique ID for the row is/was 2795 - and it does not even have a 'tx' prefix which leads me to believe its performing some maths to my request. Can what I want to do be done in a single step, or must I go back and update the record after the insert?
View Replies !
View Related
Variable Reference
$tester = 2800; $mine = 'seven'; $x = test; $tst=array(test => $tester,mine => $mine); echo $tst[$x]; Is it possible to make the above php code echo 2800 by changing the reference in the echo statement? Obviously I could replace $x to test, but I am trying to get what $x references. Is this even possible in php?
View Replies !
View Related
Pass By Reference In PHP?
I have a question regarding functions returning value to other functions in php. Up until now I just make everything happen in one function, but I am finding more and more that I will need to do the same thing 5 times and rather than having the same code five times in my function i figured i would just write a function for that process and pass the value back to the original location. So here is my question, how in the world do you do that in php. In C++ to pass by reference you have something like: int main() { passReference(int & value1, int & value2); return 0; } void passReference(int & value1, int & value2) { .....processes here } Is this done the same way in php? what would the basic outline of a reference function look like. Use my example above and I should be able to figure it out from there.
View Replies !
View Related
Reference Classes
I've never understood this, so if someone can explain the difference between these 2 methods it would be appreciated: PHP Code: $Object->function() Object::function() What are the differences between these and when would you use one method over the other?
View Replies !
View Related
Speed: By Reference Or By Value?
------- $a = array(............); $b = $a; ------- php says that $b points to $a until $b or $a changes. this means that the code above wants the same time with the code below: ------- $a = $a = array(............); $b = & $a; ------- but when I check this with a script, I found this: when I use & (ref) the script is faster. Code:
View Replies !
View Related
Reference Or A Copy
if i get a reference or a copy back when i try to retrieve it at page2.php $foobar = $_SESSION['myobject']; // page1.php .... $myobject = new MyObject(); $_SESSION['myobject'] = $myobject; .... // page2.php .... $foobar = $_SESSION['myobject']; ....
View Replies !
View Related
Reference Other Tables
is it possible to make a reference to table "computer_bays" within table "computer_main" and make the two tables work together? I want to create a series of tables for easier use. It may look something like this: Code:
View Replies !
View Related
Mulilanguage Reference
I have talked to few people and they said that php 4 has a function to handle multi language support. From what I heard you define a test file for each language and php grabs the text. I have try to search the web and this form and with no luck.
View Replies !
View Related
Reference Warnings Php
Hosting company (lunarpages) upgraded to php 4.4.1 and zend 2.5.10a and now code that worked flawlessly for many months is returning warnings. Specifically, stuff like $order =& Order::find($id); or other places where I return an object by reference. Here is the error: [2005-11-17 12:08:03]: Only variable references should be returned by reference in Order.php at line 334.
View Replies !
View Related
Script Reference
I have a script that I use inside a html page. I don't want the script to be part of the page....I just want to include it in the html but i don't know how to reference it: I assume that I have to sane the php code as something.php and then reference it....but i am not sure how to Code:
View Replies !
View Related
Reference Number
I'm having problem to generate the reference number for my application form. I've read the last thread, post by SoulAssassin on easy reference number.But I still can't get it. I would like to display the reference nyumber for each application (example : T0001,T0002...) I do understand the function of this mysql_query("INSERT INTO mee (product) values ('kossu')"); printf("Last inserted record has id J%04d ", mysql_insert_id()); But how this number will be insert into DB?when the user finished the form? $refNumber = sprintf('%s-%08d', date('Ymd'), mysql_insert_id()); how this $refNumber will be insert into the DB?
View Replies !
View Related
Object Or Reference
I'm just trying to find if there is some part of php that I'm not aware of that can do this. Probably the easiest way to describe this is with an example. In javascript, if you say: variable = document.getElementById('object'); variable will now equal that object and will be a reference to it, so if you say variable.value = 'something', the 'object' will now have a value of object. So variable EQUALS that object. Now if I did something like this in PHP, it just COPIES the object. Code:
View Replies !
View Related
Pass By Reference NOT
I have a huge project and way down in the chain of class->fucntions->functions..... etc I have a call to a function with 6 variables, the last 2 are passed by reference. now i know this has been working and i have no idea why its stopped. Now i can debug out the calculated data before the function exit and it looks fine, but back in the calling program the fields are empty?!? Code:
View Replies !
View Related
Assigned By Reference
I like to program in PHP with the errors as tight as possible. One of my newer errors or should I say "variation from standards" is. Strict Standards: Only variables should be assigned by reference in C:lahlahsomething.php on line 20 Now I understand that it is talking about the part of my code that looks like (note the &). $this->controls =& $this->commonEntityData->getControls();
View Replies !
View Related
Compare By Reference
I'm trying to check for recursive dependencies and of course if I have one, PHP dies with the error "Fatal error: Nesting level too deep recursive dependency I've done a bit of extension coding, so I know it would be fairly easy to write a function that compares the zvals directly. However, I really don't want to go the way of a custom extension just for this, mainly for portability reasons: I'm not even sure I can install a custom extension on the server I'm working on just now, nevermind all the messing about getting it compiled against the right source tree.
View Replies !
View Related
Pass By Reference Question
I am creating a program to create sudoku games. I have an object that is 1 row of a sudoku game. I store the initial state of the row before I start figuring out the values to place in each cell, in case the program runs into a problem and has to revert the row back to its original state. So first I store the initial State of the row, then I start to modify the row. My problem is that once I start to change the original row, it also changes the initial state that I saved. The only thing I can think that is doing this is that instead of creating a copy of the object's data in my initial state variable, it is just changing its pointer to the original rows location. I wrote a smaller test case to show what I am talking about. At the end of the program, each cell in the initial state should be full of numbers 1 to 9. Instead, it is a copy of the current state of the row.<?php class Cell { public $c = array(1,2,3,4,5,6,7,8,9); public function getCandidates() { return $this->c; } public function pickRandomCandidate() { return $this->c[rand() % count($this->c)]; } public function setCandidate($a) { $this->c = array($a); } public function removeCandidateFromCell($v) { $this->c = array_values(array_diff($this->c,array($v))); } } class Row { public $cells = array(); public $initialState = array(); public function __construct() { for ($i=0;$i<9;$i++) $this->cells[$i] = new Cell(); } public function getInitialState() { return $this->initialState; } public function getCurrentState() { return $this->cells; } public function setInitialState() { $this->initialState = $this->getCurrentState(); } public function printRow() { echo "<strong>Printing Row State</strong>"; echo "<table border=1 cellpadding=4>"; echo "<tr>"; foreach($this->cells as $cell) { echo "<td align='center'>"; for($i=1; $i <= 9; $i++) { echo (in_array($i, $cell->c)) ? $i : "<span style='color: #ddd'>$i</span>"; echo $i==3 || $i==6 ? "<br />" : ""; } echo "</td>"; } echo "</tr>"; echo"</table>"; } public function printInitialState($rowNum = "") { echo "<strong>Initial State of $rowNum</strong>"; echo "<table border=1 cellpadding=4>"; echo "<tr>"; foreach($this->initialState as $cell) { echo "<td align='center'>"; for($i=1; $i <= 9; $i++) { echo (in_array($i, $cell->c)) ? $i : "<span style='color: #ddd'>$i</span>"; echo $i==3 || $i==6 ? "<br />" : ""; } echo "</td>"; } echo "</tr>"; echo"</table>"; } public function resetRow($rowNum = "") { echo "resetting row $rowNum <br />"; $this->cells = $this->getInitialState(); #$this->cells = $this->initialState; } public function removeCandidateFromRow($v) { foreach($this->cells as $c =$val) { $val->removeCandidateFromCell($v); } } } $myrow = new Row(); $myrow->setInitialState(); $candidate = $myrow->cells[0]->pickRandomCandidate(); $myrow ->removeCandidateFromRow($candidate); $myrow->cells[0]->setCandidate($candidate); $myrow->printRow(); $myrow->printInitialState(); ?>
View Replies !
View Related
Returning A Null Reference In PHP 4.4.2
Consider a class that encapsulates an associative array. It has two "setter" methods: class A { .... function setAttribute( $name, $value ) { $this->attributes[ $name ] = $value; } .... function setAttributeByRef( $name, &$value ) { $this->attributes[ $name ] =& $value; } .... The following "getter" method *used* to work just fine prior to version 4.4.2: .... function & getAttribute( $name ) { if ( isset( $this->attributes[ $name ] ) ) return $this->attributes[ $name ]; return NULL; } .... However, as of version 4.4.2, this throws a "FATAL: Only variable references should be returned by reference." error. Unfortunately, my host (and several of my client hosts) are stuck on this version. Is there an elegant way to solve this problem? I want to return a value indicating that no such attribute exists. (I've googled the problem and can't find anything useful other than a suggestion in a PHP bug report that I learn how to write "proper" code.)
View Replies !
View Related
Aliasing And Reference Confusion....
I hope that someone can set me straight on "alaising" and "references" in PHP.... I'm a C coder from ways back and this "new fangled" PHP way of doing things is hurting my brain. :-) OK, to set the background: I have an XML document. I am searching it using xpath. So far so good. Now I need to replace a node value. In PHP4 and libxml 2.4, the node->set_value method doesn't actually set the value; it appends it to the node value. So I've built a function setXpathNodeValue: function setXpathNodeValue($context, $pattern, $snode, &$new_content ) { if ($snode == '') $nodeset = xpath_eval($context, $pattern); else $nodeset = xpath_eval($context, $pattern, $snode); $arNodes = $nodeset->nodeset; $node = $arNodes[0]; if ($node == NULL) return NULL; $dom = $node->owner_document(); $newnode = $dom->create_element($node->tagname ); $newnode->set_content($new_content ); $atts = $node->attributes(); foreach ($atts as $att ) { $newnode->set_attribute($att->name, $att->value ); } $kids = & $node->child_nodes(); foreach ($kids as $kid ) { if ($kid->node_type() != XML_TEXT_NODE ) { $newnode->append_child($kid ); } } $node->replace_node($newnode ); } (Yes, this comes almost verbatim from the PHP.net docs.) Except that I can't figure out how to "pass by reference" the $dom so that it is actually changed in the original. That's the whole point of this exercise. I guess I could just copy over the whole $dom, but that's potentially enormous....
View Replies !
View Related
Passing A Result Set By Reference
I have a question regarding passing a result from a database query to a function as a reference. I basically want to know if passing by reference in PHP 4* is quicker than not passing by reference.I have tested some, but it will still be a bit before I'm ready to test with enough data to matter, and I also have noticed that some people have offered differing opinions regarding this. PHP Code:
View Replies !
View Related
Return Reference In This Case?
a simple singleton class (PHP4) which way is preffered? // 1. class Foo { function getFoo() { static $instace; if (!isset($instace) ) { $instance = new Foo(); // ... } return $instance; } } $foo = Foo::getFoo(); // 2. class Foo { function &getFoo() { static $instace; if (!isset($instace) ) { $instance = new Foo(); // ... } return $instance; } } $foo =& Foo::getFoo();
View Replies !
View Related
Why Can't I Reference A Member Variable?
object-oriented PHP, but no stranger to OOP in general. I've spent the last three hours trying to figure out why this code snipped doesn't do what I expect it to: ---snip--- <?php $myTest = new Test; echo $myTest->GetClassName; // Nada class Test { // Class constructor function Test() { // Empty on purpose... } // Returns class name function GetClassName() { return "Test"; } } ?> ---snip--- I would expect the "echo ..." line to display "Test", but it doesn't. I swear I must be missing something really simple and really stupid, but I can't find it for the life of me. Little help? Oh, PHP Version 4.3.4 if it makes a difference.
View Replies !
View Related
Return A Reference Of An Object
I was wondering if there was a way to return a reference of an object that lies within another object... <?php class my_class_1{ var $_db=NULL; function my_class_1(){ $this->_db=new my_class_2(TRUE); return TRUE; } function get_db(&$db){ $db=&$this->db; return TRUE; } } class my_class_2{ var $var1=NULL; function my_class_2($var){ $this->var1=$var; return TRUE; } } $object=new my_class_1(); $object->get_db($db); var_dump($db); ?> This gives: NULL I was hoping for: object(my_class_2)(1) { ["var1"]=> bool(true) } Anyone know of a way to do this, or can you only return a copy instead of the reference?
View Replies !
View Related
|