PHP - Check If In Array But Not Include Some Values In The Array
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 Similar TutorialsIs there any simple way to check if all values in an array are same with an simple if condition Something like if( ...some code...) { //yes, all values are same } else { //nope, all are not same } my PHP Version is 5.2.9-2 Any help would be appreciated. Basically, i want to check if values in an array are equals to 1. After doing a mysql_query and pulling data into an array like: Code: [Select] $polquery = mysql_query("SELECT * FROM disclosures WHERE transid = '$transid'"); while($something = mysql_fetch_array($polquery){ $status = $something['complete']; } I need to cycle through the array $status to see if all values = "accept", I'm not sure how to do this... or just forgot. Maybe just do a while($status == "accept")? 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. 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. 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 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. Hi guys, Could someone help me please? How do I check the following data foreach [id] and check to see if it the whole array has a certain ID in it? I can access individual data like this: $array['data'][0]['id']; but I'm not sure of the best way to check the array for a particular ID. Any ideas? Thanks, RJP1 Code: [Select] Array ( [data] => Array ( [0] => Array ( [name] => sdajskdhsajd [category] => Application [id] => 323635478887920 [created_time] => 878978978 ) [1] => Array ( [name] => askdjakdjasd [category] => Application [id] => 36741566509099181 [created_time] => 2011-02-11T15:41:14+0000 ) [2] => Array ( [name] => asdasdasdasds [category] => Interest [id] => 1150572090878508639 [created_time] => 2011-02-11T15:38:40+0000 ) Hi 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 ) ) @requinix, In an earlier thread of mine this week, I think you - or maybe it was @Psycho - suggested putting my code which create an array of my photo files into an include file. I think the logic was that then when either my "photo-gallery.php" OR "photo-detils.php" scripts call that code, that the array will always synched because it is the same array. Ring a bell? So super newbie question, but I am rusty on includes... How does this sound...
1.) Create an "/includes/retrieve-photos.php" script?
2.) Reference that in each script, I would do this...
require_once('../../includes/retrieve-photos.php
right?
3.) To use that code, I would then just reference the populated array...
foreach($photoFiles as $photoKey => $photoValue){ }
Right?
Here is a section of php file A include_once(dirname(__FILE__) . '/PhpfileB.php') $VALID_RATING_IDS = $Ratethese Here is all of php file B $Ratethese = "array("1", "2",);" I need the var $VALID_RATING_IDS to = $Ratethese from file B I am trying to include an array from php file B in php file A. Is this possible? How can i accomplish this? Hi all, I'm at a dead-end here. I have a php file that I can query to return an array containing a bunch of data about a job listing. I include and query it by calling it like so within another file on my site. Quick Notes: The original file is in a different directory on my site than the page it's being included in. I have tried returning the array at the end of the script. I can print_r the array on the page I'm including the script on (so the URL path is correct). If I go to this URL directly and print_r on the array, I see the entire array as expected. Also, if I include the file on page 2 like I did above with print_r on the array in the original file, I see the array printed out on page 2. Code: [Select] include 'http://www.example.com/php/job-details.php?job=jobname&city=thecity'; However, if I do not print_r on the array in the original file and just include it on page 2, I cannot do anything with it and the array variable isn't found. Is this a case of variable scope? I'm so frustrated... Here is the code I have in my original file: Code: [Select] <? include('functions.php'); $jobtitlematch = $_GET["job"]; $jobcitymatch = $_GET["city"]; //echo $jobtitlematch; //echo $jobcitymatch; $url = 'http://somesite.com/something'; $xml = simplexml_load_file($url); foreach($xml->job as $job) { $jobtitle = makeURL((string)$job->title); $jobcity = makeURL((string)$job->region); if ($jobtitle == $jobtitlematch && $jobcity == $jobcitymatch) { $jobdata[] = array( Title => ((string)$job->title), URL_Title => makeURL((string)$job->title), Location => ((string)$job->region), URL_Location => makeURL((string)$job->region), Department => ((string)$job->department), URL_Department => makeURL((string)$job->department), Overview => ((string)$job->joboverview), Responsibilities => ((string)$job->responsibilities), Qualifications => ((string)$job->qualifications), Keywords => ((string)$job->metakeywords) ); } return $jobdata; //I have also tried return global $jobdata; } print_r($jobdata); ?> Thanks in advance for any help you can provide... Hi all! I'm developing a crude mailing list thingymajig (completely legit, I promise!!). At the moment, I have single email subscription in way of a single field form. The email is entered, checked against the database, and subscribed if it is not there. If it is there, an alert is displayed saying 'email already subscribed'. Great. Now I'm taking things further with a text field and coding hich can extract email addresses from a block of text. No, this isn't so I can harvest email addresses illegitimately, it's because I get asked to add chunks of addresses at a time, and at the moment I have to do this manually, one-by-one. I have the code working to extract the email addresses from the text to an array, but now I'm stuck at the duplication check. I think what I'm asking is: how would it be possible to check each address in the array against the database and highlight any duplicates (I'll ultimately be looking to colour the duplicates and new addresses differently, show the number of new addresses to be added, and have a final 'do it' button which will add them...but I can get there on my own once I learn how to check for dupes!) Hopefully someone can give me a nudge in the right direction Many thanks!! For example for my Skill Tree:
array (size=9) 0 => array (size=2) 'Skill_Id' => string '1' (length=1) 'Skill_Points' => string '0' (length=1) 1 => array (size=2) 'Skill_Id' => string '2' (length=1) 'Skill_Points' => string '0' (length=1) 2 => array (size=2) 'Skill_Id' => string '3' (length=1) 'Skill_Points' => string '0' (length=1) 3 => array (size=2) 'Skill_Id' => string '4' (length=1) 'Skill_Points' => string '1' (length=1) 4 => array (size=2) 'Skill_Id' => string '5' (length=1) 'Skill_Points' => string '1' (length=1) 5 => array (size=2) 'Skill_Id' => string '6' (length=1) 'Skill_Points' => string '1' (length=1) 6 => array (size=2) 'Skill_Id' => string '7' (length=1) 'Skill_Points' => string '0' (length=1) 7 => array (size=2) 'Skill_Id' => string '8' (length=1) 'Skill_Points' => string '1' (length=1) 8 => array (size=2) 'Skill_Id' => string '9' (length=1)For the Skill_Id, it must be in order when checking server side. 1 through 9. (or 9 can be changed by me whenever). So, I have: $skillrange = range(1,$amountofskills); Which will output: array (size=9) 0 => int 1 1 => int 2 2 => int 3 3 => int 4 4 => int 5 5 => int 6 6 => int 7 7 => int 8 8 => int 9But, how do I check this order from my original array? I need to check My Skill_ID values in order from my original array and make sure they are 1 through 9. (So a user cannot tamper the data) Edit: I've come up with this solution since making this thread: $array1 = $skillidarray; $array2 = $skillrange; if (!empty(array_diff_assoc($array1, $array2))){ echo "Your skill tree data is out of order, please report this to an administrator."; }Something like this would work right? Edited by Monkuar, 11 October 2014 - 03:59 PM. I would like to check multiple rows from array with elseif. It's working if i add it manually, but i would like it to check all of the rows in a single elseif. Code(First if is nothing, just a example). Row is a array filled with numbers like: 3-3, 2-2 etc. Code: [Select] if("2-2" == $row["0"]){ echo BLUE; }elseif($dnumber == $row["0"]){ echo RED; }else{ echo WHITE; } Hey all. I have a page that dynamically creates form input fields called "menuitem[]". So on the page that checks the values I need to check if any of those fields were not filed in. My syntax is wrong any help would be great. test.php foreach($_POST['menuitem'] as $key => $value) { if(!isset($value) || $value = "") { echo "Please fill in all values. You left key " . $key . "empty."; } } Nothing gets echoed when I leave the fields empty so I'm doing something wrong here. Can someone please help me figure this out: I have a normal text file with 3 lines in it, every line contain one date i.e 12112011 (12.11-2011). I put the content of that file in an array, and want to compair the dates in the text file against the date generated from the loop. Problem is, i can only get a match for the last date listed in the text file no matter what.. If i have 4 dates in the file, it only style the last date. Thanks! --- CODE SNIPPET --- $day_num = 1; $days_in_month = 30; $month = 11; $year = 2011; //Access the file containing already taken dates and load them in an array: $date_array = file("test.txt"); //count up the days, untill we've done all of them in the month and check them against the dates in the text file: while ($day_num <= $days_in_month) { // Set the variable $date to be checked against the content of the array: $date = $day_num . $month . $year; if (in_array($date, $date_array)) { echo "<td class='color_green'>$day_num</td>"; //print_r($date_array); } else { echo "<td>$day_num</td><br />"; } $day_num++; } I am trying to compare an array containing required questions to an array holding survey responses. When a user submits a survey, I take the $_POST array and write the values to a new array called $surveyResponseArray. How can I determine if $surveyResponseArray exists?
Edited February 16 by SaranacLake I have the form / script below that works to add multiple rows of data with one query. One thing I did not think about is what if the first set of form fields are filled and the second is not. Currently the script insert an empty row. I'm sure how I would go about checking for an empty row and if there is an empty row ignore it and do not put it in the DB. Can someone point me in the right direction? Code: [Select] <?php // Begin the script for this page if (isset($_POST['submit'])) { //Assign each array to a variable $id = $_POST['id']; $store = $_POST['store']; $item = $_POST['item']; $itemprice = $_POST['itemprice']; $itemnumber = $_POST['itemnumber']; $couponvalue = $_POST['couponvalue']; $couponsused = $_POST['couponsused']; $limit = count($id); $values = array(); // initialize an empty array to hold the values for($i=0;$i<$limit;$i++){ $store[$i] = check_input($store[$i]); $item[$i] = check_input($item[$i]); $itemprice[$i] = check_input($itemprice[$i]); $itemnumber[$i] = check_input($itemnumber[$i]); $couponvalue[$i] = check_input($couponvalue[$i]); $couponsused[$i] = check_input($couponsused[$i]); $values[$i] = "( '{$id[$i]}', '{$store[$i]}', '{$item[$i]}', '{$itemprice[$i]}', '{$itemnumber[$i]}', '{$couponvalue[$i]}', '{$couponsused[$i]}')"; // build the array of values for the query string } $query = "INSERT INTO `item` (user_id, store, item, itemprice, itemnumber, couponvalue, couponsused) VALUES " . implode( ', ', $values ); // Form the query string and add the implod()ed values if (!mysql_query($query,$link)){ die('Error: ' . mysql_error()); } else { $added = "Your items have been added."; } } ?> <div class="pageContent"> <div id="main"> <div class="container"> <?php echo $added; if($_SESSION['id']){ echo '<form action="" method="post">'; echo '<table cellpadding= "4">'; echo '<tr>'; echo '<input type="hidden" name="id[]" id="id[]" value='.$_SESSION['id'].' />'; echo '<td><DIV CLASS="p2"><b>Store</b></div>'; echo '<input type="text" name="store[]" id="store[]" size="15" maxlength="255"/></td>'; echo '<td><DIV CLASS="p2"><b>Item</b></div>'; echo '<input type="text" name="item[]" id="item[]" size="15" maxlength="255"/></td>'; echo '<td><DIV CLASS="p2"><b>Item Price</b></div>'; echo '<input type="text" name="itemprice[]" id="itemprice[]" size="5" maxlength="255"/></td>'; echo '<td><DIV CLASS="p2"><b>Item #</b></div>'; echo '<select name="itemnumber[]" id="itemnumber[]"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option></select></td>'; echo '<td><DIV CLASS="p2"><b>Coupon Value</b></div>'; echo '<input type="text" name="couponvalue[]" id="couponvalue[]" size="10" maxlength="255"/></td>'; echo '<td><DIV CLASS="p2"><b># Coupons</b></div>'; echo '<select style="width: 60px;" name="couponsused[]" id="couponsused[]"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option></select></td>'; echo '<td><DIV CLASS="p2"><b>Doubled Coupon</b></div>'; echo '<select style="width: 75px;" name="doubledcoupon[]" id="doubledcoupon[]"><option value="1">Yes</option><option value="0">No</option></td>'; echo '</tr>'; echo '<tr>'; echo '<input type="hidden" name="id[]" id="id[]" value="'.$_SESSION['id'].'" />'; echo '<td><input type="text" name="store[]" id="store[]" size="15" maxlength="255"/></td>'; echo '<td><input type="text" name="item[]" id="item[]" size="15" maxlength="255"/></td>'; echo '<td><input type="text" name="itemprice[]" id="itemprice[]" size="5" maxlength="255"/></td>'; echo '<td><select name="itemnumber[]" id="itemnumber[]"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option></select></td>'; echo '<td><input type="text" name="couponvalue[]" id="couponvalue[]" size="10" maxlength="255"/></td>'; echo '<td><select style="width: 60px;" name="couponsused[]" id="couponsused[]"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option></select></td>'; echo '<td><select style="width: 75px;" name="doubledcoupon[]" id="doubledcoupon[]"><option value="1">Yes</option><option value="0">No</option></td>'; echo'</tr>'; echo'<tr>'; echo '<input type="submit" name="submit" value="Submit Item">'; echo'</tr>'; echo '</table>'; echo'</form>'; } else { echo '<h1>Please, <a href="index.php">login</a> and come back later!</h1>'; } ?> </div> </div> </div> |