PHP - Not Able To Write Code For Traversing Multidimentional Array
this is an array returned after parsing YAMl file using spyc api.
i want to write a code to to extract value related to key topic name and put in a separate 1d array say $topicnames Array ( [1] => Array ( [topicname] => singapore travels [rev] => 250 => Array ( [25] => Array ( ) [topicname] => airlines [rev] => 24 ) [1] => Array ( [35] => Array ( ) [topicname] => shubham [rev] => 65 ) ) [2] => Array ( [topicname] => singapore entertainment [rev] => 210 => Array ( [45] => Array ( ) [topicname] => night safari[rev] => 54 ) i tried using simple for loop but not working.. pl help me out. Similar TutorialsHello every body i have a array like this
Array ( [19/5/14] => Array ( [94-DE-80-28-CF-BC] => Array ( [7] => Array ( [qax] => 1 ) ) } }I want to add new array under the [94-DE-80-28-CF-BC] => Array.. I am doing code but what it does it overrides the full array inside that array and create a new one... The code i've used is | array_push( $vis[$date][$macp], array( "$id"=>array( "$country"=>1 ) )); I've got a few arrays as follows: Code: [Select] $array1 = ('A', 54, 'a', 2); $array2 = (, 67, 'b',); $array3 = ('R', 54,,); $array4 = (, 36, 'b'); $array5 = (, 36, 'a',); $array6 = ('FX', 119, 's', 4); $array7 = ('R', 3, 'c',); $superarray = array($array1, $array2, $array3, $array4, $array5, $array6, $array7); Now, I've got a piece of code that sorts $superarray by index[1] of the sub-arrays. The code looks like this: Code: [Select] function sortmulti ($array, $index, $order, $natsort=FALSE, $case_sensitive=FALSE) { if(is_array($array) && count($array)>0) { foreach(array_keys($array) as $key) $temp[$key]=$array[$key][$index]; if(!$natsort) { if ($order=='asc') asort($temp); else arsort($temp); } else { if ($case_sensitive===true) natsort($temp); else natcasesort($temp); if($order!='asc') $temp=array_reverse($temp,TRUE); } foreach(array_keys($temp) as $key) if (is_numeric($key)) $sorted[]=$array[$key]; else $sorted[$key]=$array[$key]; return $sorted; } return $sorted; } $sortedarray = sortmulti($superarray, 1, ASC, $natsort=FALSE, $case_sensitive=FALSE) The output is this: R3c 36b 36a A54a2 R54 67b FX119s4 This is great because it goes 3 -> 36 -> 36 -> 54 -> 54 -> 67 -> 119. However, the problem, if you look, is this part: 36b 36a Its sorted everything according to index[1] of the sub-arrays, but now I would like to sort by index[2], then index[3] as well. I'd like the output to look like this: R3c 36a 36b A54a2 R54 67b FX119s4 Any ideas? Because this is way over my head ... Hello everybody, i am working on n level category tree structure, for your information the category tree structure image is attached with this post. Let's consider, each of the category have hundreds( n level ) of category in each, i want to traverse each category without using recursive functions, reason being recursive functions are very slow, is there any way to do this?? Thanks, phpeid How do I echo the array below in a for each loop? I'm trying to output this data in a table. Array ( [u] => Array ( [0] => Array ( [row_id] => 1 [0] => Jn [1] => Bo [2] => do [3] => jilcom [4] => hocom ) [1] => Array ( [row_id] => 3 [0] => Jk [1] => Kk [2] => wd [3] => jk [4] => ) [2] => Array ( [row_id] => 5 [0] => Jn [1] => Bo [2] => on [3] => jm [4] => hm ) ) [blog] => Array ( [0] => Array ( [row_id] => 3 [0] => dk [1] => [2] => [3] => [4] => ) [1] => Array ( [row_id] => 9 [0] => 2rs [1] => dee [2] => [3] => [4] => ) ) ) Good evening all, I am trying to expand my experience with PHP by doing a small project for a personal website. I am working from scratch using notepad and so far have been having great sucess. My experience with C# is helping greatly but sadly ive hit a snag and can't seem to free myself. I have an array that is populated with items, and the prices for those items if you are buying/selling them. What I will have is a form with a drop list that is populated by this array list (already working) and a text entry that you can enter a numerical quantity value, then another drop list that will have buy / sell as an option. What the page will do is take the values from the form and calculate the "total" from the supplied values. Sadly some reason I cant set a $Variable to the value of the array values. I can echo them and get the correct value, but can't set the Variable to the arrays value! Code: [Select] <?php $ResBuySell = array( "Iron ingot" => array("Buy" => 6, "Sell" => 7), "Shadow iron ingot" => array("Buy" => 14, "Sell" => 16), "Dull copper ingot" => array("Buy" => 14, "Sell" => 16), "Copper ingot" => array("Buy" => 6, "Sell" => 7), "Bronze ingot" => array("Buy" => 6, "Sell" => 7), "Gold ingot" => array("Buy" => 6, "Sell" => 7), "Agapite ingot" => array("Buy" => 6, "Sell" => 7), "Verite ingot" => array("Buy" => 6, "Sell" => 7), "Valorite ingot" => array("Buy" => 6, "Sell" => 7), "Feathers" => array("Buy" => 6, "Sell" => 7), "Arrows" => array("Buy" => 6, "Sell" => 7), "Bolts" => array("Buy" => 6, "Sell" => 7), "Boards" => array("Buy" => 4, "Sell" => 6), "Spined leather" => array("Buy" => 16, "Sell" => 18), "Horned leather" => array("Buy" => 6, "Sell" => 7), "Barbed leather" => array("Buy" => 6, "Sell" => 7), "Cloth" => array("Buy" => 6, "Sell" => 7), "Ribs" => array("Buy" => 6, "Sell" => 7), "Fish steaks" => array("Buy" => 6, "Sell" => 7), "Orange petals" => array("Buy" => 6, "Sell" => 7), "Red leaves" => array("Buy" => 6, "Sell" => 7), "Green thorns" => array("Buy" => 6, "Sell" => 7) ); function GetPrice($ResType = "Iron ingot", $TranType = "Buy") { return $ResBuySell[$ResType][$ResType]; } function GetQuantity() { return 5; } function GetTotal() { $ResPrice = GetPrice(); $ResQuanity = GetQuantity(); $ResTotal = $ResPrice * $ResQuanity; return $ResTotal; } echo GetTotal(); ?> Quote 0 The problem resides he Code: [Select] function GetPrice($ResType = "Iron ingot", $TranType = "Buy") { return $ResBuySell[$ResType][$ResType]; } Some reason when I echo this: Code: [Select] $ResType = "Iron ingot"; $TranType = "Buy"; echo $ResBuySell[$ResType][$ResType]; it will return; Quote 6 The proper value, but when I put it into a function and try to retrieve this info via the function it returns zero. I am a beginner with php and just trying to learn by trial and error, but after failing at this for 5 days I figured it was time to call in help from the big guns! Thanks in advance to anyone who can offer my any assistance with this problem! I have a web site where users can gain points, when they use my site. For example, if the user spends $10.00 then the user will get 10 points. In my web site there is a list of 10 retails (name). When the user clicks the link it will redirect to retailers website, where user can buy items. How can I find out how much the user has spent on the retailer? Thank you. I am trying to write post variables to a csv file but it writes everything in one line separated by comma <?php $list= array($_POST['purchases']); $file = fopen("purchases.csv", "w"); foreach ($list as $line) { fputcsv($file, $line); } fclose($file); ?> Result in purchases.csv file Marilyn,Nancy,Johan,Carol,Juanic,Shirley But I want every string value on separated line Marilyn Nancy Johan Carol Juanic Shirley
I have any array with name, gender , age I want to write into a csv which already contains many records line by line, I would like to write the array elements into the csv as a line line from the bottom without overwritten the whole csv. How to do so? Thanks Code: [Select] //this varible contains the array of existing users $existing_users=array('roshan','mike','jason'); //value got from the get metho $user_name=$_POST['user_name']; //checking weather user exists or not in $existing_users array if (in_array($user_name, $existing_users)) { //user name is not available echo "no"; } else { //username available i.e. user name doesn't exists in array echo "yes"; } the array near the top has a list of users, I would like to loop through my users in my database I know how to write a loop but not into here Code: [Select] $existing_users=array('roshan','mike','jason'); I am running a script to retrieve numerical value from a webpage, the script echoes the numbers. However I need to add all the numbers to an array or txt file and then have them sorted from highest value to lowest value. Any hints or tips to how I would achieve this? With this link http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?appid=YahooDemo&query=Madonna&results=2 , one can see suggested keywords for a query. The result is like "madonna 4 minutes madonna 4 minutes lyrics". How to put the resulting keywords in a $array, as can be used for further php coding. suggestions on how to write this code better I have a form that has 2 textboxes in it. The php listed below...confirms that the passwords match, confirms that there are at least 6 characters in in, confirms that there is at least 1 number. I am doing mostly if statements with it, but I was wondering if there was a better way to write. Code: [Select] <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form name="form1" method="post" action="<?PHP $_SERVER['PHP_SELF'] ?>"> <table width="100%" border="1"> <tr> <th scope="row"> <strong>New password: </strong> <input type="password" maxlength="15" name="password"></th> <th scope="row"> <strong>New password confirmed: </strong> <input type="password" maxlength="15" name="confirm_password"></th> <th scope="row"><input type="submit" name="update" value="Update Password" /></th> </tr> </table> </form> <?PHP //verify that the update button is pressed if (isset($_POST['update'])) { //verify that the passwords match if($_POST['password'] == $_POST['confirm_password']) { echo "passwords match"; echo "<br>"; } //verify that the password and password confirmation was entered elseif ((isset($_POST['password'])) && ($_POST['confirm_password'])) { echo "Password and confirm password set"; echo "<br>"; } //verify that the # of characters was entered elseif (strlen($_POST['password'])<6) { $num_char=strlen($_POST['password']); echo "Please enter more characters"; echo "<br>"; } //find out if number exsist if (strlen($_POST['password'])>5) { $subject = $_POST['password']; preg_match_all('/[0-9]/', $subject, $matches); $count = count($matches[0]); //echo $count; if ($count <=1) echo "Please enter in at least 1 number you currently have: " .$count; }//close of verification of both boxes entered }//close of $_POST ?> </body> </html> Can you suggest a better way to write this code (I didn't create it):
<body onload="getParameterByName('url')"> <a href="" id="urllink" >Click Here</a> </body>so, I don't have to change the <body> tag? This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=327190.0 <?php $result = "SELECT * FROM portfolio"; $result = mysql_query ($result) or die (mysql_error()); $i=0; while($row = mysql_fetch_assoc($result)) { if($i==0) echo '<div class="portfolioPage">'; if($i==2) $divclass = 'portfolioProjectWrapper borderWhite'; else $divclass = 'portfolioProjectWrapper borderGray'; echo ' <div class="'.$divclass.'"> <a href="portfolioPage.html" class="image asyncImgLoad" title="img/'.$row['image290x290'].'"></a> <p class="imageDesc">'.$row['image290x290_phot'].'</p> <h3 class="title">'.$row['title'].'</h3> <p class="subtitle">'.$row['subtitle'].'</p> <p class="desc"> '.substr($row['description'], 0, 1200).' <a href="portfolioPage.html" class="commonLink">Read more</a> </p> </div> '; if($i==2) echo '</div>'; if(i==2) $i=0; else $i++; } ?> Please help me im backward at this been along time. Hi there, I've looked into the PHP superglobal array $_SERVER, but could not find a variable that stores the plugins a client has on his/her browser such as firefox and google chrome. The point here is to detect if a user has hacking addons such as firebug and inspect element installed, and displays an error message telling the user to disable such plugins in order to access site content. Is it possible to accomplish such tasks? Please help. I have a small project. I have a control board that is connected to the parallel port. It controls 8 strips of LED on the LPT1 port. I can turn these on and off via the command prompt, using LPTOUT.exe ie lptout 1 turns on the 1st LED lptout 2 will turn off 1st LED and turn on 2nd, lptout 4 turns on 3rd and 2nd off, etc 1, 2, 4, 8, 16, 32, 64, 128. and combinations ie lptout 3 turns on LEDS 1 and 2, and so on. However lptout seems very unresponsive sometimes I have to do lptout 1 twice before LED 1 turns on. Is there another command I can use to write to LPT1 port. I need some PHP code to control the outputs. I want to sequence the outputs ie 1on (1second) 2on (1 second) 3 on (1 second) 4 on etc. to 8, then all off then repeat. Can anyone help me. THANK YOU. I am a complete newbie. I have installed xampp. I created a database and the tables but that is all. I need to create all my relationships and test my database. to do this, do I now need to write the php code to view the results in a browser or... do I do it in MySQL. If so, Can someone just point me in the direction I need to go for my next step.
Thanks
I am trying to make a loop simply write "static" code...if that is the correct explanation.... But it seems PHP wants to "process" such things as 'IF' and other Conditions which is defeating the purpose. The problem specifically is that I NEED for a dynamic write to happen based on a variable that will put the following lines that start with IF (! isset // Make sure the checkbox arrays exist if (! isset($_POST['cp_checkbox_one'])) $_POST['cp_checkbox_one'] = array(); if (! isset($_POST['cp_checkbox_two'])) $_POST['cp_checkbox_two'] = array(); if (! isset($_POST['cp_checkbox_three'])) $_POST['cp_checkbox_three'] = array(); // here in the lines below is one way I have tried to make the the LINES ABOVE get written Dynamically // THIS PART returns the values I need, the checkbox names ---- // // foreach($_POST as $meta_key => $meta_value) { // if(cp_str_starts_with($meta_key, 'cp_checkbox')) { echo $meta_key; } // } // So that code WILL RETURN THE VALUES I NEED : // cp_checkbox_one, cp_checkbox_two, cp_checkbox_three // BASED ON THIS I WROTE THE FOLLOWING CODE-- BUT IT DOES NOT WORK AS A REPLACEMENT FOR A LINE // SUCH AS--- if (! isset($_POST['cp_checkbox_one'])) $_POST['cp_checkbox_one'] = array(); // function checkboxloop() { foreach($_POST as $meta_key => $meta_value) { if(cp_str_starts_with($meta_key, 'cp_checkbox')) { if (! isset($_POST[$meta_key])) $_POST[$meta_key] = array(); } } } // HERE I ASK FOR THE FUNCTION BUT WHILE I GET NO ERRORS I GET NO RESULTS // WHICH IS A PROPER SAVE OF UNCHECKED CHECKBOXES ON SUBMIT checkboxloop; I would very much appreciate knowing how to make the code keep looping whatever $meta_key names turn up on this page and then use those names to write the complete lines as STATIC PHP so that the IF conditions don't get "processed" while this function is called. In effect, I want to duplicate the effect of having the code lines WRITTEN, so that they perform exactly as if I had written them as PHP hard-code, as many lines as there will be $meta_keys, but not SEE the code--- just have it valid PHP. This function does NOT make the same thing happen internally. As soon as I replace the function with the HARD CODE PHP then it works...but, I have reached the stage where these lines of code have to happen dynamically. I am baffled how to get this to happen! If I use Echo then I get errors about the "IF". Thank you very much for your help! I have a form that users input user name, password, and email... all writes to csv fine. My problem is I need to concatenate a a string with a variable and some html code will preserving the html when written to the csv.. I need the csv to be this username,password,email,user,category,text with http://sub.domain..com/splash/,something,something here's what i have Code: [Select] <?php if($_POST['formSubmit'] == "Submit") $varUserName = $_POST['username']; $varPW = $_POST['PW']; $varEmail = $_POST['email']; { $fs = fopen("testcsv.csv","a"); fputcsv($fs, array($varUserName,$varPW,$varEmail,"user","title",",category","some text '<a href="http://$varUserName.url.com/splash/>site.com</a>',)); fclose($fs); exit; } ?> and of course I'm getting Parse error: syntax error, unexpected T_STRING, expecting ')' on line 9 |