PHP - Implode Multidimensional Array Help
Code: [Select]
while($row=mysql_fetch_assoc($query)) { $id = $row['id']; $col1 = $row['name1']; $col2= $row['name2']; $col3= $row['name3']; ${"$id"} = array("$col1","$col2","$col3"); ${"$s".$i}[] = ${"$id"}; } This is just a brief example of what i'm trying to accomplish,$i is incremented somewhere else. I'm trying to implode the arrays in the array. So below i have imploded the main array but how do i implode the other arrays? Code: [Select] for($i=0;$i<11;$i++) { $array = ${"s" . $i}; $outcomes = implode("",$array); //implodes main array } Similar TutorialsI came accross a problem where im not sure how to add the word "and" inbetween my variables. I'm only displaying one $field below but i have 6 of them. If I add and as the seperator in the implode it will repeat it and and. Also it will put it at the end Code: [Select] <?php $field1 = "Farms to " . $_POST['farms_population']; $a = array("$field1", "$field2", "$field3", "$field4", "$field5", "$field6"); $_SESSION['changed'] = "You have changed " . implode(" ",$a); echo $_SESSION['changed']; ?> wanting it to echo something like You have changed Farms to 30 and Larva Pools to 10 and Homes to 10 This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=319071.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=308349.0 I get this error on my homepage : Warning: implode() [function.implode]: Invalid arguments passed in /home/shqip1/peqini.com/wp-content/themes/sportpress/functions/wpzoom-widgets.php on line 1469 This is the code to modify http://pastebin.com/RiBsyG3c Thanks i have a problem i cant solved for cople of hours! this is the function: function show_posts($user_id,$limit=0){ $posts = array(); $user_string = implode(',', $user_id); $extra = " and id in ($user_string) "; if ($limit > 0){ $extra = "limit $limit"; }else{ $extra = ''; } $sql = "SELECT userid, caption, stamp, filename FROM photographs WHERE userid in ($user_string) order by stamp desc $extra"; //echo $sql; $result = mysql_query($sql) or die(mysql_error()) ; while($data = mysql_fetch_object($result)){ $posts[] = array( 'stamp' => $data->stamp, 'userid' => $data->userid, 'caption' => $data->caption, 'filename' => $data->filename, ); } return $posts; } and thats the full error: Warning: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\includes\functions.php on line 12 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') order by stamp desc' at line 2 and the intresting thing that if i echo the sql i get a working one i checked it in phpmyadmin + it shows what it need to show in the index.php if i comment the "or die(mysql_error()) " here the sql when its do echo SELECT userid, caption, stamp, filename FROM photographs WHERE userid in (4,7,2) order by stamp desc limit 5 so y the hell this problem is keep coming? i really cant understand what wrong! tnx for the help! I am using a PHP class (it is attached) for Google Analytics that I got from this link below: http://www.acleon.co.uk/?p=173 Unfortunately, it appears the Class has a PHP error: Warning: implode() [function.implode]: Invalid arguments passed in Galvanize.php on line 114 and line 181. This is line 114: setcookie('__utmc', implode('.', $this->UTMC), 0, $this->CookiePath, $this->getDomain()); Here is line 181: $urchinUrl .= '&utmcc=__utma%3D'.implode('.', $this->UTMA).'%3B%2B__utmz%3D'.implode('.', $this->UTMZ).'%3B'; I did not see UTMC and UTMZ declared anywhere in the Class. The class file is attached. Thanks for your help in advance. My error is 'Warning: implode() [function.implode]: Invalid arguments passed in /home/definiti/public_html/contact.php on line 73' Im not sure how to fix this error, as I looked online and it says that my implode() has the right layout too it.. Line 73 in my code (I took the HTML code out) is this line; Code: [Select] $message = implode("<br>\n", $messages); My whole code is; Code: [Select] <?php # PHP Copyright 2007, Thomas Boutell and Boutell.Com, Inc. # Edited by Definition Designs. $recipient = 'defenitiondesigns@googlemail.com'; $serverName = 'www.definition-designs.co.uk'; if ($_POST['send']) { sendMail(); } elseif (($_POST['cancel']) || ($_POST['continue'])) { redirect(); } else { displayForm(false); } function displayForm($messages) { global $login; $escapedEmail = htmlspecialchars($_POST['email']); $escapedRealName = htmlspecialchars($_POST['realname']); $escapedSubject = htmlspecialchars($_POST['subject']); $escapedBody = htmlspecialchars($_POST['body']); $returnUrl = $_POST['returnurl']; if (!strlen($returnUrl)) { $returnUrl = $_SERVER['HTTP_REFERER']; if (!strlen($returnUrl)) { $returnUrl = '/'; } } $escapedReturnUrl = htmlspecialchars($returnUrl); ?> <?php if (count($messages) > 0) { $message = implode("<br>\n", $messages); echo("<h4>$message</h4>\n"); } ?> <form method="POST" action="<?php echo $_SERVER['DOCUMENT_URL']?>"> <p> <b>Your</b> Email Address <input name="email" size="35" maxlength="35" value="<?php echo $escapedEmail?>"/> </p> <p> Your <b>Real</b> Name <input name="realname" size="35" maxlength="35" value="<?php echo $escapedRealName?>"/> </p> <p> Subject Of Your Message <input name="subject" size="35" maxlength="35" value="<?php echo $escapedSubject?>"/> </p> <p> <i>Please enter the text of your message in the field that follows.</i> </p> <textarea name="body" rows="10" cols="60"><?php echo $escapedBody?></textarea> <p> <input type="submit" name="send" value="Send Your Message"/> <input type="submit" name="cancel" value="Cancel - Never Mind"/> </p> <input type="hidden" name="returnurl" value="<?php echo $escapedReturnUrl?>"/> </form> <?php } function redirect() { global $serverName; $returnUrl = $_POST['returnurl']; $prefix = "http://$serverName/"; if (!beginsWith($returnUrl, $prefix)) { $returnUrl = "http://$serverName/"; } header("Location: $returnUrl"); } function beginsWith($s, $prefix) { return (substr($s, 0, strlen($prefix)) === $prefix); } function sendMail() { global $recipient; $messages = array(); $email = $_POST['email']; if (!preg_match("/^[\w\+\-\.\~]+\@[\-\w\.\!]+$/", $email)) { $messages[] = "That is not a valid email address. In format: You@something.com"; } $realName = $_POST['realname']; if (!preg_match("/^[\w\ \+\-\'\"]+$/", $realName)) { $messages[] = "The real name field must contain only alphanumeric characters, spaces and + or - signs."; } $subject = $_POST['subject']; if (preg_match('/^\s*$/', $subject)) { $messages[] = "Please specify a subject for your message. "; } $body = $_POST['body']; if (preg_match('/^\s*$/', $body)) { $messages[] = "Your message was blank. Fill out a message or Click Cancel to cancel message."; } if (count($messages)) { displayForm($messages); return; } mail($recipient, $subject, $body) or die("unable to send the mail!"); $escapedReturnUrl = htmlspecialchars($_POST['returnurl']); ?> <form method="POST" action="<?php echo $_SERVER['DOCUMENT_URL']?>"> <input type="submit" name="continue" value="Click Here To Continue"/> <input type="hidden" name="returnurl" value="<?php echo $escapedReturnUrl?>"/> </form> <?php } ?> I am getting this error in the header.php of my wordpress theme... Can anyone help? Thanks! Warning: implode() [function.implode]: Invalid arguments passed in /home/bexxx/public_html/wp-content/themes/organic_portfolio_gray/header.php on line 74 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>> <head profile="http://gmpg.org/xfn/11"> <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" /> <meta name="distribution" content="global" /> <meta name="robots" content="follow, all" /> <meta name="language" content="en" /> <title><?php wp_title(''); ?><?php if(wp_title('', false)) { echo ' :'; } ?> <?php bloginfo('name'); ?></title> <link rel="Shortcut Icon" href="<?php echo bloginfo('template_url'); ?>/images/favicon.ico" type="image/x-icon" /> <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" /> <link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" /> <link rel="alternate" type="application/atom+xml" title="<?php bloginfo('name'); ?> Atom Feed" href="<?php bloginfo('atom_url'); ?>" /> <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" /> <?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?> <?php wp_enqueue_script("jquery"); ?> <?php wp_head(); ?> <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/superfish/superfish.js"></script> <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/superfish/hoverIntent.js"></script> <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery.flow.1.1.js"></script> <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/iepngfix_tilebg.js"></script> <!--IE6 Fix--> <style type="text/css"> img, div, a, input, body, span { behavior: url(<?php bloginfo('template_url'); ?>/images/iepngfix.htc); } </style> <script type="text/javascript"> var $j = jQuery.noConflict(); $j(function() { $j("div#controller").jFlow({ slides: "#slides", width: "960px", height: "480px", timer: <?php echo ot_option('slider_interval'); ?>, duration: 400 }); }); </script> <script type="text/javascript"> $j(document).ready(function() { $j('ul.ot-menu').superfish(); }); </script> </head> <body> <div id="wrap"> <div id="header"> <div class="headercenter"> <p id="title"><a href="<?php echo get_option('home'); ?>/" title="Home"><?php bloginfo('name'); ?></a></p> </div> </div> <div id="navbar"> <div id="nav"> <div id="navbarleft"> <ul class="ot-menu"><li<?php if (is_home()) { echo " class=\"current_page_item\""; }?>><a href="<?php echo get_settings('home'); ?>"><?php _e("Home", 'organicthemes'); ?></a></li> <?php $include_categories = ot_option('include_categories'); ?> <?php wp_list_categories('depth=4&title_li=&sort_column=menu_order&include='.implode(',', $include_categories)); ?></ul> </div> <div id="navbarright"> <ul class="ot-menu"> <?php $include_pages = ot_option('include_pages'); ?> <?php wp_list_pages('title_li=&sort_column=menu_order&include='.implode(',', $include_pages)); ?> </ul> </div> </div> </div> <div style="clear:both;"></div> Hello, I'm just a beginner (and french sorry for my language) : have some pb with my little implode function. It's Running well on my local webserver (MAMP - PHP PHP Version 5.2.17) but when tested on ovh server (PHP4) show this error : Warning: implode() [function.implode]: Invalid arguments passed in /homez.xx/myPathHere/entete.php5 on line 6 I understand 1/ that one argument is missing but don't find the right syntax 2/ I have tried to turn this file on php5 with this extension (ovh is under php4) Here is my code in page defining the value and where stay my query (entete.php5) <?php if (isset($theme)) { $t = implode(',',$theme); $lien = "aide.php5?theme=".$t; $aide = '<a href="'.$lien.'">'.$onglet[0].'</a>'; } else { $aide = ""; } ?> Here is my code in page which show result (aide.php5) $t = explode(',',$_GET['theme']); $n = count($t); if ($n == 1) { $where = "id = ".$t[0]; } else { $where = "id = ".$t[0]; for ($i=1;$i<$n;$i++) { $where .= " OR id=".$t[$i]; } } Hope someone kindly help me : thanks in advance This is my code for displaying images from a folder:
function generate_img ($imagepointer, $imageornot){ if($imageornot == 1){ $var = getcwd(); $dirlocation = $var."\\imageuploads\\".$imagepointer; $files = array_diff(scandir($dirlocation), array('..', '.')); foreach ($files as $image){ $data[] = array('label' => '<img src="/imageuploads/'.$imagepointer.'/'.$image.'" alt="thumbnail"> <br/>'); }; return $data; } else { return FALSE; } } I'm using it with print_r: <p><?php print_r(generate_img($imagepointer, $imageornot)); ?></p> Working ok, but obviously not good for a live site. Is implode() a good idea in this situation? And how would I use it? Appreciated. Not sure if this is right. I can't get it to insert my record. Can someone please tell me if I'm doing it right?
cus_functions.php
function dbRowInsert($table_name, $form_data) { global $conn; $fields = array_keys($form_data); $sql = "INSERT INTO ".$table_name." (`".implode('`,`', $fields)."`) VALUES('".implode("','", $form_data)."')"; return mysqli_query($sql); }process.php include("new_db.php"); include("cus_functions.php"); $do=$_GET['do']; if($do=='addpro'){ if(isset($_POST['submit'])){ $input = $_POST['title']; $comp = '0'; $form_data = array('title' => $input, 'completed' => $comp); dbRowInsert('projects', $form_data); }} Can anyone help me complete this script I can get it working with using an implode function, but for the purpose of knowledge I want to not use it. I just want it to echo back the corresponding checkboxes, but I can't get the echoing correct, it echoes back array not the value? Code: [Select] <html> <title>What do you want on your Tombstone?</title> <body> <?php $Output_form = true; if (isset($_POST['submit'])) { if (empty($_POST['toppings'])) { echo '<b>You forgot the toppings for your Tombstone.</b>'; echo '<br>'; echo '<br>'; } else { echo '<p><b>This is what you are getting on your Tombstone: '; (array)$_POST['toppings']; echo $toppings; echo '!'; echo '</p></b>'; $Output_form = false; } } if ($Output_form) { ?> <form action="ProcessToppings.php" method="post"> <p><i><h2>What do you want on your Tombstone?</h2></i></p> <p> <input type="checkbox" name="toppings[]" value="pepperoni" /> Pepperoni<br /> <input type="checkbox" name="toppings[]" value="sausage" /> Sausage<br /> <input type="checkbox" name="toppings[]" value="mushrooms" /> Mushrooms<br /> <input type="checkbox" name="toppings[]" value="cheese" /> Cheese<br /> <input type="checkbox" name="toppings[]" value="olives" /> Olives</p> <p> <input type="submit" value="Make that Peet-Za!" name="submit" /></p> </form> <?php } ?> </body> </html> Hi, I am a little bit new to php, and I am learning about array functions. I just wanted to know the difference between implode and explode in arrays, and how each of them would be used? Thank you! Hi, I'm trying to explode an array into a multidimensional array using <br /> tags as splitters but just can't get the syntax right! I can do lines of the array one by one, but not the whole lot at once. I'd really appreciate a quick example of an explode inside an array. $myarray looks something like this with print_r: ( [0] => [1] => some_html<br />some_html<br />some_html<br /> [2] => some_html<br />some_html<br />some_html<br /> [3] => some_html<br />some_html<br />some_html<br /> ) and I want to turn it into this: ( [0] => [1] => [0]some_html [1]some_html [2]some_html [2] => [0]some_html [1]some_html [2]some_html [3] => [0]some_html [1]some_html [2]some_html ) I've tried this $myarray = explode ("<br />", $myarray); but it doesn't work … It doesn't matter too much if there's are a few empty lines in the array from the final <br /> tags. Many thanks, as ever, for any help. I have the following array structu Code: [Select] [0] => Array ( [id] => Array ( [$t] => http://www.google.com/mate/ ) [updated] => Array ( [$t] => 2011-08-31T11:43:05.942Z ) [category] => Array ( [0] => Array ( [scheme] => http://schemas.google.com/g/ [term] => http://schemas.google.com/contact/ ) ) [title] => Array ( [type] => text [$t] => Name ) [link] => Array ( [0] => Array ( [rel] => http://schemas.google.com/contacts/2008/rel#edit-photo [type] => image/* [href] => https://www.google.com/mate/feeds/photos/media/ ) [1] => Array ( [rel] => self [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/contacts ) [2] => Array ( [rel] => edit [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/ ) ) [gd$email] => Array ( [0] => Array ( [rel] => http://schemas.google.com/g/2005#other [address] => email_address@gmail.com [primary] => true ) ) ) I am tried to display name and email address. I am using following method, can anyone tell me is there any better way? Thank u 4u help. NAME $name=( $emp_det[0]['title'][t]); $email =( $emp_det[0]['gdemail'][0][address]); Hi everybody, I generally find any assistance that I need on various sites, but this one has me stumped. I'm not overly advanced with my use of arrays, so I'd like some help here if anyone knows what I am looking for. I have a form that I would like to submit to a MySQL database. In that form, there is the ability to add up to 3 harddrives: Code: [Select] Brand: <input type="text" name="hdds[0][hddbrand]" id="hddbrand"><br/> Model and/or size,type: <input type="text" name="hdds[0][hddtype]" id="hddtype"><br/> SN: <input type="text" name="hdds[0][hddsn]" id="hddsn"><br/> Notes: <input type="text" name="hdds[0][hddnotes]" id="hddnotes" size="50"> Obviously the next harddrive would be hdds[1][hddbrand], etc. I am having an awful time looping through this to get more than 1 harddrive's information, however. I've tried foreach embedded in foreach and been messing around with this for a good 3-4 hours now and I feel like I am just missing something. I've got this right now to debug: Code: [Select] foreach($_POST['hdds'] as $key => $a) { $hddbrand = $a['hddbrand']; } my print_r($a) comes up with the following: Code: [Select] Array ( [hddbrand] => Toshiba [hddtype] => Shaba 500gb [hddsn] => 5fu8bvw4 [hddnotes] =>none ) 1 Good. That's what I want. But I need some help constructing my array/loop to get the values of more than 1 drive should I have to enter information for more than 1 drive. I feel like I am close, but I am just not getting what I need. I have not had to work with multidimensional arrays before and they are proving to be more trickier than I expected. Any help would be enormously appreciated! Patrick I am looking at the code below: Code: [Select] $array = array( array( 1, 2 ), 'a' => array( 'b' => 1, 'c' ) ); Is this an example of a multidimensional array? If so how would I access the value of 1 from key b? Also would the next line ('c') be automatically assigned to the asociative key of c? I need to extract data from a CSV file and insert it into a MySQL database. I am able to extract the data, however I cannot figure out how to group it. Sample File: 01 ISBN Name of Book Price 02 ISBN Name of Book Price So far I have an array with the entire file (reading file with PHP): Array ([0] => 01 [1] => 12345678X [2] => Title [3] => 120.00 ...etc. How can I modify the array to create groups of four for each item? Each item is its own array? I've been working on some code to: 1. Search a db for rows that have a particular "position" value 2. Search the same db for rows that have a particular "langs" value 3. Compare the two arrays resulting for 1 and 2 4. Create a multidimensional array $langsarray[langs][question id] if 3 is true. 5. For each langs array in $langsarray pick a random, non-duplicate value and add it to $qarray for a maximum of 12 elements (3 for each langs). What I have works about 95%. The problem is, in the first langs array, one of the 3 randomly picked values is almost always empty, and its not always the same element. Sometimes its the first, sometimes its the third, sometimes its the second... I think I've narrowed down the issue to line 44 ($qtemp = ...). If I change the min/max values for the mt_rand function, it behaves slightly differently. Any help is much appreciated! Thanks in advance. Code: //Include MYSQL class and authentication information require_once('./include/mysql.php'); require_once('./include/global.php'); //Grab apptype from URL querystring $apptype = $_GET['apptype']; //Declare arrays $qarray = array(); $posarray = array(); $langs = array(); $langsarray = array(); //Get id's of all questions pertaining to position type $sql = 'SELECT * FROM qa WHERE position = "' . $apptype . '"'; $result = $db->query($sql) or die(mysql_error()); while ($row = $result->fetch()) { $posarray[] = $row['id']; } //Determine what types of questions should be pulled if ($apptype == 'fed'){ $langs = array('html','css','javascript','jquery'); } else if($apptype == 'bed'){ $langs = array('php','asp','javascript','jquery'); } //Get each question of each language that matches the position type and store it in the multidimensional array $langsarray[language][question id] $z = 0; while ($z < count($langs)){ $sql = 'SELECT * FROM qa WHERE langs = "' . $langs[$z] . '"'; $result = $db->query($sql) or die(mysql_error()); while ($row = $result->fetch()) { if (in_array($row['id'],$posarray)){ $langsarray[$langs[$z]][] = $row['id']; } } //Takes a random question id from the current language ($z) and adds it to the final question array. $y = 0; while ($y < 3) { //$qtemp = $langsarray[$langs[$z]][mt_rand(0,count($langsarray[$langs[$z]]))]; $qtemp = $langsarray[$langs[$z]][mt_rand(0,count($posarray))]; if (!in_array($qtemp,$qarray)){ $qarray[] = $qtemp; echo $qtemp . ', '; $y++; } } echo $langs[$z]; print_r( $langsarray[$langs[$z]]); echo '<br />'; $z++; } echo '<br />'; print_r($qarray); Output: 5, , 2, htmlArray ( => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) <--Problem 7, 6, 9, cssArray ( => 6 [1] => 7 [2] => 8 [3] => 9 [4] => 10 ) 14, 15, 13, javascriptArray ( => 11 [1] => 12 [2] => 13 [3] => 14 [4] => 15 ) 21, 17, 20, jqueryArray ( => 16 [1] => 17 [2] => 18 [3] => 19 [4] => 20 [5] => 21 ) Array ( => 5 [1] => [2] => 2 [3] => 7 [4] => 6 [5] => 9 [6] => 14 [7] => 15 [8] => 13 [9] => 21 [10] => 17 [11] => 20 ) <-- Final $qarray I am trying to store all the elements of my tree in an array.My code produces multi dimensional array, but i want it to be one dimensional. How can i get it ? Code:- Code: [Select] function tree_gather($node) //Function to calculate count { $sql = "SELECT lchild,rchild FROM tree WHERE parent = '$node'"; $execsql = mysql_query($sql); $array = mysql_fetch_array($execsql); if(!empty($array['lchild']) || !empty($array['rchild'])) { $child[] = $array['lchild']; $child[] = $array['rchild']; $child[] = tree_gather($array['lchild']); $child[] = tree_gather($array['rchild']); } return $child; } Result:- Code: [Select] Array ( [0] => 2 [1] => 3 [2] => Array ( [0] => 4 [1] => 5 [2] => Array ( [0] => 10 [1] => 11 [2] => [3] => ) [3] => Array ( [0] => 8 [1] => 9 [2] => [3] => ) ) [3] => Array ( [0] => 7 [1] => 6 [2] => Array ( [0] => 15 [1] => 14 [2] => Array ( [0] => 16 [1] => 17 [2] => [3] => ) [3] => ) [3] => Array ( [0] => 13 [1] => 12 [2] => [3] => ) ) ) Table: - |