PHP - Multi Dimension Array : Need Help
i want to edit a product and allocate industries to a product refer following link to view http://meriwebsite.net/envytech/admin/industryallocate.php?pageNum=1SELECT%20a.products_id,%20a.products_image,%20b.language_id,%20b.products_description,%20b.products_name%20%20%20FROM%20products%20a%20,%20products_description%20b%20WHERE%20a.products_id%20=%20b.products_id%20and%20language_id=1%20and%201%20order%20by%20%20b.products_id,%20%20b.products_description I can update prouct name and serial number but cannot get values for industries the do while loop for industry display is given below (displaying industry id before check box here) $iquery="SELECT * from industry "; mysql_select_db($database_DBconnect, $DBconnect); $iResult = mysql_query($iquery, $DBconnect) or die(mysql_error()); $irows = mysql_fetch_assoc($iResult); $itotalrows=mysql_num_rows($iResult); $j=-1; do { $j++; ?> <br /><?php echo "id=". $irows['id'];?><input name="ind[$i][$j]" type="checkbox" class="vardana12" id="ind[$i][$j]" value="<?php echo $irows['id'];?>" /> <?php echo $irows['name'];?> <?php } while ( $irows = mysql_fetch_assoc($iResult) ); ?> I am trying to display values using following code if ( (isset($_POST['sub'] )) or (isset($srno['sub'] ))) { echo "<br> count=".count($_POST['srno']); for($i=0; $i< count($_POST['srno']); $i++){ echo "on 'save' clicked <br>"; echo "<br> products ".$_POST['products_id'][$i]; echo "<br>srno ". $_POST['srno'][$i]; echo "<br>industry ".print_r($_POST['ind'][$i]); echo "<br>product name ". $_POST['products_name'][$i]; Updateindustry($_POST['products_id'][$i], $_POST['srno'][$i],$_POST['industry'][$i],$_POST['products_name'][$i]); } $update=1; $alert= "udation done"; } I am not getting correct values for $_POST['industry'][$i] please help Similar TutorialsI'm trying to add a value to my multi dimension array. I want to add a 'quantity' value to the inner array.
This is what I have now:
Array ( [0] => stdClass Object ( [0] => stdClass Object ( [name] => BRACKET [option_sku] => [value] => B-Line BB7-16 ) [1] => stdClass Object ( [name] => BOX [option_sku] => [value] => Crouse Hinds TP40DPF ) [2] => stdClass Object ( [name] => Mud Ring[option_sku] => [value] => Rise Single Gang ) [3] => stdClass Object ( [name] => MC Cable[option_sku] => [value] =>black,white,green)) [1] => stdClass Object ( [0] => stdClass Object ( [name] => BRACKET [option_sku] => [value] => B-Line BB7-16 ) [1] => stdClass Object ( [name] => BOX [option_sku] => [value] => Crouse Hinds TP40DPF ) [2] => stdClass Object ( [name] => Mud Ring[option_sku] => [value] => Rise Single Gang ) [3] => stdClass Object ( [name] => MC Cable[option_sku] => [value] =>black,white,green)) )I'm trying to make it like this: Array ( [0] => stdClass Object ( [0] => stdClass Object ( [name] => BRACKET [option_sku] => [value] => B-Line BB7-16 [quantity] => 5 ) [1] => stdClass Object ( [name] => BOX [option_sku] => [value] => Crouse Hinds TP40DPF [quantity] => 5) [2] => stdClass Object ( [name] => Mud Ring[option_sku] => [value] => Rise Single Gang [quantity] => 5) [3] => stdClass Object ( [name] => MC Cable[option_sku] => [value] =>black,white,green [quantity] => 5)) [1] => stdClass Object ( [0] => stdClass Object ( [name] => BRACKET [option_sku] => [value] => B-Line BB7-16 [quantity] => 5) [1] => stdClass Object ( [name] => BOX [option_sku] => [value] => Crouse Hinds TP40DPF [quantity] => 5) [2] => stdClass Object ( [name] => Mud Ring[option_sku] => [value] => Rise Single Gang [quantity] => 5) [3] => stdClass Object ( [name] => MC Cable[option_sku] => [value] =>black,white,green [quantity] => 5)) )I'm trying something like this but it isn't working. <?php //This is the array I want to add to. $product_options = $registry->toObject(); //This is the value of what I want to add to the array. With the key being 'quantity'. $number = $item->orderitem_quantity; ?> <?php foreach($product_options as $key => $quan) ?> <?php $product_options[$key]['quantity'] = $number; ?> <?php endforeach; ?> Edited by Zane, 19 June 2014 - 02:43 PM. Hi I have a multi-dimension session array as per the below: $_SESSION['myarray'][][] If for example there are several elements stored in the array: $_SESSION['myarray'][0][0] = "aaa"; $_SESSION['myarray'][0][1] = "111"; $_SESSION['myarray'][1][0] = "bbb"; $_SESSION['myarray'][1][1] = "222"; $_SESSION['myarray'][2][0] = "ccc"; $_SESSION['myarray'][2][1] = "333"; $_SESSION['myarray'][2][0] = "ddd"; $_SESSION['myarray'][2][1] = "444"; How can i remove one of the above and shunt the other elements up? I have tried using the unset command but this seems to essentailly nullify the desired values where as what i would like to do is remove them completly from the array and then shorten the total number of records stored in the array. Thanks for taking the time to read this. I am having a bit of trouble with the following (just for theoretical purposes of course!). Just wondered how you do the following code: $products = array(); // makes a empty array called $products $i = 1; $icol = 1; while($row = mysql_fetch_array($result)) { $products[$i]['product1'] = $row[$icol][0]; $i = $i++; } All its meant to do is: Quote SELECT * FROM products Which the table contains just 3 columns: productid | product | price 1 | product1 | 25.55 That is it, very simple, but wanted to make a multi dimension array that will be 3 columns wide, then however many rows (hence the while loop and array fetch etc). Any help is appreciated, just coming from a VBA background when it comes to the arrays in VB lol. Thanks for any help in advance, Jeremy. Hi, i am having trouble retriving data from a 2D array in my php code an was wondering if i should be using a session array instead to keep track of the contents of the array between refreshes of the page. I am currently working with an array in the form of : $hosts[][] // $hosts[$x][0] - the hostname // $hosts[$x][1] - the ip address // $hosts[$x][2] - the operating system However, i seem to loose the ability to read data from this array when the user creates a refresh event (ie clicks a button/hyperlink). Should i be storing the data in a $_SESSION array, if so what is the correct syntax to use, i have only ever stored single values in $_SESSION variables. Thanks for reading. Hi, how to identify whether the array is single or two dimensional array? example structure of text database- mytextfile.txt 10-11201|2010/09/01|Sam|Thurston 10-11307|2010/09/04|Tony|Piper 10-11405|2010/09/11|Sarah|Smith <?php $file2 = 'mytextfile.txt'; $openedfile =fopen($file2, "r") or die("ERROR- could not open file for editing."); // flock($openedfile, LOCK_EX) or die("Error!- Could not obtain exclusive lock on the file to edit."); $hold[$record_count] = explode("|", trim(fgets($openedfile))); while(!feof($openedfile)) { $record_count++; $hold[$record_count] = explode("|", trim(fgets($openedfile))); } ?> What I need to do is loop through this and one by one- take any arrays that contain a date that is between $dateX and $dateY (which comes via a form input) and place it in a new array $matched. I am stumped. 1-Is there a way to do it inside the above while loop that I am not seeing? 2- Do I need to open the file in another manner? 2- Would it be best to now loop through $hold[$record_count] I am trying to keep the process short so as not to use up too much memory. Point me in the right direction for this one please. I have a for loop which extracts info from a MySQL table, chooses something then needs to push each bit into an array: for($i = 0 ; $i < $query_report_rows ; ++$i) { $row= mysql_fetch_row($query_names); if($row[6] == 0) { $sName = $row[1]; } elseif($row[6] == 1) { $sName = $row[2]; } $name = "$row[0], $sName"; $print = nl2br($row[7]); } What i need is a multidimensional array to hold $name and $print: $name_print = array( array('name'=>$name 'print'=>$print ) ) How do i array push into such an array from the above for loop? (multi-dimensional array are a very weak point at the moment, so any pointers here would be very much appreciated) Hi people I need a way to loop through an array where $imgid is some random number assigned to each array element and there are 4 fields id, url, tnurl, caption per array element. I need to extract these 4 fields in the same loop iteration. Thanks Code: [Select] $_SESSION['imagegallery'][$imgid]['id']; $_SESSION['imagegallery'][$imgid]['url']; $_SESSION['imagegallery'][$imgid]['tnurl']; $_SESSION['imagegallery'][$imgid]['caption']; example: Code: [Select] $_SESSION['imagegallery'][347]['id']; $_SESSION['imagegallery'][347]['url']; $_SESSION['imagegallery'][347]['tnurl']; $_SESSION['imagegallery'][347]['caption']; $_SESSION['imagegallery'][892]['id']; $_SESSION['imagegallery'][892]['url']; $_SESSION['imagegallery'][892]['tnurl']; $_SESSION['imagegallery'][892]['caption']; Code: [Select] $i=1; $selected=0; $uservalue=20; $numCols = 3; $numPerCol = ceil(20 / $numCols); echo "<table class=tuble><tr>"; for($col = 1; $col <= $numCols; $col++) { echo "<td>"; for($row = 0; $row < $numPerCol; $row++) { $resultRow = 20; if ($i<=$uservalue) { if ($i == $selected) { $checked = 'checked'; }else{ $checked = ''; } $styles = array( 'Blue Essense' => array('#2B8EFF', '#63CBFF', '50 Forum Gold'), 'veggie' => array('carrot', 'collard', 'pea') ); echo '<input type="radio" value="'.$i.'" name="form[color]">Blue Essence <b><a style="color:#2B8EFF;text-shadow:#63CBFF 2px 1px 1px" href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b><br>'; } else { echo '<label for="s'.$i.'"><input type="radio" value="'.$i.'" name="form[star]" disabled><b><a href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b><br>'; } $i++; } echo "</td>"; } echo "</tr></table>"; Okay, see the $styles array? Code: [Select] $styles = array( 'Blue Essense' => array('#2B8EFF', '#63CBFF', '50 Forum Gold'), 'veggie' => array('carrot', 'collard', 'pea') ); The first is hex code, background, and 2nd is #63CBFF (Front color) and the 3rd is how much it costs I need those arrays to echo out correspondent with my Code: [Select] echo '<input type="radio" value="'.$i.'" name="form[color]">Blue Essense<b><a style="color:#2B8EFF;text-shadow:#63CBFF 2px 1px 1px" href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b> (50 Forum Gold)<br>'; See how the style="color etc/etc" I need to grab the data from $styles to go into the correct spots, Is this feasable? Long story short, I need to explode those arrays out somehow, any idea? What am I missing here? The array: protected $form_bonus = array( "Attacker" => array( "Ashwin" => array( "normal" => 1.15, "rps" => 1.38), "Cordelon" => array( "normal" => 1.15, "rps" => 1.38), "Mersan" => array( "normal" => 1.15, "rps" => 1.38), "Phlanixian" => array( "normal" => 1.195, "rps" => 1.494), "Slythe" => array( "normal" => 1.15, "rps" => 1.38) ), "Defender" => array( "Ashwin" => array( "normal" => 1.15, "rps" => 1.38), "Cordelon" => array( "normal" => 1.15, "rps" => 1.38), "Mersan" => array( "normal" => 1.15, "rps" => 1.38), "Phlanixian" => array( "normal" => 1.15, "rps" => 1.38), "Slythe" => array( "normal" => 1.15, "rps" => 1.38) ) ); accessing it: $bonus *= form_bonus[$this->role][$this->race]['rps']; error: PHP Parse error: syntax error, unexpected '[' Whats the most efficient way of searching within a multi dimensional array? My Array = Array ( => Array ( => Item 1 [title] => Item 1 [1] => 2012-03-21 [eventDate] => 2012-03-21 [2] => 21 [eventDay] => 21 ) [1] => Array ( => Item 2 [title] => Item 2 [1] => 2012-03-21 [eventDate] => 2012-03-21 [2] => 21 [eventDay] => 21 ) ) String to find = '21' If (MY ARRAY contains STRING TO FIND) {} Clearly in this case there are several '21' (s) so if I only wanted to search the [eventDay] keys...would there be a fast effective and efficient manner? Thoughts and help gratefully received. Will How do I do this? It only does the first nine records and wont cycle through the many records in the second dimension. Thanks. // External Loop is 1 - 9 // Internal Loop is 0 - whatever it is //$muarray_s[0] = $facility_id; //$muarray_d[$external_loop][$interal_loop] = $objfields['139339_date']; //$muarray_t[$external_loop][$interal_loop] = $objfields['139339_time']; //$muarray_i[$external_loop][$interal_loop] = $objfields['139339_main_id']; //$array_of_mu[$external_loop][$interal_loop] = $checklist_item_disc; echo "Begin Draw Runway Image Function <br>"; echo "Establish Array Looping Procedure"; for ($j=0; $j<10; $j=$j+1) { // External Loop // This is actually the Condition not the mu for ($k=0; $k<count($array_of_mu); $k=$k+1) { // Internal Loop // This is the Mu Value; echo "Mu Value is ".$array_of_mu[$j][$k]."<br>"; } } Hi can anybody help me out with the following problem I am having? I need to populate the key values in the multi-dimentional array structure shown below and the values will be used later in the following way: <tr> <td><?php echo $sm_array[1]['col_1']; ?></td> <td><?php echo $sm_array[1]['col_2']; ?></td> <td><?php echo $sm_array[1]['col_3']; ?></td> <td><?php echo $sm_array[1]['col_4']; ?></td> //and so on, up to 10 </tr> <tr> <td><?php echo $sm_array[2]['col_1']; ?></td> <td><?php echo $sm_array[2]['col_2']; ?></td> <td><?php echo $sm_array[2]['col_3']; ?></td> <td><?php echo $sm_array[2]['col_4']; ?></td> //and so on, up to 10 </tr> This is the array structure, $td->innertext is just a different value each time and is not important for this example, it could essentially be anything: sm_array[$row] = array( "col_1" => $td->innertext, "col_2" => $td->innertext, "col_3" => $td->innertext, "col_4" => $td->innertext, "col_5" => $td->innertext, "col_6" => $td->innertext, "col_7" => $td->innertext, "col_8" => $td->innertext, "col_9" => $td->innertext, "col_10" => $td->innertext, ); But I need to add the values inside a loop, like this: while($row=0; $row<=10; $row++) { $sm_array[$row] = array( "col_".$row => $td->innertext); } But this doesn't allow me to change the key name inside the loop, so for $sm_array[1] there will be 10 keys and their values, and the same for $sm_array[2], it will also have 10 keys and different values for each key and so on! But for some reason the key value will not work this way inside a loop, it doesn't allow "col_".$row and I even tried to echo the key name, like this: while($row=0; $row<=10; $row++) { $sm_array[$row] = array( "<?php echo $row; ?>" => $td->innertext); } It only seems to accept a fixed string or integer as the key, like this: while($row=0; $row<=10; $row++) { $sm_array[$row] = array( "col_1" => $td->innertext); // or like this $sm_array[$row] = array( 1 => $td->innertext); } But then I have a problem, I can't change the key name dynamically! I have tried doing this with conditional staments like this, but it only accepts the first key name and the 2nd, 3rd etc, etc all fail: while($row=0; $row<=10; $row++) { if($row==1){ $sm_array[$row] = array( "col_1" => $td->innertext); }else if($row==2){ $sm_array[$row] = array( "col_2" => $td->innertext); } //and so on } Can somebody please tell me what I am doing wrong or can't this be done or is their some other way of doing this? Thanks in advance Grant I am having a hell of a time getting this to work. I need the keys in an array to be specific, not a sequential number or a row in my database. I need to add to the array through each loop of my while() statement. This code does it, but does not use the keys I specify, after the first entry, it starts assigning numbers to keys. Any guidance would be great. <?php require_once ('includes/config.php'); require_once ('includes/connect.php'); $echoarray = array(); $resultsql = mysql_query("SELECT * FROM clients")or die(mysql_error()); while($row = mysql_fetch_array($resultsql)){ if(empty($echoarray)){ $echoarray = array( 'id' => $row['ID'], 'name' => $row['First_Name'] . " " . $row['Last_Name'], 'price' => $row['Status'], 'number' => $row['Sex'], 'address' => $row['Phys_Street'], 'company' => $row['Agency'], 'desc' => $row['Notes'], 'age' => $row['Date_Birth'], 'title' => $row['Occupation'], 'phone' => $row['Phone'], 'email' => $row['Email'], 'zip' => $row['Phys_Zip'], 'country' => $row['Phys_City'] ); } else { array_push($echoarray, $echoarray['id'] = $row['ID'], $echoarray['name'] = $row['First_Name'] . " " . $row['Last_Name'], $echoarray['price'] = $row['Status'], $echoarray['number'] = $row['Sex'], $echoarray['address'] = $row['Phys_Street'], $echoarray['company'] = $row['Agency'], $echoarray['desc'] = $row['Notes'], $echoarray['age'] = $row['Date_Birth'], $echoarray['title'] = $row['Occupation'], $echoarray['phone'] = $row['Phone'], $echoarray['email'] = $row['Email'], $echoarray['zip'] = $row['Phys_Zip'], $echoarray['country'] = $row['Phys_City'] ); } Any Ideas? I have been trying to solve this problem for a few days now and can't seem to work it out. I'm sure i'm missing some simple point.. I am using a mysql PDO fetch to return 2 fields in each row and then put them into an array of their own: foreach ($st->fetchAll() as $row){ $subs[] = array($row['subscriber'],$row['plant']); Some of the rows share the same $row[0], which is the 'subscriber' field. What I want to do is make up another array with each smaller array having a single, unique $row[0] and any number of added 'plant' fields following (e.g $row[1],$row[2] etc). I've tried all sorts of ways to achieve this but am at a loss. Would someone be able to point me in the right direction ? Thanks. HI - Pulling my hair out .. I'm trying to extract from a 3 dimensional array the data for insert to MySql. It is looping correctly however, and it is inserting, however for some reason I am not getting values for price and quantity, just 0. The array is contained within Code: [Select] $prod_details = ($_SESSION['cart']); if you do a print_r on the the session, you get : Code: [Select] Array ( [22] => Array ( [name] => Turkey Jumbo Pack [price] => 155.25 [count] => 1 ) [20] => Array ( [name] => Chicken Variety Pack [price] => 120.35 [count] => 1 ) ) I am using a nested foreach loop to traverse the array: Code: [Select] $prod_details = ($_SESSION['cart']); foreach ($prod_details as $keyOne=> $value){ $prodID = $keyOne; foreach($value as $keytwo=>$row) { $price = $keytwo['price']; $quantity = $keytwo['count']; } But I all I get out of $price and $quantity when I do an insert is 0. What am I doing wrong ? MANY MANY thanks for all your help ! ! $form_bonus = array( "Attacker" => array( "Ashwin" => array( "normal" => 1.15, "rps" => 1.38), "Cordelon" => array( "normal" => 1.15, "rps" => 1.38), "Mersan" => array( "normal" => 1.15, "rps" => 1.38), "Phlanixian" => array( "normal" => 1.195, "rps" => 1.494), "Slythe" => array( "normal" => 1.15, "rps" => 1.38) ), "Defender" => array( "Ashwin" => array( "normal" => 1.15, "rps" => 1.38), "Cordelon" => array( "normal" => 1.15, "rps" => 1.38), "Mersan" => array( "normal" => 1.15, "rps" => 1.38), "Phlanixian" => array( "normal" => 1.15, "rps" => 1.38), "Slythe" => array( "normal" => 1.15, "rps" => 1.38) ) ); echo "<pre>".print_r($form_bonus)."</pre>"; output: 1 what am i missing here? Hello, I have been at this for awhile now and cant seem to figure it out. I have 2 separate arrays like shown below and I am trying to insert each value into my database using one update query. I have tried a for loop inside each array, but only one column populates. I have tried breaking the values outside their for loop, etc, etc. Does anyone know a way this can be done? Code: [Select] //Cars $cars = array('ford', 'toyota', 'bmw'); //Places $places =arrray('Orlando','California','Texas'); foreach ($cars as $car){ foreach ($places as $place){ $place .=$place; } mysql_query("INSERT INTO vehicles (cars, places) VALUES ('" . $car. "', '" . $place . "'')"); } Hi - I have an array which is created from a session variable I must have constructed my array wrong, as for the life of me I can not extract the individual values. I have tried innumerable $key=>$value combinations. I must be doing something stupid, maybe it is obvious, but not to me. I'll be darn grateful if you can help me ! Many Thanks MY array looks like this: Code: [Select] Array ( [quantity] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 ) [name] => Array ( [0] => Ribs Large pack 20 pieces [1] => 25 Piece Bag [2] => Sirloin Steak [3] => 50 piece bag of Scalops [4] => Sirloin Steak ) [prodid] => Array ( [0] => 17 [1] => 28 [2] => 27 [3] => 25 [4] => 27 ) ) The CodeIgniter form which generated this array looks like this: Code: [Select] <?php foreach ( $_SESSION['openorders'] as $key=>$value) { ?> <tr align="center"> <td width="30"><?php $data=array('name' =>'quantity[]','value'=>$value['quantity']); echo form_input($data); ?></td> <td><?php echo $value['prodid']; ?></td> <td><?php echo $value['name'];?></td> <td><?php echo $value['ordervalue']; ?></td> <td><?php echo $value['pricelb']; ?></td> <td align="right"><?php echo form_checkbox('delete','delete',FALSE); echo form_hidden('prodid[]', $value['prodid']); echo form_hidden('name[]', $value['name']); echo form_hidden('orderid', $value['orderid']); $totalordervalue += $value['ordervalue']; } ?> I want to create an array from a file with this structure TITLE1, HEADER1, key11, key12, key13, key14 TITLE2, HEADER2, key21, key22, key23, key24 . . . How I can get a foreach with these elements: $title="TITLE" $header="HEADER" $key="(one random key)" If needed, I can change the file structure too. |