RE: [PHP] Problem with arrays
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Hi, I have 2 arrays: Array ( [0] = Array ( [0] = 28 [1] = Music ) [1] = Array ( [0] = 5 [1] = Books ) ) and Array ( [0] = aaa [1] = bbb ) I want to join this two array and the result must be loke this: Array ( [0] = Array ( [0] = 28 [1] = Music [2] = aaa ) [1] = Array ( [0] = 5 [1] = Books [2] = bbb ) ) Thanks in advance for your help In this specific example, I think this would work: ? for ($i = 0; $i count($second_array); $i++) { array_push($first_array[$i], $second_array[$i]); } ? That's not terribly flexible, though. Is this used in a more generalized sense, or is it just this specific instance? -- Mike Johnson Smarter Living, Inc. Web Developerwww.smartertravel.com [EMAIL PROTECTED] (617) 886-5539 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with arrays
Mike Johnson wrote: From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Hi, I have 2 arrays: Array ( [0] = Array ( [0] = 28 [1] = Music ) [1] = Array ( [0] = 5 [1] = Books ) ) and Array ( [0] = aaa [1] = bbb ) I want to join this two array and the result must be loke this: Array ( [0] = Array ( [0] = 28 [1] = Music [2] = aaa ) [1] = Array ( [0] = 5 [1] = Books [2] = bbb ) ) Thanks in advance for your help In this specific example, I think this would work: ? for ($i = 0; $i count($second_array); $i++) { array_push($first_array[$i], $second_array[$i]); } ? That's not terribly flexible, though. Is this used in a more generalized sense, or is it just this specific instance? This variation of Mike's solution will allow the array keys to be non-numeric and/or non-incrementing. ?php foreach($second_array as $key=$value) { if(array_key_exists($key, $first_array)) { array_push($first_array[$key], $value); } else { print 'ERROR: Key '.$key.' does not exist in array $first_array.\n'; // or alternatively to add a new sub-array to $first_array // $first_array[$key] = array($value); } } ? --Bob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with arrays
for ($i = 0; $i count($array1); i++) $array1[$i][] = $array2[$i]; from kevin l'huillier -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with arrays
On 24/06/05, Josh Olson [EMAIL PROTECTED] wrote: for ($i = 0; $i count($array1); i++) $array1[$i][] = $array2[$i]; from kevin l'huillier That's basically what Mike wrote (only with array_push instead of []), and Bob improved upon. And they didn't mix the arrays up. I was only making the point that the solution was simple with a quick comment in IRC. I had not intended for it to be posted to the list. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Problem with arrays
[snip] form action=products.php method=post input name=position[pos][$value] type=text id=position[pos][$value] value=number 1 input name=position[pos][$value] type=text id=position[pos][$value] value=number 2 input name=position[pos][$value] type=text id=position[pos][$value] value=number 3 /form Note that $value from position[pos][$value] is different in all 3 fields. For each element of the array i want to update the value in the database. For example, for each value of the position[pos][$value] i want to update the database with the specific number. Something like this: $query = UPDATE table SET value = 'number 1' WHERE id='$value'; [/snip] On Good Friday, when traffic is really slow, we have someone who wants to post their problem over and over again.cool Lets start simply, shall we? http://www.php.net/print_r -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with Arrays?
At 13:46 22.02.2003, Patrick Teague said: [snip] here's what I had that didn't print anything other than 3 blank lines for this section of code - $byteSize[0] = bytes; $byteSize[1] = kb; $byteSize[2] = mb; function getMaxSize( $maxSize ) { echo $byteSize[0] . br/\n; echo $byteSize[1] . br/\n; echo $byteSize[2] . br/\n; } [snip] if you declare $bytesize global within the function it will work: function getMaxSize( $maxSize ) { global $bytesize; echo $byteSize[0] . br/\n; } You'd be still better off passing the array as variable: function getMaxSize( $bytesize, $maxSize ) { echo $byteSize[0] . br/\n; } And lastly, for performance issues, pass it as a reference: function getMaxSize( $bytesize, $maxSize ) { echo $byteSize[0] . br/\n; } Without a reference, the array is copied to the function. By passing a reference the function is working on the original array, no copy overhead. Note: When passed as reference, any modification on the array within the function will effect the original array (same is true if declared global). Without reference the original array remains unchanged. -- O Ernest E. Vogelsinger (\)ICQ #13394035 ^ http://www.vogelsinger.at/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with Arrays?
on 22/02/03 11:46 PM, Patrick Teague ([EMAIL PROTECTED]) wrote: I ran into something interesting the only thing I can figure out is that functions won't use any variables other than globals, those past to the function, or those created inside the function? exactly :) you bring the array or variable into the scope of the function ? $byteSize[0] = bytes; $byteSize[1] = kb; $byteSize[2] = mb; function getMaxSize( $maxSize ) { global $byteSize; echo $byteSize[0] . br/\n; echo $byteSize[1] . br/\n; echo $byteSize[2] . br/\n; } ? Justin French -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php