PHP - How To Get The Values Of The Array
Hi, I have this $_POST array coming from a paypal transaction
Code: [Select] Array ( [address_city] => San Jose [address_country] => United States [address_country_code] => US [address_name] => Test User [address_state] => CA [address_status] => confirmed [address_street] => 1 Main St [address_zip] => 95131 [business] => test1_biz@gmail.com [charset] => windows-1252 [custom] => [first_name] => Test [item_name1] => Product Name 1 [item_name2] => Product Name 6 [item_name3] => Product Name 8 [item_number1] => 1 [item_number2] => 6 [item_number3] => 8 [last_name] => User [mc_currency] => USD [mc_fee] => 0.82 [mc_gross] => 18.00 [mc_gross_1] => 14.00 [mc_gross_2] => 2.00 [mc_gross_3] => 2.00 [mc_handling] => 0.00 [mc_handling1] => 0.00 [mc_handling2] => 0.00 [mc_handling3] => 0.00 [mc_shipping] => 11.00 [mc_shipping1] => 11.00 [mc_shipping2] => 0.00 [mc_shipping3] => 0.00 [notify_version] => 3.4 [num_cart_items] => 3 [payer_email] => test_biz@gmail.com [payer_id] => TRCLJTHLNCJ7Q [payer_status] => verified [payment_date] => 22:52:56 Jan 27, 2012 PST [payment_fee] => 0.82 [payment_gross] => 18.00 [payment_status] => Completed [payment_type] => instant [protection_eligibility] => Eligible [quantity1] => 1 [quantity2] => 1 [quantity3] => 1 [receiver_email] => test_biz@gmail.com [receiver_id] => 74PV23B8KSK84 [residence_country] => US [tax] => 0.00 [tax1] => 0.00 [tax2] => 0.00 [tax3] => 0.00 [test_ipn] => 1 [transaction_subject] => Shopping CartProduct Name 1Product Name 6Product Name 8 [txn_id] => 7BS85664SB906284D [txn_type] => cart [verify_sign] => AJ2IuqHp8G0lIxhedAqrwRQbv8fVA4-Gum9e7DMZmEWIFrljEwFCJDfP ) 1 as you can see, there are key value pairs like this, Code: [Select] [items_name1] => Product Name 1 [items_name2] => Product Name 2 [items_name3] => Product Name 3 those keys aren't constant, I mean, it came from a shopping cart, so the item name depends on how many items were placed in the shopping cart to be purchased.. now my question is, How to loop to that post array, and get only the item_names plus their corresponding values ?, because I need them and save in a db table Similar TutorialsHi all, I'm a first time poster here and I would really appreciate some guidance with my latest php challenge! I've spent the entire day googling and reading and to be honest I think I'm really over my head and need the assistance of someone experienced to advise the best way to go! I have a multi dimensional array that looks like (see below); the array is created by CodeIgniter's database library (the rows returned from a select query) but I think this is a generic PHP question as opposed to having anything to do with CI because it related to working with arrays. I'm wondering how I might go about searching the array below for the key problem_id and a value equal to a variable which I would provide. Then, when it finds an array with a the matching key and variable, it outputs the other values in that part of the array too. For example, using the sample data below. How would you recommend that I search the array for all the arrays that have the key problem_id and the value 3 and then have it output the value of the key problem_update_date and the value of the key problem_update_text. Then keep searching to find the next occurrence? Thanks in advance, as above, I've been searching really hard for the answer and believe i'm over my head! Output of print_r($updates); CI_DB_mysql_result Object ( [conn_id] => Resource id #30 [result_id] => Resource id #35 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 5 [row_data] => ) Output of print_r($updates->result_array()); Array ( [0] => Array ( [problem_update_id] => 1 [problem_id] => 3 [problem_update_date] => 2010-10-01 [problem_update_text] => Some details about a paricular issue [problem_update_active] => 1 ) [1] => Array ( [problem_update_id] => 4 [problem_id] => 3 [problem_update_date] => 2010-10-01 [problem_update_text] => Another update about the problem with an ID of 3 [problem_update_active] => 1 ) [2] => Array ( [problem_update_id] => 5 [problem_id] => 4 [problem_update_date] => 2010-10-12 [problem_update_text] => An update about the problem with an ID of four [problem_update_active] => 1 ) [3] => Array ( [problem_update_id] => 6 [problem_id] => 4 [problem_update_date] => 2010-10-12 [problem_update_text] => An update about the problem with an ID of 6 [problem_update_active] => 1 ) [4] => Array ( [problem_update_id] => 7 [problem_id] => 3 [problem_update_date] => 2010-10-12 [problem_update_text] => Some new update about the problem with the ID of 3 [problem_update_active] => 1 ) ) Hello all, I have yet again trouble finding a logical solution to my problem. I'm fetching an array which can hold 1 or more values. The problem is, I want these values to ouput in my json_encode function, but this also needs to happen dynamically depending on the amount of values. I can't explain it further, so here's the code so far: Code: (php) [Select] $files = mysql_fetch_array($get_files); $a = count($files); $i = 1; while ($files) { $variablename = 'fileName' . $i; $$variablename = $files['fileName']; $i++; } $output = array( OTHER VALUES , 'fileName1' => $fileName1, 'fileName2' => $fileName2, 'fileName3' => $fileName3, ............); // How do I add the fileNames dynamically depending on how many there are? This got me thinking, I also need to dynamically GET the values with jQuery. How would I do that, when the above eventually works? Thank you. Hi there, Basic question, but I can't seem to find a page in the PHP docs that answers (though I'm sure such a one exists, I just can't seem to think up the right search terms - anyway) My question is: is there a short-hand way to use a value from an associative array as the key in another associative array? So for example, given this array: $the_array = array(0 => array('name': 'the_name', 'value' : 'the_value'), 1 => etc....); then this kind of foreach loop doesn't work Code: [Select] $new_data = array(); foreach($the_array as $element){ $new_data[$element['name']] = $element['value']; } Obviously I can just save out the values, like $name = $element['name'] --- but I was just wondering if there was a more efficient way to do it? Cheers, PS. I tried encapsulating with {} - i.e. $new_data[{$element['name']}] --- but that also fails. I have mysql select query shown below retrieving data from a database:
$sql = " SELECT student, sum( test_score ) as score FROM test INNER JOIN level ON test.level_id = level.level_id GROUP BY student" $result = $mysqli->query($sql, MYSQLI_STORE_RESULT); if($result) { while ($row = $result->fetch_array(MYSQLI_ASSOC)) { var_dump($row); } }On inspection of the retrieved array with var_dump(), i have these result below: array (size=2) 'John' => string 'English' (length=2) 'Score' => string '20' (length=3) array (size=2) 'Mary' => string 'Math' (length=3) 'Score' => string '35' (length=3) array (size=2) 'Samuel' => string 'Physics' (length=3) 'Score' => string '5' (length=3)How do I get at and print out the highest score from this set of array values within this context. I tried this echo max($row['count']) but i understandably got this feedback: Warning: max(): When only one parameter is given, it must be an array Thanks. I have an array created to control the down states of the my buttons in my navigation. Which is working well. This array will have quite a few strings in it before I'm done. I then want to conditionally load a CSS file for all the files in that array with the exception of two, is this possible? In plain in english I want to say If in array apart from these values then load my css file. My array (will have more values when complete) Code: [Select] $otherTitles = array('Product Selector', 'Avon Range', 'Knightsbridge Range', 'Kingston Range' ); My code to load a css file Code: [Select] <?php if (in_array($title, $otherTitles)) { ?> <link rel="stylesheet" type="text/css" href="homepage.css"/> <?php } ?> I want all titles in the otherTitles array to get this CSS file apart from two Product Selector and Avon Range. Thanks Richard I need to some how pull comma separated images $data[23] from this $data array and then put them into there own array so I can insert them into separate rows in a database. This is the provided array: $data[0] => VIN $data[1] => StockNumber ...(all the other number here) $data[23] => Image_URLs (comma seperated) $data[24] => Equipment // I need something like this (obviously doesn't work?!@#) $delimiter = ","; $img = explode($delimiter, $data[23]); foreach($img as $pic){ $sqlPic = "insert into class_prodimages (pid image rank) values('".$LastId['id']."', '$pic', '".rand(1,20)."')"; } Any help here? 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]); Hello there again!
I have the following construct of array now (schematics):
array[0] array[id] = "1" array[title] = "title" array[1] array[id] = "3" array[title] = "another title"Now what i want to do, is to assign the parent-array keys to the IDs of it's contents. This is what it should look like: array[1] array[id] = "1" array[title] = "title" array[3] array[id] = "3" array[title] = "another title"How do I do that again? I know I did this some years ago, but I can not remember how. How do you get the values in an array and name them as variables? Lets say I want the 0 and the 2... Array ( => 02.02.2011 [1] => 97% [2] => 538 [3] => 20 [4] => 63,247 (2.20) [5] => 77,639 [6] => 195,617 [7] => 09.13.2010 ) Basically I am pulling data from an XML feed on wowarmory.com Everything is working as far as getting the item names and even a list of the item levels. What I am trying to do now is add together all of the item levels in to one value that I will call $calc (I will then divide this by 16 to find the average item level). I am using a foreach loop to echo the Item Name and it's corresponding item level. I am now trying to grab the item level on each loop so I can add them all up and store them in $calc for further manipulation outside of the loop. I am a complete novice but learning, How would I go about achieving this? Of course the code I have atm is re-defining $calc every loop, so naturally it isn't working. Here is my current code: //Get the UID for each item the character has //Then look up the name for that item foreach($char_xml->characterInfo->characterTab->items->item as $item) { //get the item id $item_id = $item->attributes()->id; //get the xml doc for that item from wow armory $url = "http://www.wowarmory.com/item-info.xml?i=" . $item_id; $data = fetchXML($url); //create a SimpleXML object to parse the xml $item_xml = new SimpleXmlElement($data); //print the item's name echo "<b> Item Name: </b>" . $item_xml->itemInfo->item->attributes()->name . "</br>"; echo "<b><i> iLvL: </i></b>" . $item_xml->itemInfo->item->attributes()->level . "</br>"; //obviously this is wrong, because It is redefining $calc every loop $calc=$item_xml->itemInfo->item->attributes()->level; } //print the total item level echo $calc; I'm trying to split a url into segments so that a url would be stored in an array. Example: A url like this http://localhost/application/blog/view/Test-Blog-Entry/1 Would look like this in an array: Code: [Select] array('blog', 'post', 'Test-Blog-Entry', '1'); It's an MVC url so 'blog' would be controller, 'post', would be method and the last two would be variables. I want to display the variable values of the array. Is there an easy way to do this? I am working in wordpress and things are going good. I made a form, filled it with values and have been able to store those values in a database using a wordpress function. Now my issue is that I want learn how to write them into an array and store them that way. I have been looking at several different ways but can't figure out how to code it. This is my code now Code: [Select] function add_slider_box(){ global $post; $imageurl = get_post_meta( $post->ID, 'imageurl', true ); $imagelink = get_post_meta( $post->ID, 'imagelink', true ); $imagecap = get_post_meta( $post->ID, 'imagecap', true ); ?> <div style="width: 50%" class="sliderbox"> <p><label for="imageurl">Image Url: <br /> <input type="text" class="widefat" id="imageurl" name="imageurl" value="<?php if( $imageurl ) { echo $imageurl; } ?>" /></label></p> <p><label for="imagelink">Image Link: <br /> <input type="text" class="widefat" id="imagelink" name="imagelink" value="<?php if($imagelink) { echo $imagelink; }?>" /></label></p> <p><label for="imagecap">Image Caption: <br /> <input type="text" class="widefat" id="imagecap" name="imagecap" value="<?php if( $imagecap ) { echo $imagecap; } ?>" /></label></p> <button class="remove_slide button-secondary">Remove This Slide</button> </div> <?php } function bigBusiness_save_slider_options($post_id){ global $post; if( $_POST ) { update_post_meta( $post->ID, 'imageurl', $_POST['imageurl'] ); update_post_meta( $post->ID, 'imagelink', $_POST['imagelink']); update_post_meta( $post->ID, 'imagecap', $_POST['imagecap'] ); } } so the update_post_meta function is what is saving the information into the database. How do I put that into an array? I was thinking Code: [Select] foreach($_POST as $slides) $slides[] = array( 'imageurl' => '$_POST['imageurl'], 'imagelink' => $_POST['imagelink'], 'imagecap' => $_POST['imagecap']; ); I have been able to use the data out of the database by creating a function Code: [Select] function get_half_slider_slides() { global $post; $imageurl = get_post_meta( $post->ID, 'imageurl', true ); $imagelink = get_post_meta( $post->ID, 'imagelink', true ); $imagecap = get_post_meta( $post->ID, 'imageurl', true ); return array( $imageurl, $imagelink, $imagecap ); } and then calling that function in my code like this $slide = get_half_slider_slides(); I thought since it returned an array that I would be able to do this $slides['imagelink'] $slides['imageurl'] but it doesn't work. What I am trying to do is populate the nivo-slider ( http://nivo.dev7studios.com ) with images from the database. I have been following a tutorial but since the license on the tutorial doesn't cover reusing the code, I am trying to rewrite it in a way that doesn't violate the license agreement. This code is so far is hacked together but completely different from the tut. Can you help? Thanks Hi all, I'm trying to figure out how to update values I have in an array. I'm a bit stuck and hope someone can advise. I've read about all sorts of bits but I'm stuck, I dont really understand associative so thought I'd post an example of my code for someone to hopefully assist.. so, if I have a table $fees, which is filled from a select, for example.. select * from `cases_fees`. "contains fields for id, type, amount and others now I want to do is foreach through that table and update a field called `amount`. but I cant understand the foreach >= as key part. what is a key and what isn't ? (I know what the keys of the db table arem but that's not the same eh?) I'm fine with normal foreach, but I cant update back to the array so I need to find out how to be able to set valus back into $fees[`amount`] hope someone can point me in the right direction please... Ted Hi All, small question here. I was just playing around with a small script I made to obtain an ip-address, remove the dots and than glue the parts together. Not for any use but for practice. But I am having difficulties to accessing the array values. But maybe that because its the first time i am using explode() and print_r. If someone has a spare minute I would be pleased to be enlightened. <?php $ip=$_SERVER['REMOTE_ADDR']; echo $ip.'<br />'; $cleaned_ip = substr($ip,0,6); echo $cleaned_ip.'<br />'; $cleaned_ip2 = print_r(explode('.', $ip, 6)).'<br />'; // this is the point where I dont know how to get the array values. // echo $cleaned_ip2[0].[1].[2]; that doesnt work. ;( ?> Hey guys, I am having issue with understanding/implementing an array. Currently my code does the following it displays an array of values to the user as a note and allows the user to enter in values if they wish. This is done through using a select a statement which displays those values through a while loop.I then use a line like so:-<textarea name=Details[detailsno] value="'.$Text.'">"'.$Text.'"... which passes the input from the user into an array. Underneath this I have a checkbox that allows the user to select particular notes to update.<input type=checkbox name=update[] value=Update>. This passes over the values correctly. The problem im currently having is grabbing the values that the user has entered for those particular detailsno so that I can implement an insert statement. i was wondering if you knew how to do this? Thank you for taking your time to even read this or even help its much appreciated. Hi Just learning PHP and I'm struggling with a very simple problem. I'm playing around with arrays as i figure they're one of the most important things to know inside out but I can't work out how to change the value of an array element via a loop. I can obviously do it if I just directly write $cars[2] = "test" but not having any luck with the loop. I've tried using "foreach" and passing a new value to the $value variable but when i do a print_r the values in the array are unchanged. Code: [Select] $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; foreach ($cars as $key => $value) { $value = "TEST"; echo $key . " " . $value . "<br />"; } print_r($cars) I then tried using a for loop Code: [Select] for ($i=0; $i <= count($cars); $i++) { echo $cars[$i]; $cars[$i] = "2"; } But this code just threw up a fatal error as follows: "Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 16 bytes) in /home/zzzzzzzz/public_html/php_testing/arrays.php on line 27" So would someone be kind enough to explain where i'm going wrong and why i get the fatal error on the second for loop. Thanks Drongo Hello, I am having some trouble with calling certain array values while a condition equals something. I have called values from a database table into an array: $result = mysql_query("SELECT `TeamID`, `ScoreA`, `ScoreB`,`TeamPlayed`, `RoundNum` FROM `scores` WHERE `CompID` = 4 ORDER BY `RoundNum` ASC") or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { print_r($row); } Code: [Select] Array ( [TeamID] => 3 [ScoreA] => 16 [ScoreB] => 4 [TeamPlayed] => 4 [RoundNum] => 1 ) Array ( [TeamID] => 1 [ScoreA] => 16 [ScoreB] => 1 [TeamPlayed] => 6 [RoundNum] => 1 ) Array ( [TeamID] => 2 [ScoreA] => 16 [ScoreB] => 14 [TeamPlayed] => 5 [RoundNum] => 1 ) Array ( [TeamID] => 4 [ScoreA] => 16 [ScoreB] => 0 [TeamPlayed] => 6 [RoundNum] => 2 ) Array ( [TeamID] => 1 [ScoreA] => 10 [ScoreB] => 16 [TeamPlayed] => 2 [RoundNum] => 2 ) Array ( [TeamID] => 5 [ScoreA] => 14 [ScoreB] => 16 [TeamPlayed] => 3 [RoundNum] => 2 ) From here I want to able to get display certain values while RoundNum = $value So while RoundNum = 1 display the array values of: Code: [Select] TeamID V TeamPlayed ScoreA - ScoreB Hope you can decipher what I am asking, don't think I have worded it very well :S. Thanks for anyhelp! I want to store... - questionID - questionNo - questionText ...in one array? How do I do that?! Debbie Hi, i have a password array like below and i was wondering if i could have a script to add values to the bottom of it in the syntax below. Also, could i fit a corresponding email address in there somewhere too. Thanks! <?php //Secure PHP login withou Database by Jerry Low @crankberryblog.com //Find other useful scripts at the Crankberry Blog //Installation Guide At: //http://www.crankberryblog.com/2009/secure-php-login-without-database //Users and Settings $domain_code = 'asdkb767'; //Alpha Numeric and no space $random_num_1 = 83; //Pick a random number between 1 to 500 $random_num_2 = 709; //Pick a random number between 500 to 1000 $random_num_3 = 2; //Pick a random number between 1 to 3 //Usernames can contain alphabets, numbers, hyphens and underscore only //Set users below - Just add '' => '' with the first '' being //the username and the second '' after the => being the password. //Its an array so add an , after every password except for the //last one in the list. As shown below //Eg. $users = array( // 'user1' => 'password', // 'user2' => 'password' // ); $users = array( 'user1' => 'test', 'user2' => 'test' ); ?> Hello I have been taking a look at associative arrays. I 'sort' of get the key/value idea, but I'm not quite sure if they appropriate for use with 3 items (as opposed to the 2 key/value items.) In the code below, I can incorporate the 'company' and 'table' values - but I'm not sure how to add the 'id' value (from the query). I would very much appreciate some guidance here. Thank you. Code: [Select] $a = array(); // 'A' table array $b = array(); // 'B' table array $c = array(); // 'C' table array $query = "SELECT id, company, `table` FROM EXHIBITORS WHERE year = $year ORDER BY company ASC"; $result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) { $testCol = substr($row['table'], 0, 1); if ($testCol == 'A') { $a[$row['company']] = $row['table']; } else if ($testCol == 'B') { $b[$row['company']] = $row['table']; } else if ($testCol == 'C') { $c[$row['company']] = $row['table']; } } echo "<table>"; foreach($a as $key => $value){ echo "<tr><td>company: $key</td><td>Table: $value </td></tr>"; } echo "</table>"; echo "<table>"; foreach($b as $key => $value){ echo "<tr><td>company: $key</td><td'>Table: $value </td></tr>"; } echo "</table>"; echo "<table>"; foreach($c as $key => $value){ echo "<tr>"<td>company: $key</td><td>Table: $value </td></tr>"; } echo "</table>"; } else { echo "No Exhibitor data for the year $_POST[selectByYear]"; } |