PHP - Foreach To Access Specific Spot In A 2d Array
Hey all. another quick question.
here is the output of my 2D array: Code: [Select] array(2) { ["folder.jpg"]=> array(2) { ["valid"]=> bool(true) ["error"]=> bool(false) } ["file.mp3"]=> array(2) { ["valid"]=> bool(true) ["error"]=> bool(false) } } how can i foreach my way into getting an iterator that points to the folder.jpg and file.mp3? as is now, i have something that looks like this: foreach($arr as $val){ var_dump($val) } the var_dump of $val is an *array*. Thanks for your help! Similar TutorialsI am trying in vain to access the numbers in the "sims" array using the code below, I have tried various numbers of nested foreach loops but get errors every time, can anyone help point me in the correct direction please.
Thanks
Gordon
$stock = (array(1) { ["stock"]=> array(2) { [0]=> array(2) { ["operator"]=> string(3) "ECL" ["sims"]=> array(51) { [0]=> int(8944122650438410000) [1]=> int(8944122650438409000) [2]=> int(8944122650438409000) } } [1]=> array(2) { ["operator"]=> string(2) "JT" ["sims"]=> NULL } } } ) foreach ($stock as $key1 => $item1) { foreach($item1 as $key2 => $item2) { foreach($item2 as $key => $value) { echo $value[sims] . "</br>"; } } } Hello first time poster here . Soni have been in the proccess of designing a website that would give images to users . But only owner of an image will get thier own image . And some people may not access thier image whom are invalid untill i make them valid users. Si.my problem is i want to stop people from accessing these images by typing thier mysite.com/path and these files only be accessable via a php that is in my website . How do i go about doing that . Is it iam my new to this or there is not a convenient way to do this . Thanks in advance Hi guys, in my database i have the table called users, where i have 5 fields (id, username, email, password, user_level) - for the user_level field i have 2 options administrator and editor.
What i want to do is that when the user who is logged in have administrator in the user_level field to see all the pages from backend, and the user who have in the user_level field editor to see only some of the pages from the backend such as newsletter, or messages.
I hope you understand what i'm asking if not fell free to ask me if you need more specific details.
I tried to make a php page called access.php wher i put the following code, but not working
<?php session_start(); $sql = $mysqli->query("SELECT user_level FROM imobiliare_users WHERE id=$id"); $user_level = $mysqli->query($sql); echo $user_level; if ($user_level !="administrator") { echo "You are not the proper user type to view this page"; die(); } ?>Hope you can help me. Thx in advance for help. I'm not sure if the title explains it very well, but here is an image of it. I am trying to figure out how to access the option selected (equal, not equal, etc.), along with its input box, for each column (the checkboxes on the right) selected. The purpose of this is to allow a non-programming administrator user to create a custom query to access information. I can find out which checkboxes are selected no problem, but finding the accompanying drop-down option and input box is difficult for me. How do I do this? If you have a suggestion on how to change it, I'm very open. Thank you in advance! Code: [Select] if(!isset($_POST['submit'])) { echo " <form method='post'> <table> <tr> <td valign='top'>SELECT</td> <td valign='top'> <table>"; // LIST ALL COLUMNS IN A TABLE $result = mysql_query("SELECT * FROM table_name") or die(mysql_error()); $rowcount=mysql_num_rows($result); $y=mysql_num_fields($result); for ($x=0; $x<$y; $x++) { echo " <tr> <td> <input type='checkbox' name='fields[]' value='".mysql_field_name($result, $x)."'>".mysql_field_name($result, $x)." </td> </tr>"; } echo " </table> </td> <td valign='top'>FROM allocations</td> <td valign='top'>WHERE</td> <td> <table>"; // LIST ALL COLUMNS IN A TABLE $result = mysql_query("SELECT * FROM table_name") or die(mysql_error()); $rowcount=mysql_num_rows($result); $y=mysql_num_fields($result); for ($x=0; $x<$y; $x++) { echo " <tr> <td> <input type='checkbox' name='column[]' value='".mysql_field_name($result, $x)."'>".mysql_field_name($result, $x)." </td> <td> <select name='operator[]'> <option value='='>equals</option> <option value='<>'>does not equal</option> <option value='>'>greater than</option> <option value='<'>less than</option> <option value='>='>greater than or equal to</option> <option value='<='>less than or equal to</option> <option value='LIKE'>is like</option> </select> </td> <td><input type='text' name='criteria[]'></td> </tr>"; } echo " </table> </td> <td valign='top'><input type='submit' value='Process Query' name='submit' class='submit'></td> </tr> </table> </form>"; } else { echo "<b>Fields to edit:</b> "; foreach ($_POST['fields'] as $fields) { echo $fields,", "; } echo "<br><br>"; echo "<b>Columns to query:</b> "; foreach ($_POST['column'] as $columns) { echo $columns," HERE IS THE SPOT, "; } } 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]); The title makes it sound more complicated than it is. I have a recurring function that searches for a specific value in a multi-dimensional array. When it finds it, it returns an array of the index. For example, if it found the value at $array[1][4][2][0], it would return an array like this: [0]=>1 [1]=>4 [2]=>2 [3]=>0 Here's the function. Code: [Select] <?php // Other Code /** * Find's the parent of an item * Unfortunately, it takes a bit more work than just using the loudspeaker at a grocery store * @param int $parent The parent to be searched for * @param array $array * @param array $cur_index */ private function findParent($parent, $array, $cur_index) { for($i = 0; $i < count($array); $i++) { // Search the array if($array[$i]['id'] == $parent) { // If the parent is found $cur_index[count($cur_index)] = $i; return array(true, $cur_index); } if(count($array[$i][1]) > 1) { // Call findParent again to search the child $cur_index[count($cur_index)] = $i; $found_in_child = $this->findParent($parent, $array[$i], $cur_index); if($found_in_child[0]) { return $found_in_child; } } } return array(false); // Return no matches } I need to be able to call that index using the keys from the key array (as we will call it). How can I do that? Thank you in advance for your help. Array ( [game] => Array ( [id] => 2011012593950636050 [name] => Array ( [us] => Castlevania II: Belmont's Revenge [uk] => Castlevania II: Belmont's Revenge ) echo print_r($this->game[0]['name']) but undefined offset error. I don't want to loop through the first array either. Thanks When I send values to the foreach they are not recorded in the array. $x = $getProductID; $_SESSION['pColors'] = array(); if(isset($_POST['pColors'])) { foreach($_POST['pColors'] as $colorKey => $RColors) { $_SESSION['pColors'][$colorKey] = $RColors; } } var_dump($_SESSION['pColors']); Send date with AJAX: $('.addToCart').click(function(){ $.ajax({ url:""+realLink+"cart.php", method:"POST", data:{action:action,pColors:pColors}, success:function(data){ //alert(data); } }); }); I got this already, my problem is I can't get all values of my $prog array because of '=>' operator. Is there any way to get rid of this? Code: [Select] foreach ($_POST['course'] as $val =>$course) { $prog = $_POST['program'][$val] ; echo $course; echo $prog; } What I' trying to do is take my array Code: [Select] $stores = array("name"=>"$MktName","address"=>"$address"); And for each each value, insert it into my db Code: [Select] foreach($stores as $k => $v){ mysql_query("INSERT INTO stores (name, address) VALUES('$k', '$v' ) ") or die(mysql_error()); } Now, I'm just learning more about arrays, and this is obviously wrong How do I go about getting the value of "name"? It pulls the value for "address". Thanks in advance Hi, I have an array: Code: [Select] Array ( [0] => index.php [1] => Home [2] => page.php?id=23 [3] => Page ) How would I loop through it so I can link the previous value to the next value within the array e.g.: Code: [Select] <a href="index.php">Home</a> <a href="page.php?id=23">Page</a> I have the following foreach statement that gets the error msg's from the array called errors. However for the last key in the array, i would not like the <br /> to be echo'd out. How can I achieve that? Code: [Select] foreach ($errors as $msg) { echo $msg; echo "<br />"; } Hi guys,
Having trouble trying to show values from my $friend array. Only the first friend code is being displayed in my output.
There are several fields in my mysql associated with the $sql.
Any ideas?
Thanks
$sql = "SELECT * FROM friends WHERE Code = '$code' OR FriendCode = '$code' AND Status ='A'"; $result = mysqli_query($cxn,$sql) or die ("Can't get friends."); $friend = array(); while($row=mysqli_fetch_array($result)) { if($code == $row['FriendCode']) { $friend[] = $row['Code']; } elseif($code == $row['Code']) { $friend[] = $row['FriendCode']; } foreach($friend as $key => $value) { echo $value.'<br />'; } Edited by unistake, 18 June 2014 - 06:13 AM. what am I doing wrong? I have an assoc array, but I can't seem to access it. Code: [Select] $var = Array ( 'domain' => asdfasdf.com 'port' => 2082 'adminuser' => admin 'INST_password' => adsfasdf 'adminnickname' => admin 'adminemail' => admin@asdfasdf.com 'sitename' => Play Bingo Online Austrailia 'description' => How to play bingo online in Australia 'site_root_path' => /home/chris/public_html/ 'theme' => default 'anchor_one' => How to play bingo online in Australia 'anchor_two' => play bingo 'anchor_three' => bingo online ) if( empty($var['domain']) ) exit; the code exits everytime. What am I missing? I am trying to pull images from my Flickr account with some success except that of accessing the description to be used as a caption for the image. What I have so far is:- Code: [Select] <?php //>>> Define some variables $maximgs = 5; $images = array(); $params = array( 'api_key' => '***********************', 'method' => 'flickr.photos.search', 'user_id' => '****************', 'sort' => 'date-posted-desc', 'format' => 'php_serial', 'extras' => 'description', ); $encoded_params = array(); foreach ($params as $key => $value){ $encoded_params[] = urlencode($key) . '=' . urlencode($value); } //>>> Make request and decode the response $url = 'http://api.flickr.com/services/rest/?' . implode('&', $encoded_params); $response = file_get_contents($url); $response_object = unserialize($response); //var_dump($response_object['photos']['photo']); //>>> Why is this returning 100 results? if($response_object['stat'] !== 'ok'){ echo "Something went wrong!"; } //>>> Select the required number of images ($maximgs) for($loop = 0; $loop <= $maximgs; $loop++){ $images = $response_object['photos']['photo'][$loop]; var_dump($images); //>>> Url of the individual image location for the img src. $imgurl = 'http://farm'.$images['farm'].'.'.'static'.'.'.'flickr.com/'. $images['server'].'/'.$images['id'].'_'.$images['secret'].'_n.jpg'; //>>> Url of the individual image for the link. $flkrurl = 'http://www.flickr.com/photos/'.$images['owner'].'/'.$images['id'].'/'; /*The complete Url (<a href="link($flkrurl)"> <img alt="$images['title']" src="image location"($imgurl) width="required width of image" /> </a>) */ $compurl = '<a href="'.$flkrurl.'" target="_blank">'.'<img alt="'. $images['title'].'" src="'.$imgurl.'" width="320px" /></a>'; //>>> Display Images echo $compurl . '<br />' . $images['description']['content']; //<<<<<< Line 66 } ?> Which annoyingly throws this error:- Notice: Undefined index: content in C:\wamp\www\php\flickr_test1.php on line 66 The result of a var_dump():- array 'id' => string '6914498038' (length=10) 'owner' => string '************' (length=12) 'secret' => string '601a885f31' (length=10) 'server' => string '7137' (length=4) 'farm' => float 8 'title' => string 'newhaven_15' (length=11) 'ispublic' => int 1 'isfriend' => int 0 'isfamily' => int 0 'description' => array '_content' => string 'Great reflections found in Newhaven Harbour.' (length=44) with the info that I want to extract underlined. What I can't figure out is how to find out the name of the array below "description =>". I have been scratching my head for several hours now and am no nearer to working it out. Maybe a genius here will have the solution. How would you access the number 10 in the $arr array? I've never worked with multidimensional arrays. I have developed a calendar app. Right now i have it query the db for every day. So potentially 31 queries. Two part question: Would it be better(more "optimized") to query the db once to get all the events in that month for a certain calendar and put the rows into a multidimensional array and use php to determine which events to put in what days? And if that's the case, how do you cycle through the first key in an array like the following? I'm not even sure if this is the best way to structure the array. The number's represent the id number in the db and i will need to cycle through these. Code: [Select] $events = array ( "1"=>array ( "Bob's Birthday", "2011-05-24 16:30:0", "2011-05-25 12:00:00", ), "2"=>array ( "Day Off", "2011-05-27 08:30:0", "2011-05-28 17:00:00", ), "3"=>array ( "Pay rent", "2011-06-01 08:00:0", "2011-05-01 17:00:00", ) ); //something like this...maybe? foreach($events['first_key'] as $key => $value) { // not a clue how to reference the contents } I am a beginner, so forgive me all to whom this is trivial... I have an array, generated from simpleXML object, trying to parse... The array is not symmetrical & I am having trouble finding the right code to make it work. Help & comments are welcomed.. Here is the raw array with print_r: Code: [Select] Array ( [Area] => Array ( [0] => Array ( [Name] => Basement [Id] => 1 [Room] => Array ( [0] => Array ( [Name] => Equipment [Id] => 1 ) [1] => Array ( [Name] => Theater [Id] => 9 ) [2] => Array ( [Name] => Wine Cellar [Id] => 10 ) ) ) [1] => Array ( [Name] => 1st Floor [Id] => 2 [Room] => Array ( [0] => Array ( [Name] => Kitchen [Id] => 2 ) [1] => Array ( [Name] => Main Hall [Id] => 4 ) [2] => Array ( [Name] => Foyer [Id] => 8 ) [3] => Array ( [Name] => Stone Room [Id] => 14 ) [4] => Array ( [Name] => Garage Foyer [Id] => 16 ) [5] => Array ( [Name] => Dining Room [Id] => 17 ) [6] => Array ( [Name] => Living Room [Id] => 18 ) [7] => Array ( [Name] => Hearth Room [Id] => 19 ) [8] => Array ( [Name] => Office [Id] => 20 ) [9] => Array ( [Name] => Powder Room [Id] => 21 ) ) ) [2] => Array ( [Name] => 2nd Floor [Id] => 3 [Room] => Array ( [0] => Array ( [Name] => Landing [Id] => 7 ) [1] => Array ( [Name] => Master Bedroom [Id] => 11 ) [2] => Array ( [Name] => Nannys Room [Id] => 13 ) [3] => Array ( [Name] => East Kids Bed [Id] => 27 ) [4] => Array ( [Name] => West Kids Bed [Id] => 28 ) [5] => Array ( [Name] => Kids Bath [Id] => 29 ) [6] => Array ( [Name] => Robs Closet [Id] => 31 ) [7] => Array ( [Name] => Kelli's Office [Id] => 32 ) ) ) [3] => Array ( [Name] => 3rd Floor [Id] => 7 [Room] => Array ( [Name] => Bonus room [Id] => 5 ) ) [4] => Array ( [Name] => Outside Areas [Id] => 8 [Room] => Array ( [0] => Array ( [Name] => Veranda [Id] => 22 ) [1] => Array ( [Name] => Back Outdoor [Id] => 23 ) [2] => Array ( [Name] => Christmas Lights [Id] => 33 ) [3] => Array ( [Name] => Front Outdoor [Id] => 34 ) [4] => Array ( [Name] => Garage [Id] => 35 ) [5] => Array ( [Name] => Poolhouse Kitchen [Id] => 24 ) [6] => Array ( [Name] => Poolhouse Utility [Id] => 25 ) ) ) ) ) Here is the code: Code: [Select] foreach($xml2['Area'] as $v){ echo $v['Name'],' <br>'; foreach($v['Room'] as $v2){ echo ' ',$v2['Name'],' <br>'; } } Here is the result: Code: [Select] Basement Equipment Theater Wine Cellar 1st Floor Kitchen Main Hall Foyer Stone Room Garage Foyer Dining Room Living Room Hearth Room Office Powder Room 2nd Floor Landing Master Bedroom Nannys Room East Kids Bed West Kids Bed Kids Bath Robs Closet Kelli's Office 3rd Floor B 5 Outside Areas Veranda Back Outdoor Christmas Lights Front Outdoor Garage Poolhouse Kitchen Poolhouse Utility The problem lies with the "3rd Floor" which should contain "Bonus Room" as the room. I know that this is the only element that has no "Rooms" array, but I can't figure out how to code the trap for it. With this data, the condition could exist anywhere, not just my example, but I am stumped with the non symmetrical array that I am unfamiliar with. Any help? Thanks. --akaweed |