Re: [PHP] array_push
Frank Arensmeier wrote: 2 sep 2012 kl. 19.48 skrev John Taylor-Johnston: Why not add two lines of code within the first loop? $chunks = explode("-30-", $mystring); foreach($chunks as $chunk) { preg_match_all("/News Releases\n(.+)/s", $chunk, $matches); foreach($matches[1] as $matched_text_line) { $mynewarray[] = $matched_text_line; } } Besides the regex, this is pretty basic php. /frank Thanks! I'm still a newbie, (10 years later). I'm still fuzzy when it comes to arrays. John -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push
2 sep 2012 kl. 19.48 skrev John Taylor-Johnston: > How can I clean this up? >>> My approach would be to split the hole text into smaller chunks (with e.g. >>> explode()) and extract the interesting parts with a regular expression. >>> Maybe this will give you some ideas: $chunks = explode("-30-", $mystring); foreach($chunks as $chunk) { preg_match_all("/News Releases\n(.+)/s", $chunk, $matches); var_dump($matches[1]); } The regex matches all text between "News Releases" and the end of the chunk. >> 2) How could I suck it into one nice easy to handle array? >> >> |$mynewarray=|array { >> [0]=> "Residential Fire Determined to be Accidental in Nature ..." >> [1]=> "Arrest Made in Residential Fire ..." >> } > I was hoping preg_match_all would return strings. I w/as hoping |$matches[1] > was a string.|/ > > source: http://www.cegepsherbrooke.qc.ca/~languesmodernes/test/test4.phps > result: http://www.cegepsherbrooke.qc.ca/~languesmodernes/test/test4.php Have you read up on 'preg_match_all' in the manual? What makes you think that preg_match_all returns strings? "$matches[1]", in the above case contains an array with all matches from the parenthesized subpattern, which is "(.+). > This is ugly. How can I clean this up like this? > > $mynewarray= array { > [0]=> "Residential Fire Determined to be Accidental in Nature ..." > [1]=> "Arrest Made in Residential Fire ..." > } Why not add two lines of code within the first loop? $chunks = explode("-30-", $mystring); foreach($chunks as $chunk) { preg_match_all("/News Releases\n(.+)/s", $chunk, $matches); foreach($matches[1] as $matched_text_line) { $mynewarray[] = $matched_text_line; } } Besides the regex, this is pretty basic php. /frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_push
How can I clean this up? My approach would be to split the hole text into smaller chunks (with e.g. explode()) and extract the interesting parts with a regular expression. Maybe this will give you some ideas: $chunks = explode("-30-", $mystring); foreach($chunks as $chunk) { preg_match_all("/News Releases\n(.+)/s", $chunk, $matches); var_dump($matches[1]); } The regex matches all text between "News Releases" and the end of the chunk. 2) How could I suck it into one nice easy to handle array? |$mynewarray=|array { [0]=> "Residential Fire Determined to be Accidental in Nature ..." [1]=> "Arrest Made in Residential Fire ..." } I was hoping preg_match_all would return strings. I w/as hoping |$matches[1] was a string.|/ source: http://www.cegepsherbrooke.qc.ca/~languesmodernes/test/test4.phps result: http://www.cegepsherbrooke.qc.ca/~languesmodernes/test/test4.php This is ugly. How can I clean this up like this? $mynewarray= array { [0]=> "Residential Fire Determined to be Accidental in Nature ..." [1]=> "Arrest Made in Residential Fire ..." } I wanted to push $matches[] (which I hoped was a string, not an array). I probably don't understand what preg_match_all did. Now I have arrays nested inside the array(). How can I clean it up? http://www.cegepsherbrooke.qc.ca/~languesmodernes/test/test4.php
Re: [PHP] array_push in array maps
On 5/3/06, Jochem Maas <[EMAIL PROTECTED]> wrote: Jonas Said: >> But when I want to have a value from a special column i a row the >> followint >> doesn't work: >> >>$test[$row[2]] = $row[5]; >> >> Or: >> >>$test[$row(2)] = $row[5]; >> >> Do I need to do some kind of concatenating? >> Ah, Lasso! I coded that in my previous job. Left it to commit to the world of PHP, but I saw lots of potential in Lasso 8 (we were in 3/4/5). Sort of miss it sometimes...*sniff* Anyway, all of the comments are in the right direction. A couple things to fill in the gaps: PHP Arrays are indeed a "mash-up" like Jochem said. If you want to get at an array element using a numerical key, then simply use the number without any quotes, like so: echo $array[2]; However if you want to get at an array element using a string key, then it must be surrounded in quotes (single or double, but see below for more): echo $array['my_key']; Now, single quotes and double quotes are slightly (and also very) different in PHP. Read the manual for the details (http://uk.php.net/manual/en/language.types.string.php), but one thing to know is that the $ sign is a literal $ sign when it is in a string surrounded by single quotes; but surround it with double quotes, and it will help you create a variable (assuming you follow it with legal variable naming characters). So if you have a variable: $lang = 'Lasso'; And try to print it in a string with single quotes, it won't work: echo 'I used to program in $lang'; // this prints 'I used to program in $lang' But around double quotes, all is well: echo "I used to program in $lang"; // this prints 'I used to program in Lasso' And if you have a complex variable within a string and you want to "help" PHP figure out what's a variable and what's not, use { } to frame the variable name: echo "Look at my class {$my_class->get('name')} and my array {$my_array['key']} !"; Make sense? The manual explains a lot more, so be sure to read up. Also, unless Lasso changed in 8+, there's one other thing to remember about arrays. In PHP, arrays will keep their order of elements as you have created it. IIRC, Lasso never guaranteed this, so you always had to take extra measure to build your arrays such that you could iterate through them in a reliable fashion. No worries in PHP, the order sticks (and you can of course re-order it). HTH, Good luck, John W -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push in array maps
Jonas - what do you mean 'it doesn't work' (it's like me saying 'the ATM doesn't work and forgetting to mention I'm trying to withdraw 10,000,000 disney dollars - when I mention that fact the reason is probably obvious to you why it doesn't work) Brad Bonkoski wrote: What kind of values are stored in $row[2] and $row[5]? You might need to keep the single quotes $test['$row[2]'] = $row[5]; no this is wrong, unless you want the array key in this case to be the *exact* *literal* string '$row[2]' - which is unlikely. Jonas do the following (in the relevant place in your code) and see what is in $row: var_dump($row); -Brad Jonas Rosling wrote: There's allways mutch to learn. :-) I'm very happy for all help I can get. I ran into another problem when trying to insert a value. I had no problem with: $test['something'] = '2500'; But when I want to have a value from a special column i a row the followint doesn't work: $test[$row[2]] = $row[5]; Or: $test[$row(2)] = $row[5]; Do I need to do some kind of concatenating? Thanks again // Jonas Den 06-05-03 15.46, skrev "Jochem Maas" <[EMAIL PROTECTED]>: Jonas Rosling wrote: Need solve another case regarding array maps. Is it possible to insert more Jonas - in php we just call these things arrays (no 'map') - the array datatype in php is a mash up (and we love it :-) of the 'oldschool' numerically indexed array and what is commonly known as a hash (or arraymap) - you can mix numeric and associative indexes freely (that might seem odd and/or bad when coming from another language but it really is fantastic once you get your head round it). values like with array_push in arrays as bellow? $colors = array( 'red' => '#ff', 'green' => 'X00ff00', 'blue' => '#ff' ); sure: $colors['grey'] = '#dedede'; the position of the 'inserted' (actually appended) is often not important - if it is there are plenty of functions that allow you to manipulate arrays in more a complex fashion e.g. array_splice(). and all sorts of sorting functions are also available e.g. asort(). check out the vast number of array related function and introductory texts here: http://php.net/array http://php.net/manual/language.types.array.php Tanks in advance // Jonas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push in array maps
What kind of values are stored in $row[2] and $row[5]? You might need to keep the single quotes $test['$row[2]'] = $row[5]; -Brad Jonas Rosling wrote: There's allways mutch to learn. :-) I'm very happy for all help I can get. I ran into another problem when trying to insert a value. I had no problem with: $test['something'] = '2500'; But when I want to have a value from a special column i a row the followint doesn't work: $test[$row[2]] = $row[5]; Or: $test[$row(2)] = $row[5]; Do I need to do some kind of concatenating? Thanks again // Jonas Den 06-05-03 15.46, skrev "Jochem Maas" <[EMAIL PROTECTED]>: Jonas Rosling wrote: Need solve another case regarding array maps. Is it possible to insert more Jonas - in php we just call these things arrays (no 'map') - the array datatype in php is a mash up (and we love it :-) of the 'oldschool' numerically indexed array and what is commonly known as a hash (or arraymap) - you can mix numeric and associative indexes freely (that might seem odd and/or bad when coming from another language but it really is fantastic once you get your head round it). values like with array_push in arrays as bellow? $colors = array( 'red' => '#ff', 'green' => 'X00ff00', 'blue' => '#ff' ); sure: $colors['grey'] = '#dedede'; the position of the 'inserted' (actually appended) is often not important - if it is there are plenty of functions that allow you to manipulate arrays in more a complex fashion e.g. array_splice(). and all sorts of sorting functions are also available e.g. asort(). check out the vast number of array related function and introductory texts here: http://php.net/array http://php.net/manual/language.types.array.php Tanks in advance // Jonas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push in array maps
There's allways mutch to learn. :-) I'm very happy for all help I can get. I ran into another problem when trying to insert a value. I had no problem with: $test['something'] = '2500'; But when I want to have a value from a special column i a row the followint doesn't work: $test[$row[2]] = $row[5]; Or: $test[$row(2)] = $row[5]; Do I need to do some kind of concatenating? Thanks again // Jonas Den 06-05-03 15.46, skrev "Jochem Maas" <[EMAIL PROTECTED]>: > Jonas Rosling wrote: >> Need solve another case regarding array maps. Is it possible to insert more > > Jonas - in php we just call these things arrays (no 'map') - the array > datatype in php is a mash up (and we love it :-) of the 'oldschool' > numerically > indexed array and what is commonly known as a hash (or arraymap) - you can mix > numeric and associative indexes freely (that might seem odd and/or bad when > coming > from another language but it really is fantastic once you get your head round > it). > >> values like with array_push in arrays as bellow? >> >> $colors = array( >> 'red' => '#ff', >> 'green' => 'X00ff00', >> 'blue' => '#ff' >>); >> > > sure: > > $colors['grey'] = '#dedede'; > > the position of the 'inserted' (actually appended) is often not > important - if it is there are plenty of functions that allow you to > manipulate > arrays in more a complex fashion e.g. array_splice(). and all sorts of sorting > functions are also available e.g. asort(). > > check out the vast number of array related function and introductory texts > here: > > http://php.net/array > http://php.net/manual/language.types.array.php > >> Tanks in advance // Jonas >> > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push in array maps
Jonas Rosling wrote: Need solve another case regarding array maps. Is it possible to insert more Jonas - in php we just call these things arrays (no 'map') - the array datatype in php is a mash up (and we love it :-) of the 'oldschool' numerically indexed array and what is commonly known as a hash (or arraymap) - you can mix numeric and associative indexes freely (that might seem odd and/or bad when coming from another language but it really is fantastic once you get your head round it). values like with array_push in arrays as bellow? $colors = array( 'red' => '#ff', 'green' => 'X00ff00', 'blue' => '#ff' ); sure: $colors['grey'] = '#dedede'; the position of the 'inserted' (actually appended) is often not important - if it is there are plenty of functions that allow you to manipulate arrays in more a complex fashion e.g. array_splice(). and all sorts of sorting functions are also available e.g. asort(). check out the vast number of array related function and introductory texts here: http://php.net/array http://php.net/manual/language.types.array.php Tanks in advance // Jonas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push in array maps
Stut wrote: Brad Bonkoski wrote: $colors['black'] = '#ff'; Black? Are you sure? Brad might be wearing his 'negative' goggles today :-) -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] array_push in array maps
[snip] Need solve another case regarding array maps. Is it possible to insert more values like with array_push in arrays as bellow? $colors = array( 'red' => '#ff', 'green' => 'X00ff00', 'blue' => '#ff' ); [/snip] // Append associative array elements function array_push_associative(&$arr) { $args = func_get_args(); array_unshift($args); // remove &$arr argument foreach ($args as $arg) { if (is_array($arg)) { foreach ($arg as $key => $value) { $arr[$key] = $value; $ret++; } } } return $ret; } $theArray = array(); echo array_push_associative($theArray, $items, $moreitems) . ' items added to $theArray.'; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push in array maps
or white ;-) Stut wrote: Brad Bonkoski wrote: $colors['black'] = '#ff'; Black? Are you sure? -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push in array maps
Brad Bonkoski wrote: $colors['black'] = '#ff'; Black? Are you sure? -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push in array maps
Jonas Rosling wrote: Need solve another case regarding array maps. Is it possible to insert more values like with array_push in arrays as bellow? $colors = array( 'red' => '#ff', 'green' => 'X00ff00', 'blue' => '#ff' ); Did you try it? It will work, but there is no need for array_push. All you need is... $colors['yellow'] = '#00'; -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push in array maps
Need solve another case regarding array maps. Is it possible to insert more values like with array_push in arrays as bellow? $colors = array( 'red' => '#ff', 'green' => 'X00ff00', 'blue' => '#ff' ); Yes. http://www.php.net/array_push -- Postgresql & php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push in array maps
I don't believe you 'push' to an associative array like this, but if you want to add black for example...just do: $colors['black'] = '#ff'; -Brad Jonas Rosling wrote: Need solve another case regarding array maps. Is it possible to insert more values like with array_push in arrays as bellow? $colors = array( 'red' => '#ff', 'green' => 'X00ff00', 'blue' => '#ff' ); Tanks in advance // Jonas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_push in array maps
Need solve another case regarding array maps. Is it possible to insert more values like with array_push in arrays as bellow? $colors = array( 'red' => '#ff', 'green' => 'X00ff00', 'blue' => '#ff' ); Tanks in advance // Jonas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] array_push
On Fri, 21 May 2004, Edward Peloke wrote: > How do I then cycle through this array to build the select box? I > currently use this which works fine when I hard code the values..ie. > $yesno=array("Yes"=>"1","No"=>"0") but doesn't seem to work using your > first option. > > Here is the form select box code > while($value=each($array)) > { > $res .= " ? " selected=\"selected\"" : '') . " > value=".$value['value'].">".$value['key']."\n"; > } > I'm not sure about what you mean, cause these variables have other names than before :-) But what I think you mean is this: foreach( $clients as $id => $name ) echo "{$name}"; Correct me if this isn't what you meant. -- Jeroen Like the creators of sitcoms or junk food or package tours, Java's designers were consciously designing a product for people not as smart as them. -- Paul Graham -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] array_push
How do I then cycle through this array to build the select box? I currently use this which works fine when I hard code the values..ie. $yesno=array("Yes"=>"1","No"=>"0") but doesn't seem to work using your first option. Here is the form select box code while($value=each($array)) { $res .= "".$value['key']."\n"; } Thanks, Eddie -Original Message- From: Jeroen Serpieters [mailto:[EMAIL PROTECTED] Sent: Friday, May 21, 2004 12:16 AM To: Edward Peloke Cc: 'php-general' Subject: Re: [PHP] array_push On Fri, 21 May 2004, Edward Peloke wrote: > > $sql=new Database(); > $sql->query("select fname,lname, id from clients"); > $clients[]=array(); > while($sql->nextRecord()){ >array_push($clients, $sql->getField('id')=>$sql->getField('fname')); > } // while > > Parse error: parse error, unexpected T_DOUBLE_ARROW in > C:\obox\Apache2\htdocs\nh_vacdest\admin\test.php on line 38 > If you want to do it the way you're doing now you should do ik like this: array_push($clients, array($sql->getField('id')=>$sql->getField('fname'))); Another possible solution is like this: $clients[$sql->getField('id')] = $sql->getField('fname'); -- Jeroen There are only two kinds of programming languages: those people always bitch about and those nobody uses. -- Bjarne Stroustrup -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push
On Fri, 21 May 2004, Edward Peloke wrote: > > $clients[]=array(); > Here you should probably use $clients = array(); -- Jeroen Anybody who thinks a little 9,000-line program [Java] that's distributed free and can be cloned by anyone is going to affect anything we do at Microsoft has his head screwed on wrong. -- Bill Gates -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push
On Fri, 21 May 2004, Edward Peloke wrote: > > $sql=new Database(); > $sql->query("select fname,lname, id from clients"); > $clients[]=array(); > while($sql->nextRecord()){ >array_push($clients, $sql->getField('id')=>$sql->getField('fname')); > } // while > > Parse error: parse error, unexpected T_DOUBLE_ARROW in > C:\obox\Apache2\htdocs\nh_vacdest\admin\test.php on line 38 > If you want to do it the way you're doing now you should do ik like this: array_push($clients, array($sql->getField('id')=>$sql->getField('fname'))); Another possible solution is like this: $clients[$sql->getField('id')] = $sql->getField('fname'); -- Jeroen There are only two kinds of programming languages: those people always bitch about and those nobody uses. -- Bjarne Stroustrup -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array_push
Ok, I am having a brain cramp for some reason but I need to pass in an array to my form class to build select boxes. The array is ("id"=>"John Doe"), I need to dynamically build this array from a query but I am getting errors using array_push.do I need to use array_merge? Example: $sql=new Database(); $sql->query("select fname,lname, id from clients"); $clients[]=array(); while($sql->nextRecord()){ array_push($clients, $sql->getField('id')=>$sql->getField('fname')); } // while Parse error: parse error, unexpected T_DOUBLE_ARROW in C:\obox\Apache2\htdocs\nh_vacdest\admin\test.php on line 38 How can I dynamically create my array? Thanks, Eddie
[PHP] array_push
Hello, why this array_push($x ? $a : $b, 'value'); produce error(Fatal error: Only variables can be passed by reference) http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] ARRAY_PUSH with $key
On Fri, Jul 19, 2002 at 01:58:42AM +0300, Pekka Saarinen wrote: > > I think array_push gets you cleaner lookin code (subjective). It also > ensures you always add to end of the array - good for novices like > me. Array_push is also TWO times faster (academic difference in the speeds > of modern computers, but faster :) : AH! But your test is flawed. Reorder things so the push is first and that'll be slower. In addition, push() is more like setting $Array[] rather than $Array[$n]. So, here's my test, including some arithmetic calculation changes: =1;$n++) { $Array[$n] = $n; } $stop = getMicrotime(); echo 'key num: ' . (bcsub($stop, $start, 6)) . "\n"; $Array = array(); $start = getMicrotime(); for ($n=0; $n>=1; $n++) { array_push($Array, $n); } $stop = getMicrotime(); echo 'push: ' . (bcsub($stop, $start, 6)) . "\n"; $Array = array(); $start = getMicrotime(); for ($n=0; $n>=1; $n++) { $Array[] = $n; } $stop = getMicrotime(); echo 'key blank: ' . (bcsub($stop, $start, 6)) . "\n"; # ROUND 2 $Array = array(); $start = getMicrotime(); for ($n=0;$n>=1;$n++) { $Array[$n] = $n; } $stop = getMicrotime(); echo 'key num: ' . (bcsub($stop, $start, 6)) . "\n"; $Array = array(); $start = getMicrotime(); for ($n=0; $n>=1; $n++) { array_push($Array, $n); } $stop = getMicrotime(); echo 'push: ' . (bcsub($stop, $start, 6)) . "\n"; $Array = array(); $start = getMicrotime(); for ($n=0; $n>=1; $n++) { $Array[] = $n; } $stop = getMicrotime(); echo 'key blank: ' . (bcsub($stop, $start, 6)) . "\n"; ?> Results... key num: 0.000301 push: 0.000145 key blank: 0.000135 key num: 0.000136 push: 0.000135 key blank: 0.000135 The times compared to each other varied each time I reran the test. --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] ARRAY_PUSH with $key
At 7/18/2002, you wrote: >On Thu, Jul 18, 2002 at 03:40:58PM -0400, Joshua E Minnie wrote: > > Hey all, > > Does anybody know of a way to push an element onto an array with a > > specific key? What I am trying to do is create a dynamically created > > associative array. Here is a sample print_r of what an array would look > > like: > >I don't understand the value of array_push(). I just add new elements by >something like: > >$Array[$Key] = $Value; I think array_push gets you cleaner lookin code (subjective). It also ensures you always add to end of the array - good for novices like me. Array_push is also TWO times faster (academic difference in the speeds of modern computers, but faster :) : =1;$n++) { $Array[$n] = $n; } $stop = getMicrotime(); $diff1 = $stop - $start; print "array key insert time was: " . $diff1 . "s"; $start = getMicrotime(); $array2 = array(); for ($n=0;$n>=1;$n++) { array_push($array2,$n); } $stop = getMicrotime(); $diff2 = $stop - $start; print "array_push time was: " . $diff2 . "s"; ?> - Pekka Saarinen http://photography-on-the.net - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] ARRAY_PUSH with $key
On Thu, Jul 18, 2002 at 03:40:58PM -0400, Joshua E Minnie wrote: > Hey all, > Does anybody know of a way to push an element onto an array with a > specific key? What I am trying to do is create a dynamically created > associative array. Here is a sample print_r of what an array would look > like: I don't understand the value of array_push(). I just add new elements by something like: $Array[$Key] = $Value; --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] ARRAY_PUSH with $key
Hey all, Does anybody know of a way to push an element onto an array with a specific key? What I am trying to do is create a dynamically created associative array. Here is a sample print_r of what an array would look like: Array ( [default] => css/default.css [forms] => css/forms.css ) I have tried array_push, but can't seem to get the results I am looking for. i.e.: "$value"); // this throws an error array_push($samp, array("$key"=>"$value")); // doesn't give the results i'm looking for /* output from second array_push Array ( [0] => Array ( [default] => css/default.css ) [1] => Array ( [forms] => css/forms.css ) )*/ ?> If anyone can point me in the right direction, I would appreciate any help you can give me. -- Joshua E Minnie/Webmaster [EMAIL PROTECTED] Phone: 616.276.9690 Fax: 616.342.8750 Nextel: 616.862.2847 "Don't work for recognition, but always do work worthy of recognition." -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] ARRAY_PUSH()
Chris: Why'd you start a second thread on this? See my reply in the "File reading help with Syntax" thread. --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] ARRAY_PUSH()
I am getting data froma website for stock information. If I type in the brower the URL I get a text file display list this; Date,Open,High,Low,Close,Volume 16-Jul-02,7.92,8.10,7.68,7.82,605500 15-Jul-02,7.98,8.02,7.59,8.02,577200 12-Jul-02,7.80,8.00,7.57,7.95,411100 11-Jul-02,7.82,7.94,7.34,7.80,802400 Now I want to break each line and then seperate each line by the commas. The amount of linesin the file is never known so I assume I have to use something like a foreach or while statement,but I am not sure the best way to do it. This is what I have so far. $Symbol = "IKN"; $LookupUrl = "http://demos.inxdesign.com/download?sym=$Symbol&format=.txt";; $Results = implode('', file("$LookupUrl")); $Data = array(); split("\n", $Results) = array_push($Data, $line) The end result I am trying to get is each line to be an element in an array. Later I will go back and stepthrough each element of the array and then split that by the commas and have it output into an HTML table.At least this is the best way I can think to deal with it. I suppose a better way to do this would be to make this an associative array and have the data of each line be associated with the date then I could producea variable something like $StockData[16-Jul-02][value], but I don't know how to do any of that. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array_push but with key, value pairs
Dean Hall wrote > Instead of 'array_push', do this: > > $input[$key] = $val; > Hey thanks dean that did the trick, I was trying: $array[] = $key . "=>" . $val which of course doesn't work hehe. Regards Joseph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] array_push but with key, value pairs
Joseph. "Joseph Blythe" <[EMAIL PROTECTED]> wrote: ... > Ok fine, I now have an associative array with a numeric index, but > hangon I wanted to push the key => value into the array. How would I do > this, I have tried a couple of things but am having some really crazy > results ;-) 'array_push' is only useful for numerically-indexed arrays. It's basically a shortcut for getting the length of the array and putting a value in the last available spot in an array. It treats the array like a stack. In an associative array (or hash), the array is indexed by strings, so the order you input them in is irrelevant (for the most part). I think in PHP you can actually retrieve the values in the same order as you put them in, but this does not have to be the case. Hash keys should be arbitrarily ordered. > $input = array(); > while ( list($key, $val) = each($HTTP_POST_VARS) ) { > if ( $key != "Submit" ) { > array_push($input, $val); > } > } Instead of 'array_push', do this: $input[$key] = $val; Dean http://hall.apt7.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] array_push but with key, value pairs
Peter Houchin wrote: > what about if you assign the key to = the id (if in a data base)? > > maybe like > > while (list ($key, $val) = each ($id)) { > if ( $key != "Submit" ) { > >>array_push($input, $val); >>} >> >> } > I am not using a database for this part as I am just collecting the data to validate. I really want to know how to create a new array in a while loop, which contains the key and value pairs of the array passed to each. So lets say the above code would output: 0 => value1 1 => value2 2 => value 3 4 = value4 But I want to create: keyname1 => value1 keyname2 => value2 keyname3 => value3 keyname4 => value4 Does anyone understand what I am on about? Regards, Joseph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] array_push but with key, value pairs
what about if you assign the key to = the id (if in a data base)? maybe like while (list ($key, $val) = each ($id)) { if ( $key != "Submit" ) { >array_push($input, $val); >} > > } -Original Message- From: Joseph Blythe [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 17, 2001 3:40 PM To: Peter Houchin Cc: [EMAIL PROTECTED] Subject: Re: [PHP] array_push but with key, value pairs Peter Houchin wrote: > maybe tring this ... > $input = array(); > > reset ($HTTP_POST_VARS); > > while ( list($key, $val) = each($HTTP_POST_VARS) ) { >if ( $key != "Submit" ) { >array_push($input, $val); >} > > } Hmm, as far as I can see this will just reset the internal pointer back to the start of the HTTP_POST_VAR array, I suppose this should be done in case I have called 'each' on the array before, but it does not answer my question being: I want to push the key and value into an array not just the value. I basically need array_push functionality but with the ablility to use your own index values. I hope this is more clear. Regards Joseph
Re: [PHP] array_push but with key, value pairs
Peter Houchin wrote: > maybe tring this ... > $input = array(); > > reset ($HTTP_POST_VARS); > > while ( list($key, $val) = each($HTTP_POST_VARS) ) { >if ( $key != "Submit" ) { >array_push($input, $val); >} > > } Hmm, as far as I can see this will just reset the internal pointer back to the start of the HTTP_POST_VAR array, I suppose this should be done in case I have called 'each' on the array before, but it does not answer my question being: I want to push the key and value into an array not just the value. I basically need array_push functionality but with the ablility to use your own index values. I hope this is more clear. Regards Joseph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] array_push but with key, value pairs
maybe tring this ... $input = array(); reset ($HTTP_POST_VARS); while ( list($key, $val) = each($HTTP_POST_VARS) ) { if ( $key != "Submit" ) { array_push($input, $val); } } Peter -Original Message- From: Joseph Blythe [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 17, 2001 3:13 PM To: [EMAIL PROTECTED] Subject: [PHP] array_push but with key, value pairs Hey all, I was just trying to figure out something that should be quite simple but seems to be eluding me, consider the following: $input = array(); while ( list($key, $val) = each($HTTP_POST_VARS) ) { if ( $key != "Submit" ) { array_push($input, $val); } } Ok fine, I now have an associative array with a numeric index, but hangon I wanted to push the key => value into the array. How would I do this, I have tried a couple of things but am having some really crazy results ;-) What I am really trying to do is create a new array from the HTTP_POST_VARS while excluding certain fields, like Submit or the X and Y values from a image button. Any ideas/suggestions are very welcomed, Regards Joseph, -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] array_push but with key, value pairs
Hey all, I was just trying to figure out something that should be quite simple but seems to be eluding me, consider the following: $input = array(); while ( list($key, $val) = each($HTTP_POST_VARS) ) { if ( $key != "Submit" ) { array_push($input, $val); } } Ok fine, I now have an associative array with a numeric index, but hangon I wanted to push the key => value into the array. How would I do this, I have tried a couple of things but am having some really crazy results ;-) What I am really trying to do is create a new array from the HTTP_POST_VARS while excluding certain fields, like Submit or the X and Y values from a image button. Any ideas/suggestions are very welcomed, Regards Joseph, -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]