PHP - Counting Multi-dem Array
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>"; } } Similar TutorialsI have an array. When I print it it looks like this: Quote Array ( [1] => Array ( [Cushion] => Cushion 1 [Fabric] => f-111111 [FabricPrice] => 0 [Fill] => Fiber [Button] => none [ContWelt] => none [ContWeltFabric] => none [Zipper] => N [Quantity] => 2 [WeltSize] => [SKU] => c-111111 [Edge] => Knife [Cap] => N [Straps] => N [Hinged] => N [Type] => Boxed Button [Price] => 54.25 [Total] => 108.5 ) [2] => Array ( [Cushion] => Cushion 1 [Fabric] => f-111111 [FabricPrice] => 0 [Fill] => Fiber [Button] => none [ContWelt] => none [ContWeltFabric] => none [Zipper] => N [Quantity] => 4 [WeltSize] => [SKU] => c-111111 [Edge] => Knife [Cap] => N [Straps] => N [Hinged] => N [Type] => Boxed Button [Price] => 54.25 [Total] => 217 ) ) Now when I use count() I get a result of 01 when I should get 02 when I use count( ,1) I get a result of 381. I am Using this wrong? the array is stored in $_SESSION['cushArray'] Hi guys, How would I write a small script in php to count the unique values in the below array : the below code if part of cubecart 3.20 shopping cart, I need to count the unique number of Seller_ID fields within this array in the basket to be able to calculate shipping according to the number of sellers. How would I to this ? please any help is appreciated. Code: [Select] if(isset($_GET['act']) && $_GET['act']=="step4"){ // set live vars for order inv and its the last step $view_cart->assign("QUAN_DISABLED","disabled"); $view_cart->assign("TEXT_BOX_CLASS","textboxDisabled"); $basket = $cart->setVar($productId,"productId","invArray",$i); $basket = $cart->setVar($product[0]['name'],"name","invArray",$i); $basket = $cart->setVar($product[0]['productCode'],"productCode","invArray",$i); $basket = $cart->setVar($plainOpts,"prodOptions","invArray",$i); $basket = $cart->setVar(sprintf("%.2f",$price*$quantity),"price","invArray",$i); $basket = $cart->setVar($quantity,"quantity","invArray",$i); $basket = $cart->setVar($product[0]['digital'],"digital","invArray",$i); $basket = $cart->setVar($product[0]['Prod_Seller'],"Prod_Seller","invArray",$i); $basket = $cart->setVar($product[0]['Seller_ID'],"Seller_ID","invArray",$i); } else { $basket = $cart->unsetVar("invArray"); } Hi all What I am trying to achieve is a for each loop for the following output Code: [Select] Array ( [result] => success [source] => getDirectory [success] => Array ( [0] => /root/mc/world/data/ [1] => /root/mc/world/level.dat [2] => /root/mc/world/level.dat_old [3] => /root/mc/world/players/ [4] => /root/mc/world/players/herghost.dat [5] => /root/mc/world/region/ [6] => /root/mc/world/region/r.-1.0.mcr [7] => /root/mc/world/region/r.0.0.mcr [8] => /root/mc/world/session.lock [9] => /root/mc/world/uid.dat ) ) How would I go about saving each path from the [success] output? Basically I wish to loop through the array and delete the files. Many Thanks 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) 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? 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? 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 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']; 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 '[' 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. 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 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 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. 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 ! ! Hi, I have been banging my head against a brick wall the past few days trying to figure this out after reading so many tutorial and forum post but I still can't figure it out. I would be very grateful if someone could help me out. Basically I am being supplied with this JSON Data structu $json='{ "countries": { "country": [ { "@attributes": { "Name": "Germany", "Lat": "52.6847077739394", "Lng": "-6.81976318359375", "ZoomLevel": "7" } }, { "@attributes": { "Name": "Sweeden", "Lat": "54.00131186464818", "Lng": "-7.36358642578125", "ZoomLevel": "8" } } ] } }'; I decode the JSON data structure as a PHP array and I print out the contents of the array for debugging purposes: $json_array=json_decode($json,true); print_r($json_array); Heres what the outputed array looks like: Code: [Select] Array ( [countries] => Array ( [country] => Array ( [0] => Array ( [@attributes] => Array ( [Name] => Germany [Lat] => 52.6847077739394 [Lng] => -6.81976318359375 [ZoomLevel] => 7 ) ) [1] => Array ( [@attributes] => Array ( [Name] => Sweeden [Lat] => 54.00131186464818 [Lng] => -7.36358642578125 [ZoomLevel] => 8 ) ) ) ) ) I want to be able to access all the country names contained in [Name] Can anyone please help me on how to do this correctly as I've tried lots of code with multi-dimensional arrays but I've had no luck. I am not able to acces the variable correctly. Thanks in advance. I have an array which will be below. The array is being built from an XML file, the problem is I need to extrapolate data from it.
I am having trouble with looping through the array properly.
The output should be something like this:
Beth's state is 0 (Where Beth's is the name and 0 is the state)
Clint's state is 0
Array ( [0] => Array ( [did] => 216616014153767704 [known] => 1 [lock] => 0 [state] => 0 [level] => 100 [node] => 30 [port] => 0 [nodetype] => 16386 [name] => Beth's [desc] => LED [colorid] => 1 [type] => multilevel [rangemin] => 0 [rangemax] => 99 [power] => 0 [poweravg] => 0 [energy] => 0 [score] => 0 [productid] => 1 [prodbrand] => TCP [prodmodel] => LED A19 11W [prodtype] => LED [prodtypeid] => 78 [classid] => 2 [class] => [subclassid] => 1 [subclass] => [other] => ) [1] => Array ( [did] => 216616014154116936 [known] => 1 [lock] => 0 [state] => 0 [level] => 100 [node] => 30 [port] => 0 [nodetype] => 16386 [name] => Clint's [desc] => LED [colorid] => 1 [type] => multilevel [rangemin] => 0 [rangemax] => 99 [power] => 0 [poweravg] => 0 [energy] => 0 [score] => 0 [productid] => 1 [prodbrand] => TCP [prodmodel] => LED A19 11W [prodtype] => LED [prodtypeid] => 78 [classid] => 2 [class] => [subclassid] => 1 [subclass] => [other] => ) ) I am validating a form, and if there are errors in the input, the error array is filled with appropriate mssg and the "sticky" form with right values is shown back, and errors on top. Example: first name, last name, email, date of birth. each field is validated,and if for example first name does not pass,i will nullify the value and populate error array like below: $_SESSSION['error']['fname']="Please check the format of the name,can contain only alphabets!" $_SESSSION['error']['email']="Please check the format of email!" When I slap back the form, last name has passed, and so will have the previously entered value, but first name and email will be empty, and i need to know how to display the 2 errors on top of the form. It uses post method, and posts to itself. I have a pipe delimited file with this sample content: INV8004199|01/26/2012|Next door|648.00 SPT803487|01/25/2012|This and That|3360.00 INV8004193|01/25/2012|Where is it|756.00 INV8004133|01/05/2012|snowy winter|6750.00 I open the file to read it: Code: [Select] $CPfile = "sample.DEL"; $ft = fopen( $CPfile, "r"); $content1 = fread($ft, filesize($CPfile)); fclose($ft); Then I loop thru it: Code: [Select] $out = explode("\n", $content1); $total = 0; foreach($out as $var) { $tmp = explode("|", $var); for($i=0;$i<count($tmp);$i++){ echo $tmp[$i] . "<BR>"; } } What I would like to get is the total of all prices (last column) in all lines of the file. Can someone help please?. Thanks! $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? I'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. |