PHP - Xml Array Problem
Hello all,
I've been working on parsing through some XML data that is formatted as x-schema. It's been a challenge to say the least. I've looked at XMLReader and the like to see if I can parse through the data but with no luck. I found an approach that works but having trouble iterating over the nodes inside the nodes. I'll try to explain: Here's what I have so far. The code produces the following result set: Code: [Select] [4] => Array ( [FARM:NAME] => Array ( [FARM:NAME] => Kenyon Farm [FARM:FARM] => [FARM:TOTALAREA] => 999 [FARM:BUSINESSFIELDS] => [BUSINESSFIELD:FIELD] => [BUSINESSFIELD:LABEL] => K9 [BUSINESSFIELD:TOTALROTATIONS] => 1 [BUSINESSFIELD:TOTALAREA] => 998 [BUSINESSFIELD:FIELDOPTIONS] => [BUSINESSFIELD:FIELD_BOUNDARY] => [BUSINESSFIELD:DATAFILE] => FieldBoundary_2682 [BUSINESSFIELD:ACTIVITIES] => [CUST:FARMS] => ) ) Here's the code that produces that array: Code: [Select] $tuData=file_get_contents("xml/tim.xml"); $simple = $tuData; $p = xml_parser_create(); xml_parse_into_struct($p, $simple, $vals, $index); xml_parser_free($p); $getValue=""; $counter=0; print"<pre>"; print_r($vals); foreach($vals as $data) { if(isset($data['tag'])) { if($data['tag'] == 'FARM:NAME') { $key = $data['tag']; $counter++; } if(trim($key)!== '' && trim($data['tag']) !== '') { $arData[$counter][$key][$data['tag']] = $data['value']; } } } print"<pre>"; print_r($arData); Now to my problem. You'll notice that the array has a key called [BUSINESSFIELD:LABEL] ... in the attached xml file there can be more than one field for each farm. My current solution only gets the first field in the xml file (as the current example shows above) and moves to the next farm and creates a new array of data. I would like to get the other fields for each farm and "stuff" them in the same array. Array's are not my strong suite and would be grateful to anyone that could help me with this problem. Thanks a bunch! Similar TutorialsMy array results keep displaying like this. I want it to just display the numbers in the quotations. any help? Code: [Select] array ( 0 => '4396', ) array ( 0 => '3934', ) array ( 0 => '5063', ) array ( 0 => '4394', ) array ( 0 => '3936', ) array ( 0 => '4393', ) array ( 0 => '4395', ) array ( 0 => '5064', ) array ( 0 => '5062', ) array ( 0 => '4224', ) array ( 0 => '3322', ) array ( 0 => '3321', ) array ( 0 => '3320', ) array ( 0 => '3725', ) Hello mates, i've been googling about my problem but i couldn't fix it. Code: ###################################### $web = file_get_contents($tmpUrl); $explodedWeb = explode("<b>Somecode:</b> ",$web); print $explodedWeb [1]; ###################################### I'm just printing it for testing purposes, and the $web var works fine(since tried printing the hole source). Error: "Notice: Undefined offset: 1 in ..." I tried to create an empty array in explodedWeb ("$explodedWeb[]") but didn't work either. Greets. Okay the problem is that it wont find the correct carrier. it dont even find it. i dont know whats wrong. Please take a look at the code. i dont know how to explain it better $carriers = array('verizon'=>'@vtext.com','tmobile'=>'@tomomail.com','sprint'=>'@messaging.sprintpcs.com','att'=>'@txt.att.net','virgin'=>'@vmobl.com','textnow'=>'@textnow.me','metro'=>'@mymetropcs.com','unknown'=>'@teleflip.com'); if (array_key_exists($carrier, $carriers)) { $correctCarrier = $carriers[$carrier]; $i = 0; While($i < $_POST['amount']) { $i++; $formatted_number = $to.$correctCarrier; $result = ("$i of Your Messages Has been sent to the number ". $_POST['to'] . ".<br>" . mail("$formatted_number", "$subject", "$message") . ""); } mysql_query("INSERT INTO `msgssent` (`number`, `numberofmsg`, `subject`, `message`) VALUES ('". $formatted_number ."', '". $i ."', '". $subject ."', '". $message ."')") Or die(mysql_error()); Echo $result; } For some reason this array will only return 'white'. Is there a problem with the code?
<?php $test = array('blue','green','white'); foreach($test as $val) ?> <?php echo "$val"; ?> Hello all, I think I have a simple question. I have several arrays that I need the output from but I need the output from each array to be together. I know WHAT?! I'll try to explain: I have a dynamic jQuery form that fills in a table with data. Each "field" or <td> has separate information in each. The way I output the data is in an input box so that I can capture the value of each. The name attribute is the array for each input. So for example the output for each would look like this: Code: [Select] <tr> <td> <input type="text" name="fullName[]" value="input_from_user"/> </td> <td> <input type="text" name="Address[]" value="input_from_user"/> </td> </tr> If you can visualizes this we could have several rows of data from each array (fullName[] and Address[]). How would I extract the data in such a way that I can clump them into their appropriate rows? I know how to extract them one by one using foreach loop on each array but that won't do what I want. I would like the output to be like this: John Doe - 22 S Main Street Jim Smith - 111 N Main Street and so on... This is what I have so far but I'm sure this isn't how to accomplish what I'm after. $name = $_POST['fullName']; $address = $_POST['Address']; $outputArry = array( "FullName" => $name, "Address" => $address ); foreach ( $outputArry as $output ) { foreach ( $output as $row ) { $rowFullName = $row['FullName']; $rowAddress = $row['Address']; $rowOutput .= 'Name: ' . $rowFullName . ' Address: ' . $rowAddress . '<br />'; } } echo $rowOutput; Thanks for any help on this issue. Damian I want to store all the hours 0,1,2,3... in my $time array but it returns the last value only and I dont understand why... Code: [Select] <?php $time = array(); $hour = 0; for ( $i=0; $i<=7; $i++ ) { $time['hour'] = $hour; $hour++; } ?> Hey guys i have a table of pages set up with the possability of inserting pages inside pages. I am trying to make a delete button that would delete every child pages creating an array of page id's. page 1 L page 1.1 L page 1.2 L page 1.3 L page 1.4 page 2 my code looks like this so fare class delete_page { function page($cat_id, $x = ''){ $sql = mysql_query("SELECT * FROM pages WHERE parent='$cat_id' order by id asc"); while ($select = mysql_fetch_assoc($sql)) { $this->x[] = $select['id']; $this->x = $this->page($select['id'], $this->x); } return $this->x; } function get_page($cat_id){ return $this->page($cat_tid); } } $page = new delete_page; $get_array_page = $page->get_page(1); For some reason the above code sends me an array of all the pages in the database instead of returning only the child pages of the ID i inserted in the $get_array_page = $page->get_page(1) Code: [Select] Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) It should normaly return only page 1 L page 1.1 L page 1.2 L page 1.3 L page 1.4 with out page 2 like this Code: [Select] Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) What did i miss? Hello All - I have the following array: Code: [Select] array 0 => array 'farmName' => string 'Hallinan' (length=8) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 1 => array 'farmName' => string 'Holt' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 2 => array 'farmName' => string 'Holt' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 3 => array 'farmName' => string 'Holt' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 4 => array 'farmName' => string 'Holt' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 5 => array 'farmName' => string 'Holt' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 6 => array 'farmName' => string 'Home' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 7 => array 'farmName' => string 'Home' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 8 array 'farmName' => string 'Home' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) The data comes from a flat file that has (as you can see) many of the same names like Home and Holt. Is there a way to extract the first Holt and Home from the array and put those values in a new array? So my new array would look something like: Code: [Select] array 0 => array 'farmName' => string 'Hallinan' (length=8) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 1 => array 'farmName' => string 'Holt' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 2 => array 'farmName' => string 'Home' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) I look through php.net and really couldn't find a function that could handle what I was after. I'm sure I'd have to use a combination of each() and reset() or maybe not? Any help would be very welcome. Thanks! Hey guys, I've got a problem with an array sum. I tried doign array_sum(), and a few other stuff but none worked. Here's my code: Code: [Select] $upkeepcheck = $db->query("SELECT i.itmid, i.upkeep, inv . * , u.userid, u.will, u.maxwill, u.upkeepowed, u.money, h.hPRICE, hWILL FROM items i LEFT JOIN inventory inv ON inv.inv_itemid = i.itmid LEFT JOIN users u ON inv.inv_userid = u.userid LEFT JOIN houses h ON h.hWILL = u.maxwill WHERE i.upkeep >0"); $q5 = $db->query("SELECT * FROM settings WHERE conf_name = 'upkeep'"); while($upkeep=mysql_fetch_array($upkeepcheck)) { while($upkeep1=mysql_fetch_array($q5)) { if($upkeep['money'] > $upkeep['upkeep']) { $db->query("UPDATE users SET money = money - {$upkeep['upkeep']} WHERE userid = {$upkeep['userid']}"); } else { $db->query("UPDATE users SET upkeepowed = upkeepowed + {$upkeep['upkeep']} WHERE userid = {$upkeep['userid']}"); } if($upkeep1['conf_value'] == 'Stackup') { if($upkeep['upkeepowed'] > $upkeep['hPRICE']) { $db->query("UPDATE users SET will=100,maxwill=100 WHERE userid = {$upkeep['userid']}"); $db->query("UPDATE users SET upkeepowed = 0 WHERE userid = {$upkeep['userid']}"); event_add($upkeep['userid'], "You haven't paid your upkeep, so your house has been taken away. Next time sell some weapons or earn some more money to pay your upkeep!", $c); } } else if($upkeep1['conf_value'] == 'RemoveItems') { $rem = $db->query("SELECT i.itmid, i.upkeep, inv.*, u.userid, u.upkeepowed FROM items i LEFT JOIN inventory inv ON inv.inv_itemid = i.itmid LEFT JOIN users u ON inv.inv_userid = u.userid WHERE userid = {$upkeep['userid']}"); while($remove = mysql_fetch_array($rem)) { if(isset($remove['upkeep'])) { var_dump($remove['upkeep']); $upkeep = $upkeep + $remove['upkeep']; } } } } } The part that is messed up is Code: [Select] $upkeep = $upkeep + $remove['upkeep'];var_dump($remove['upkeep']); produces: Quote null string '100' (length=3) which is correct, I have 4 null values and 1 value of 100. And the error I get is: Fatal error: Unsupported operand types in C:\wamp\www\pagetest.php Any ideas how to fix this? I want the $upkeep to be updated with $remove['upkeep'] if the value isn't null. I am extracting an array from the database in sorted form with respect to it's parent_id. Now I have to make a sub tree of this array in such a way that related nodes would be inserted in 'pages' field. cleths->upper->shirts I'm sure there must be a way to do whatI am after, as it seems like such an obvious thing to do, but I can't figure it out. 1. I have an array which contains a list of database's and their connections $slaveRes_array[0] = array('server' => 'localhost:3306', 'user' => $u, 'password' => $p, 'database' => 'MySQL01_5083', 'sendmail' => '0', 'repeat' => '0', 'dbID' => '1' ); and so on 10 times I then have a list of MySQL Queries: (at the moment there is only one, but there will be more later on). $query_array[] = array( 'query1' => "SHOW SLAVE STATUS"); The idea is that I take the first database from $slaveRes_array, connect to it, and run each query from the $query_array in turn against that database, and ultimately pass that resultset into an array that I can refer back to later on. Within $slaveRes_array I have a DB name (e.g. MySQL01_5083), I want this to be the first entry in my resultset array, so I can reference this. However, not all database's will return a result. So what I want to end up with is something like: => Array ( [name] => MySQL02_5083 ) ( [Slave_IO_State] => Waiting for master to send event)... (in this case the first server (MySQL01) didn't return a result, so the first item in the array is MySQL02. I have found lots of ways to achieve bits of this, but none that work together. I can get a list of database names that returned a result, but can't get the data to return to the array (or any array). I can get the data into an array, but not the database names. Surely this must be something straight forward that people do all the time, right? I have an array that displays the following information with the print_r function: Array ( => Array ( => 2206.235 [1] => 3.9 [2] => 0.48 [3] => 0.477 [4] => 0.477 [5] => 0.475 [215] => 0.434 [216] => 0.436 [217] => 0.435 [218] => 0.434 [219] => 0.435 [220] => 0.435 ) ) Array ( => Array ( => 2206.235 [1] => 3.9 [2] => 0.48 [3] => 0.48 [4] => 0.435 [5] => 0.435 ) ) I would like to save the last row to my database which in this case is 220 but the last row could vary from time to time as the array is created from an email attachment so l wanted a code that would find the number of rows in the array. When l echo out the row like this: echo $matches[0][210] it retrieves the correct information so l wanted to implement this code instead to cater for an unknown amount of array rows so that the highest row was always returned: $i = count($matches[0]); echo $matches[0][$i]; but this does not work as when echoing $i the result is 2216 instead of 220 which l find odd or l am doing something wrong. Any ideas? thanks Callum Hello guys I have here my array result Array ( => Array ( => 1 [1] => [2] => 123456789 [3] => [4] => 2012-02-02 [5] => 09:12:21 [6] => Test Array ) ) I would like to view this result into a string so that it will remove the unnecessary array elements like brackets etc. Can some tell me how to handle this properly? The result would be : 1 123456789 2012-02-02 09:12:21 thanks 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. I use Popshops Datafeed API and cant figure out an issue im having with an array. I'm trying to retrieve a category name from an array for the current page. Here is the array code which is in the index.php file. $categories = array(); $categories[] = array( 'name' => 'page name', 'search_options' => array( 'keywords' => 'page keyword' ) ); $categories[] = array( 'name' => 'page name', 'search_options' => array( 'keywords' => 'page keyword' ) ); $categories[] = array( 'name' => 'page name', 'search_options' => array( 'keywords' => 'page keyword' ) ); This is how the script calls the current page keyword. <?php echo ucwords($_REQUEST[$popshops->nameSpace.'keywords']) ?> The code above works fine, so I thought all I had to do was change the 'keywords' to 'name' to get what I was looking for. But that does not seem to work? <?php echo ucwords($_REQUEST[$popshops->nameSpace.'name']) ?> I've tried as much as my limited php knowledge can take me, so now I need some advice. Here is the function in the popshops.php file they use to pull the page name out for the links in the navigation, but I just want to know how to retrieve just the category name for the current page, nothing else. function customCategoryLink($category) { $url = $this->baseURL($this->params); if (isset($category['search_options']) && sizeOf($category['search_options']) > 0) { foreach($category['search_options'] as $key => $value) { $url = $this->addParameter($url,$key,$value); } } else { $url = $this->addParameter($url,'keywords',$category['name']); } $url = str_replace('?&','?',$url); return '<a href="'.$url.'">'.$category['name'].'</a>'; } Thanks in advanced! Hi guys ,
Again i facing array comparison problem
i got this after var_dump() $a and $b
array(4) { ["q1"]=> string(3) "yes" ["q4"]=> string(2) "no" ["q3"]=> string(2) "no" ["q2"]=> string(2) "no" } array(4) { ["q1"]=> string(2) "no" ["q4"]=> string(3) "yes" ["q3"]=> string(3) "yes" ["q2"]=> string(3) "yes" } But when i use array_intersect($a,$b).. it results this array(4) { ["q1"]=> string(3) "yes" ["q4"]=> string(2) "no" ["q3"]=> string(2) "no" ["q2"]=> string(2) "no" } Don't know why, but there is no same value in both Hi,Dear Frnd...........I Hope u r all fine. I am using php array_unique function.Here is the problem in this function. This is the code. Code: [Select] <?php $frnd1 = array( 0=> 'arfan', 1=> 'haider', 2=> array( 0=> 'azeem', 1=> 'jeme', ), 3=> array( 0=> 'haider', 1=> 'one' ) ); // please do not change the above code. // You can only change below code. print_r($frnd1); echo '<hr />'; print_r(array_unique($frnd1)); ?> This work fine,But when I am used array_unique then it does not work. When I am used array_unique.It ignore the [3]=>Array. As you can see in my array,Here is two time name "haider". I want to do that in this array all same names are convert into one unique name. In this array I want to ignore the "haider" name bcs it is two time.How can I do that. Remmber :: This example is very simple.Bcs u can understand it easily.Suppose if it is dynamically changed array then what i did do to make it unique. Hi What I need to do is retrieve information from a web form, that contains a file id, and which options from check boxes have been ticked. The code in the web form is. Code: [Select] <td width=\"206\"><p>$filename1<br><img src=\"$filename1\" alt=\"$filename1\" /></p> <p> <label> <input type=\"checkbox\" name=\"image_id[$unique_id1][save]\" value=\"save\" id=\"CheckboxGroup1_1\" /> Save</label> <br /> <label> <input type=\"checkbox\" name=\"image_id[$unique_id1][delete]\" value=\"delete\" id=\"CheckboxGroup1_2\" /> Delete</label> <br><label> <input type=\"checkbox\" name=\"image_id[$unique_id1][rotate]\" value=\"rotate\" id=\"CheckboxGroup1_3\" /> Image needs rotating</label> <br /> </p></td> This returns the following when I do a Code: [Select] var_dump of $_POST['image_id'][/code] when all three boxes are ticked. Code: [Select] array(1) { [4600]=> array(3) { ["save"]=> string(4) "save" ["delete"]=> string(6) "delete" ["rotate"]=> string(6) "rotate" } } the format is as follows 4600 is the file ID, the 3 others are save, delete, and rotate It appears to work ok, but I can't get at the data easily I can get the key using key(), but I can't get the other bits of data out using a foreach loop. Code: [Select] if(isset($_POST['image_id'])) { $file_info = $_POST['image_id']; if (isset($file_info)) { var_dump($_POST['image_id']); while($element = current(file_info)) { echo "<br> Loop = " . key($file_info)."\n"; $index_key = key($file_info); foreach ($dog1 as $index_key => $value) { Echo "<br> foreach value = $value"; } next($file_info); } } } I'm wondering if the array that is being sent back is ok as I tried to creat an array that sent back the same and couldn't, so maybe my code in the form needs changing. I came accross a problem where im not sure how to add the word "and" inbetween my variables. I'm only displaying one $field below but i have 6 of them. If I add and as the seperator in the implode it will repeat it and and. Also it will put it at the end Code: [Select] <?php $field1 = "Farms to " . $_POST['farms_population']; $a = array("$field1", "$field2", "$field3", "$field4", "$field5", "$field6"); $_SESSION['changed'] = "You have changed " . implode(" ",$a); echo $_SESSION['changed']; ?> wanting it to echo something like You have changed Farms to 30 and Larva Pools to 10 and Homes to 10 |