PHP - Array_merge Issues
Here's the code I am working with:
Code: [Select] <?php echo " cupcakes, ".$_SESSION['delcol'].", with the following toppings: "; $array1 = $_SESSION['notopping']; $array2 = $_SESSION['topping']; $resultam = array_merge($array1, $array2); $last = array_pop($resultam); $topping = implode(', ', $resultam) .' & ' .$last; echo $topping; ?> I have two arrays which I want to join together, if $_SESSION['notopping'] this can contain 1 - 6. $_SESSION['topping'] can contain vanilla, chocolate or chocolate orange. What I'm trying to achieve is to merge the arrays so that it shows like 1 x Vanilla, 2 x Chocolate & 3 x Choc Orange. As it was it was formatted so it showed just the toppings (with commas and ampersand). The code above appears as this : 1, 2, 3, Vanilla, Chocolate & Choc Orange - the two arrays are separated out, what am I missing here? Thanks! Similar TutorialsHi Guys, Been a while since I've done this and I cant seem to get it right. I am using an array to create a dropdown menu. The first array is all options, and the second array is the selected option. Here is the code I am using to merge the arrays. //create the dropdown $possible_pages = $this->admin_model->page_dropdown(); $current_page = $this->admin_model->current_page_dropdown($page_id); $data['pages_dropdown'] = array_merge($current_page, $possible_pages); The result I am getting seems to be reseting the keys in the array, which will not work as intended. Please point me in the right direction to get this array to look as intended the in the example below. I would like the array to look like... Array ([8] => VPO's [7] => CPO's [8] => VPO's) Here are the arrays... Array ( [7] => CPO's [8] => VPO's ) Array ( [8] => VPO's ) This is the result I am getting now. Array ( [0] => VPO's [1] => CPO's [2] => VPO's ) Hi, I failed to merge arrays. This is the case. $arraylength= 2; for($i=0;$i<$arraylength;$i++){ $vars[] = array( $key1[$i] => $value1[$i], $key2[$i] => $avalue2[$i], $key3[$i] => $avalue3[$i]); } print_r($vars); I got then, for example, this output Code: [Select] Array( [0] => Array ( [key0] => value1, [key0] => value1, [key2] => value2 ) [1] => Array ( [key10] => value10, [key11] => value11, [key12] => value12 ) ) Then I need to merge those two arrays. It should be like array_merge($vars[0],$vars[1]);, but how can i merge more arrays? Thank you in advanced, ayok So I have two arrays (soon to be multiple). What I want to do is merge all those arrays into one, so that I can then proceed to inserting the data all together. Here's an example of what array1 looks like. Code: [Select] Array ( [0] => Array ( [name] => samus1 [forum] => phpfreaks.com ) [1] => Array ( [name] => samus2 [forum] => stackoverflow.com ) ) And array2: Code: [Select] Array ( [0] => Array ( [posts] => 25 ) [1] => Array ( [posts] => 2500 ) ) I then tried to do: Code: [Select] print_r(array_merge($array1, $array2)); Yet it prints nothing? When I print the other arrays individually the values are shown, but after I use array_merge() it doesn't. Anyone got an idea why? I am trying to understand why the following code outputs the value of 4 (it is part of a PHP quiz question). I know that array_diff will compare one array with another and return the result but I just don't understand what is going on with the code below: Code: [Select] <?php $a = array(1,2,4,8); $b = array(0,2,4,6,8,10); echo count (array_merge(array_diff($a, $b), array_diff($b, $a))); //prints 4 ?> Would someone be able to walk me through it? I want to merge some arrays as Code: [Select] $mixing = array_merge($mixedresult1, $mixedresult2, $mixedresult3);but the arrays can be empty sometimes. In this case, I will get an error for the empty array as Code: [Select] PHP Warning: array_merge() [<a href='function.array-merge'>function.array-merge</a>]: Argument #1 is not an arrayHow I can avoid this error by omitting empty arrays? i am trying to sort out a session now, i normally use the folllowing code to check if someone is logged in and if they are not to forward them to the login page, but i am getting this message: Notice: Undefined index: username in code: Code: [Select] if ($_SESSION['username']) { }else{ include 'login.php'; die(""); } can someone help please. Issue # 1 How can i send the values come from $_POST['name'],$_POST['number'] to an email address xyz@radiations3.com??? Issue # 2 I am trying to display data from four different tables with the following sql query: (The problem is that the data is getting displayed fine but i don't know why some of the (same )data gets repeated two or three times where as i checked in all four tables and all the data exists one time not twice) If anything wrong with my query kindly help SELECT distinct * FROM applicant,applicant_info_nic,room,student WHERE applicant_info_nic.status='Available' and applicant.appNIC=applicant_info_nic.appNIC and applicant.roomID=room.roomID and applicant.appNIC=student.appNIC ORDER BY applicant.roomID Hello, I'm having troubles parsing text properly. Whenever I submit data into a mysql db, and then call that specific data, in the text, I get this symbol, �. Does anybody know what it is or how to remove it? I believe it's with utf-8 and me using a function that corrupts the string but I'm not sure. Anybody have any clues? Thanks in advance. Hey all, I can not get utf-8 working properly. I'm having issues with special characters, such as bullets, e with an accent over it, etc. The data is being stored in my mssql database properly, and I can see everything there fine. It's when I grab it from the database and display it on my php page. The page it set up to utf-8, if I go to Tools>Page Info, it says encoding is utf-8. I have it in my headers, I just can not figure out why it will not display on the page. Any thoughts? Hello, I have the following code Code: [Select] class Uploads extends Controller { public function __construct() { parent::__construct(); if (empty($_FILES)) { log_message('error', 'Uploads - files empty'); exit('No files uploaded'); } ini_set('memory_limit', '128M'); $this->load->helper('file'); $this->load->helper('helpers'); $this->load->model('uploads_model'); } public function font_handler() { $this->uploads_model->font_handler(); } } class Uploads_model extends Model { public function font_handler() { $config = array( 'max_size' => '8388', 'allowed_types' => 'ttf', 'upload_path' => 'assets/fonts/' ); $this->load->library('upload', $config); if ($this->upload->do_upload('Filedata')) { $file = $this->upload->data(); $jsFile = $file['raw_name'].'.js'; $query = $this->db->where('location', 'menuFont')->get('fonts')->row(); if ($query->customFile != '' && file_exists('assets/fonts/'.$query->customFile)) { unlink('assets/fonts/'.$query->customFile); } $path = getcwd() . '/assets/fonts/cufon/convert.php'; $command = 'php ' . $path . ' -u "U+??" ' . $file['full_path'] . ' 1> ' . getcwd() . '/assets/fonts/' . $jsFile; system($command); } } } Ok so, the ttf file is uploaded correctly and I can run $command from the command promt in putty and it works perfect, but when I try to run the same command from system() the js file just contains (from the controller) "No files uploaded" I have no idea why this is happening? Anyone have any ideas? Also one thing i'm not sure about is if it should be 1> or > when creating the js file?!? Thanks guys! Hello, Well let me explain my problem to you guys. I am using a printf function with an array, a custom function which i got off php.net Anyways, Everytime I use it, It seems to leave random numbers at the end of the string for no reason at all. Here is the result in source: Code: [Select] <td><a href="?page=download&name=DarkRP">DarkRP</a></td> <td>08:10:17 AM 07/08/2010</td> <td>Updated DarkRP from revision: 670 to revision: 67150</td> Array ( [Name] => DarkRP [TimeStamp] => 1281186617 [Revision1] => 670 [Revision2] => 671 ) </tr>It is mean't to show Code: [Select] Updated DarkRP from revision: 670 to revision 671but instead it has the random number 50 at the end for no reason... Anyways heres the echoing code: <?php foreach($Logs as $Log){ $Time = TimeToDate($Log['TimeStamp']); if($Log['Revision1']&$Log['Revision2']){ $Message = 'Updated %1$s from revision: %3$s to revision: %4$d'; }else{ $Message = 'Freshly downloaded: %1$s revision: %3$d'; } ?> <tr> <td><?php echo '<a href="?page=download&name='. $Log['Name'] .'">'. $Log['Name'] .'</a>'; ?></td> <td><?php echo $Time['Time'] .' '. $Time['Date']; ?></td> <td><?php echo printf_array($Message, $Log); ?></td> <?php print_r($Log); ?> </tr> <?php } ?> </table> <br /> Functions: function MySQLGetLogs(){ global $Connection; if($Connection){ $Query = sprintf('SELECT name, timestamp, revision1, revision2 FROM update_logs ORDER BY timestamp DESC;'); if($Result = mysql_query($Query)){ $Array = array(); while($Row = mysql_fetch_array($Result)){ $Array2 = array(array( 'Name' => $Row['name'], 'TimeStamp' => $Row['timestamp'], 'Revision1' => $Row['revision1'], 'Revision2' => $Row['revision2'] ) ); $Array = array_merge($Array, $Array2); } mysql_free_result($Result); return $Array; } } } function printf_array($Format, $Array) { return call_user_func_array('printf', array_merge((array)$Format, $Array)); } Also, I got the same issue with printf alone instead of the custom array function above. Hey guys, I wrote a function called isServer() so I can quickly move files from my local machine to my server without modifying too much. The function is as follows: function isServer() { if ($_SERVER['SERVER_NAME'] == 'myservername') { return(true); } else { return(false); } } // isServer() I have placed this function in a system.php file I created that is used to simply to keep things in order. That files contents a <?php session_start(); function isServer() { if ($_SERVER['SERVER_NAME'] == 'myservername') { return(true); } else { return(false); } } // isServer() include "configuration.php"; include "functions.php"; include "functions_sql.php"; include "functions_soap.php"; include "functions_mail.php"; if (isProductionServer()) { $dbhost = 'db'; $dbuser = 'db_user'; $dbpass = 'pass123'; } else { $dbhost = 'localhost'; $dbuser = 'user'; $dbpass = 'pass'; } $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'my_database'; mysql_select_db($dbname) or die ('Selected database does not exist'); ?> The problem I am having is for some reason none of the include files are recognizing the function. For example, the functions_mail.php file references the function to determine which emails to send (because the different environments have different modules and such installed). I know this is something easy, but I can't seem to figure it out. Thanks for any help! Hey, I'm editting some code on an OS Commerce install, For some reason I dont get the various buttons displaying. Underneath the shopping basket, it is supposed to show a view cart button and a checkout button, but they dont show up. I'm also trying to edit it to use a layout of the stylesheet, Basically if you look at my website, you will see the categories list, these are generated by an <ul><li> code, which puts the background underneath the item name, I would like to do the same thing and limit the charactors in the name to say 50 (so they stay on one line) Can you point me in the right direction to set an <li> on each item in the cart, and change it to UCWORDS, and restrict the charactors please? Any ideas on why the images dont show up on the page? http://tinyurl.com/2f6g2lg This is my code. <?php /* $Id: shopping_cart.php,v 1.18 2003/02/10 22:31:06 hpdl Exp $ osCommerce, Open Source E-Commerce Solutions http://www.oscommerce.com Copyright (c) 2003 osCommerce Released under the GNU General Public License */ ?> <!-- shopping_cart //--> <?php // $info_box_contents = array(); // $info_box_contents[] = array('text' => BOX_HEADING_SHOPPING_CART); // new infoBoxHeading($info_box_contents, false, true, tep_href_link(FILENAME_SHOPPING_CART)); $cart_contents_string = ''; if ($cart->count_contents() > 0) { $cart_contents_string = '<table border="0" width="100%" cellspacing="0" cellpadding="0">'; $products = $cart->get_products(); for ($i=0, $n=sizeof($products); $i<$n; $i++) { $cart_contents_string .= '<tr><td align="right" valign="top" class="infoBoxContents">'; if ((tep_session_is_registered('new_products_id_in_cart')) && ($new_products_id_in_cart == $products[$i]['id'])) { $cart_contents_string .= '<span class="newItemInCart">'; } else { $cart_contents_string .= '<span class="infoBoxContents">'; } $cart_contents_string .= $products[$i]['quantity'] . ' x </span></td><td valign="top" class="infoBoxContents"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $products[$i]['id']) . '">'; if ((tep_session_is_registered('new_products_id_in_cart')) && ($new_products_id_in_cart == $products[$i]['id'])) { $cart_contents_string .= '<span class="newItemInCart">'; } else { $cart_contents_string .= '<span class="infoBoxContents">'; } $cart_contents_string .= $products[$i]['name'] . '</span></a></td></tr>'; if ((tep_session_is_registered('new_products_id_in_cart')) && ($new_products_id_in_cart == $products[$i]['id'])) { tep_session_unregister('new_products_id_in_cart'); } } $cart_contents_string .= '</table>'; } else { $cart_contents_string .= BOX_SHOPPING_CART_EMPTY; } $info_box_contents = array(); $info_box_contents[] = array('text' => $cart_contents_string); if ($cart->count_contents() > 0) { $info_box_contents[] = array('text' => tep_draw_separator()); $info_box_contents[] = array('align' => 'right', 'text' => $currencies->format($cart->show_total()) ); //cart buttons start $info_box_contents[] = array('align' => 'center', 'text' => '<a href="' . tep_href_link(FILENAME_SHOPPING_CART) . '">' . tep_image_button ('button_cart_view.gif', IMAGE_BUTTON_CART_VIEW) . '</a>' . '<a href="' . tep_href_link(FILENAME_CHECKOUT_SHIPPING) . '">' . tep_image_button ('button_cart_checkout.gif', IMAGE_BUTTON_CART_CHECKOUT) . '</a>' ); //cart buttons end } new infoBox($info_box_contents); ?> <!-- shopping_cart_eof //--> Hey guys im having a real problem trying to make this string happend the problem is passing parameters on the javascript function "onmouseover" $map_array .= "<a href='map.php?planet=$address' onmouseover='ajax_popup ($picture,$detail_name,$stargate_address,$detail_owner,$time_conquered,$time_offset,$detail_siege, $alliance)'> <img src='images/star.jpg' style='position:absolute; left:".$b_x."px; top:".$b_y."px; border-style:solid; border-color: yellow;'></a>"; if I try this it works onmouseover='ajax_popup(1,2,3,4,5,6,7,8)'> So the problem is when the variables equal words. Cause of the quote issue. Is there like a way i should be using JSON to do what im trying to do? I have to keep this as a php string too. Or end as a php string I have no idea what the problem is with preg_replace and strings smaller than 3 characters. I'm writing to do a pattern search of abreviations(array) and replacements (array)... tried word boundries, which seems not to work at all. (it is VITAL that im able to match the whole word) here is a quick example which I cannot understand maybe someone could explain to me: 1. why repitions of certain abrevs do not get replaced. 2. why '/\bs.\b/' wont get matched (where there is a space before the b. - hense the word boundry) here is a snippet of code could someone please explain why this happens: Code: [Select] <?php $string='de d. s. pres. div. de de d. s.'; //DEFINE PATTERN $patterns = array(); $patterns[0] = '/ s. /'; $patterns[1] = '/ b. /'; $patterns[2] = '/ pres./'; $patterns[3] = '/ Ind./'; $patterns[4] = '/ Mar./'; $patterns[5] = '/ Agt./'; $patterns[6] = '/ Agy./'; $patterns[7] = '/ Indpls./'; $patterns[8] = '/ Chgo./'; $patterns[9] = '/ Corp./'; $patterns[10] = '/ U./'; $patterns[11] = '/ Fin./'; $patterns[12] = '/ ls./'; $patterns[13] = '/ Pres. /'; $patterns[14] = '/ Internat. /'; $patterns[15] = '/ m. /'; $patterns[16] = '/ N.Y.C./'; $patterns[17] = '/ div. /'; $patterns[18] = '/ de /'; $patterns[19] = '/ d. /'; //DEFINE PATTERN REPLACE $replacements = array(); $replacements[0] = ' son of '; $replacements[1] = ' born in '; $replacements[2] = ' president'; $replacements[3] = ' Indiana '; $replacements[4] = ' March '; $replacements[5] = ' Agent '; $replacements[6] = ' Agency '; $replacements[7] = ' Indianapolis '; $replacements[8] = ' Chicago'; $replacements[9] = ' Corporation '; $replacements[10] = ' U '; $replacements[11] = ' Financial '; $replacements[12] = ' Island'; $replacements[13] = ' President '; $replacements[14] = ' International '; $replacements[15] = ' married to '; $replacements[16] = ' New York City '; $replacements[17] = ' divorced'; $replacements[18] = ' de '; $replacements[19] = ' daughter of '; echo '------ Original ---------<br>'.$string.'<br>'; $data = preg_replace($patterns, $replacements, $string); echo '<br>------ Abbreviation Replace ---------<br>'.$data.'<br>'; ?> OUTPUT: ------ Original --------- de d. s. pres. div. de de d. s. ------ Abbreviation Replace --------- de daughter of son of president divorcedde daughter of d. s. any help is appriciated thanks. This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=327118.0 I'm going to post two scripts my login.php and my register.php problem is i'm always getting usernamepassword don't match i think something to do with md5 but can't figure out where... or why? <?php session_start(); $login = $_POST['login']; $password = $_POST['password']; if ($login&&$password) { $connect = mysql_connect("localhost","heaven","jefF0614") or die ("could not connect to server"); mysql_select_db("heaven_users") or die ("could not find database"); $query = mysql_query("SELECT * FROM members WHERE username='$login'"); $numrows = mysql_num_rows($query); if($numrows!=0) { while ($row = mysql_fetch_assoc($query)) { $dbusername = $row['username']; $dbpassword = $row['password']; } // check to see if they match if($login==$dbusername&&$password==$dbpassword) { echo "Your In <a href='member.php'>click here to go to the member page</a>"; //start session $_SESSION['username']=$login; } else echo "Incorrect Password/Username Combination"; } else die("That username dose not exist"); } else die ("Please enter a username and password"); ?> and register.php <?php //check for submit $submit =$_POST['submit']; //gather POST variable $fullname =strip_tags($_POST['fullname']); $email =strip_tags($_POST['email']); $username =strip_tags($_POST['username']); $password =strip_tags($_POST['password']); $password2 =strip_tags($_POST['password2']); if ($submit) { //check that fields were filled in if ($fullname&&$email&&$username&&$password&&$password2) { //check to see if our two password fields match if ($password==$password2) { $password = md5($password); // register the user //open database $connect = mysql_connect("localhost", "heaven", "jefF0614"); mysql_select_db("heaven_users"); //begine insert $queryreg = mysql_query("INSERT INTO members VALUES ('','$fullname','$email','$username','$password')"); if ($queryreg) { echo "Thanks, For Registering! Click <a href=\"index.php\">HERE</a>To Login!"; } } //otherwise if passwords don't match else { echo "<font color='red'>The passwords you entered do not match</font>"; //end of checking password } } else { echo "<font color='red'>Please enter all fields!</font>"; } } //end check for submit ?> I'm trying to run this: Code: [Select] <?php ini_set('max_execution_time','3600'); ini_set('memory_limit','512M'); ini_set('upload_max_filesize','100M'); ini_set('post_max_size','100M'); phpinfo(); ?> Max execution time and memory_limit works but upload_max_filesize and post_max_size don't work. Are you allowed to set those at run time? Okay, some code broke. I was doing this, basically. inc_db.php Code: [Select] <?php $link_db = mysql_connect('localhost','user','pass') or die('Could not connect: ' . mysql_error()); mysql_select_db('main_db') or die ('Could not select database'); ?> inc_other_db.php Code: [Select] <?php $other_db = mysql_connect('localhost','user','pass') or die('Could not connect: ' . mysql_error()); mysql_select_db('other_db') or die ('Could not select database'); ?> funcs.php Code: [Select] <?php function do_something( $a, $b ) { $query = "SELECT id FROM users WHERE a = '$a' AND b = '$b'"; $result = mysql_query( $query ) or die ( 'Query result failed: ' . mysql_error() ); // do something with result } ?> Caveat: One script pulls in inc_other_db.php *after* the inc_db.php so it uses that. New funcs.php Code: [Select] <?php function do_something( $a, $b ) { global $linkdb; $query = "SELECT id FROM users WHERE a = '$a' AND b = '$b'"; $result = mysql_query( $query, $linkdb ) or die ( 'Query result failed: ' . mysql_error() ); // do something with result } ?> Runs, but mysql_query() always fails, and mysql_error() always returns a blank error. Any ideas? Something big changed in PHP5 that I'm totally missing? I'm getting jack in the PHP error logs. How can I print out some info about $linkdb in my function? I checked it with if ( !$linkdb) and isset(), and both seemed fine. Totally frustrated. Sleeping now. I have an XML document that id decrypted and shown onto a table. At the bottom of the page, I'm trying to have it count up one Code: [Select] foreach($Data as $Types) // loop through book foreach($Types as $Colors) // loop through book { print_r(count($Colors->Color)); } There are 4 colors, blue, green, red, yellow. Blue is listed 7 times, green is 3, red is 8, and yellow is 4 times. What I expect it to print is: 7 3 8 4 Instead, it's printing this: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 If I was to just do the print function without count, it lists like this: blue red blue red green yellow blue red yellow red red green yellow yellow blue blue green blue yellow blue red red blue red My end-goal of this code is to get it to print this: Blue: 7 Green: 3 Red: 8 Yellow: 4 |