PHP - Appending Char To An Array Using Count()
Hi all
I am trying to loop through an array and output as JSON. What I'm looking to do is create something like: Code: [Select] "something": [ {"title":"Test 1"}, {"title":"Test 2"} ], Notice that the final row has no comma. My code is as follows: <?php foreach($something as $thing): ?> <?php $something_array = array('title'=>$thingt->getId()); ?> <?php echo json_encode($something_array).','."\n"; ?> <?php endforeach; ?> I had to add a comma to the end of the array, or the JSON rows would not be ended with one. How can I get it so that, no matter how many items are in the array, the last row, wil not have the comma at the end? With the comma at the end, my JSON doesn't validate Thanks [/code] Similar TutorialsHi, I have an html page with multiple image tags inside it. I'm wanting to get the full URL of a specific image within that file. The specific image I'm looking for must start with 'http://www.mysite.com/images/' in the src part of the tag to make sure I'm getting the correct image. To find the occurence, I'm using strpos() to find the character position of the string match. What I then need to do is complete the rest of the URL to get the image name and then output this. So basically I need to get the string that comes after 'http://www.mysite.com/images/' and end when the speech mark occurs to close the src part of the image tag. This will then give me the full URL of my image. How do I do this? Or is there a better way to do this? Thanks. Hey all, I have two objects that contain object instances. I want to compare the properties of the instances and if the old_parent's property doesn't exist as one of the $new_parent's property, then I place the old_parent's property in an array called unneeded_ids. Problem is when I use nested foreach to loop through the instances of the two objects, despite old_parent having only one instance with a property id of 1, because it loops twice, the array gets two "1" values rather than just one: Code: [Select] public function update(){ $vanity_url = new VanityUrl(); $vanity_url->where('user_id',$this->current_user()->id)->get(); $zones = $this->input->post('zones'); $zone = new Zone(); $zone->where_in('name', $zones)->get(); $subcategory = new Category(); $subcategory->where('id',$this->uri->segment(4))->get(); $old_parents = new Category(); $old_parents = $subcategory->related_category->get(); $unneeded_ids = array(); if(!$this->input->post('catupload')){ if($subcategory->save($zone->all)){ redirect("blogs/$vanity_url->url/categories"); } else { echo "Error occurred. Please try again."; } } else { $new_parents = new Category(); $controller = $this->input->post('catupload'); $new_parents->where_in('controller',$controller)->get(); foreach($new_parents as $new){ foreach($old_parents as $old){ if($new->id != $old->id){ array_push($unneeded_ids, $old->id); } } } var_dump($unneeded_ids); } } The var_dump outputs: Code: [Select] array(4) { [0]=> int(126) [1]=> int(127) [2]=> int(126) [3]=> int(127) } but it should only output: Code: [Select] array(2) {[0]=> int(126) [1]=> int(127)} That's because there's only one object instance of old_parent that has a property id value of 126 and the same for 127. The nested for each loop is causing it to iterate twice against the same object and sticking it into the array. How do I get desired result? Thanks for response. Hi, can anyone help me with this. I have a column in my table call "machine", I want to count the number of time each appears and return the results into an array In the example below the number of times each printer appears in the column is returned to the array ____________ machine ____________ fb7500-1 fb7500-2 turbojet turbojet xl1500-1 xl1500-2 canon roland When I run the following I get the names as well as the values and I just want the values?? Result like this Canon 1 FB7500-1 1 Roland 1 TurboJet 2 Vutek QS3200 1 XL1500-1 1 Code: [Select] <?php mysql_connect("localhost","XX","XX"); @mysql_select_db("schedule") or die( "Unable to select database"); date_default_timezone_set('Europe/London'); $query = "SELECT machine, COUNT(machine) FROM maindata GROUP BY machine"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ $array = array( 'key1' => $row[0], 'key2' => $row[1],'key3' => $row[2],'key4' => $row[3], 'key5' => $row[4],'key6' => $row[5],'key7' => $row[6]); extract($array); echo "$key1<br>$key2<br>$key3<br>$key4<br>$key5<br>$key6<br>$key7"; } ?> I have the following array: Code: [Select] Array ( [item0] => Array ( [0] => name1 [1] => name2 [2] => name3 [3] => name4 [4] => name5 )etc... Is it possible to count the number of times a name exists in the compleet array? Thanx in advance! Code: [Select] $LinksText = array(); for ($i=0; ($i < $cnt && $i < $total); $i++ ) { $Text = explode ('#', $Links[$i]); $LinksText[]=$Text[1]; } $count = count($LinksText); print_r(LinksText); print count($LinksText); if($count > 0){ echo "I am not zero" } else { echo "I am zero" } Code: [Select] output: Array ( [0] => [1] => ) 2 Please tell me why the array count is 2. HI - I have tried COUNT and array_count _values but they don't do what I want. I have an array of 'quantities' relating to orders. I want to count how many key=>value pairs have a positive value that is to say Not 0 or null or "". So for example array ( shoes=>5, coats=> 3, ties=>0) The answer I seek in this case would be 2 as ties is zero. I don't want to know what the aggregate of the values are or other such info. I have tried looping through with a foreach loop testing for $value > 0 and I failed miserably Any ideas ? Many Thanks ! <?php $title = "This is an example of a sentence in a paragraph"; $title_array = explode(" ", $title); $count = strlen($title_array[0]); ?> what i want to do is store the count for every characters of every word in the array using a loop e.g the count will be stored in a variable $chr_count then when i access the count for "This" it should be like this $chr_count[0]=4, $char_count[1]=2 etc. Hello; I would like to do the following operation; Count duplicate (adjacent) my code
<?php $Array = array("test", "test", "hello", "test", "world", "world", "world", "hello", "test"); for( $i= 0 ; $i <= 6 ;$i++ ) { $j=1; if ($Array[$i] === $Array[$i+1]) { $j+=$j; echo $j; } ?> and showing something like 2,1,1,3,1,1 Thanks I have been researching php.net regain the function parse_ini_file() I know you can pass it an actual ini file but what if you already saved the ini file as a string and saved the string in a variable can you pass that variable to the function? I am getting the following error Code: [Select] [function.parse-ini-file]: failed to open stream: No such file or directory in this is how I am calling it. Code: [Select] $settings = parse_ini_file($data); echo $settings["mac"]; HI ! I have a table which captures how customers came to be customer ie 'advertisement', 'friend' etc etc. So I want to be able to show / count that N number came from referral or advertisement. My query is simple, I select all from the Media column. This gives me a multi-dimensional array, where each table row, becomes a new sub array. I have 3000 customers ... that's a quite a few sub arrays ! So, a sample print_r of the array looks like this: Code: [Select] Array ( [0] => Array ( [media] => friend ) [1] => Array ( [media] => friend ) [2] => Array ( [media] => friend ) [3] => Array ( [media] => Coming Over From Old Site I have tried using a couple of array functions such as count(array,1) and (array_count_values($media)) but they both don't count the individual values out of the sub arrays. I'm a bit stuck - Can anyone help me ? Many Many Thanks !! I can't get the COUNT of the matching results? Please help. $SomeVar = myname; $queryU = "SELECT * FROM adxone WHERE username = '".$SomeVar."'"; $resultU = mysql_query($queryU); while($scoreP = mysql_fetch_array($resultU)) { $scoreP['roundzAa']; $scoreP['roundzAb']; $scoreP['roundzAc']; $scoreP['roundzAd']; $scoreP['roundzAe']; $scoreP['roundzAf']; $scoreP['roundzAg']; $scoreP['roundzAh']; } $WinV = 'resultA'; $query = "SELECT * FROM acs WHERE resultx = '".$WinV."'"; $result = mysql_query($query); while($scoreUsr = mysql_fetch_array($result)) { $scoreUsr = $scoreM1['winxa']; $scoreUsr = $scoreM1['winxb']; $scoreUsr = $scoreM1['winxc']; $scoreUsr = $scoreM1['winxd']; $scoreUsr = $scoreM1['winxe']; $scoreUsr = $scoreM1['winxf']; $scoreUsr = $scoreM1['winxg']; $scoreUsr = $scoreM1['winxh']; } $count1 = count( array_intersect($resultU, $result) ); echo($count1); mysql_query("UPDATE comp SET id=$count1 WHERE username = '".$SomeVar."'"); ?> I'm using the code below to on echo certain words based on their index/key value, this works fine however, im trying to only do this in exploded quotes... the problem is, exploding quotes turns the portion into an array which cant be passed to str_word_count... how can i do the same thing only inside quotes. So that hello world "this is a test" would become: Code: [Select] Hello World This [0] => Is [1] => a [2] => Test [3] Im trying to always remove say the 4th word in quotations, or maybe the 3rd and 4th, whatever i want to omit i leave out from the echo, so echo $data[0].' '.$data[3].'; would output "This Test" omitting "Is A" Code: [Select] $data = 'hello world "this is a test"'; $data = str_word_count($data, 1); echo $data[1].' '.$data[2].' '.$data[0]; Hi guys I'm kinda stuck..so hopefully you guys can lend me a hand I've got an array containing date elements ("Y-m-d")... I'm trying to output some data into googlecharts..so i need to count how many elements that are in the array with todays date -1 day, todays date-2 days...todays date -3days etc... Up until a set number of days (for example 7 days, 14 days etc).. Any advice on how to go about to achieve this? i HAVE FOLLOWING VALUES IN FEED TABLE
a1a a1b a1c a1d ......
7 8 7 7
8 9 8 8
9 7 7 8
7 8 10 9
6 4 6 7
7 8 8 5
9 7 7 6
This in an array ( select a1a,a1b,a1c,a1d from feed where .....)
1) I want to know how many times each values has occured in the array ( eg: 7 occured 10 times, 8 occured 8 times)
2) How many 7's, 8's, 9's are there in a1a, howmany .....in a1b
Number of times each values occured OVERALL in array as well as also under each head
Please help
I need to find out how many times the "mac" key show is in an array. Code: [Select] Array ( [serial] => Robert [added] => Peter [mac] => 1232091823 [mac] => 7538572375 [mac] =>943820348239804 [place] =>home ) Code: [Select] array ( 0 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 1 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 2 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 3 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 4 => array ( 'id' => '2', 't' => 'Automotive', ), 5 => array ( 'id' => '2', 't' => 'Automotive', ), ) I've get after launching. foreach($array as $value) { if(!isset($new_array[$value['id']])) { $new_array[$value['id']] = $value; $new_array[$value['id']]['cnt'] = 0; } $new_array[$value['id']]['cnt']++; } I get. Code: [Select] array ( 6 => array ( 'id' => '6', 't' => 'Food & Beverage', 'cnt' => 10, ), 2 => array ( 'id' => '2', 't' => 'Automotive', 'cnt' => 2, ), ) where I should get. Code: [Select] array ( 6 => array ( 'id' => '6', 't' => 'Food & Beverage', 'cnt' => [b]4[/b], ), 2 => array ( 'id' => '2', 't' => 'Automotive', 'cnt' => 2, ), ) How can I fix this issue? ----------------------------------------------------- I've received solution on other board. $array = array ( 0 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 1 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 2 => array ( 'id' => '2', 't' => 'Automotive', ), ); $return = array(); $count = array(); foreach( $array as $item ) { if ( !in_array($item, $return) ) { $return[] = $item; $count[ $item['id'] ] = 1; } else { $count[ $item['id'] ]++; } } foreach( $return as & $item ) { $item['cnt'] = (int)$count[ $item['id'] ]; } var_dump( $return ); thank you --- the code above works too, I've put it in WHILE() and didn't noticed it. Sorry Thanks I have an array that looks like this:
array(5642) { [0]=> string(19) "2021-02-10 09:04:48" [1]=> string(19) "2021-02-10 09:04:54" [2]=> string(19) "2021-02-10 09:05:00" [3]=> string(19) "2021-02-10 09:05:06" [4]=> string(19) "2021-02-10 09:05:12" [5]=> string(19) "2021-02-10 09:05:18" [6]=> string(19) "2021-02-10 09:06:18" [7]=> string(19) "2021-02-10 09:06:24" }
I need to group the instances any time there is more than a 6 second gap between elements. So 0 =>5 would be one array and 6 and 7 would be a new array.
Ive been able to produce the following but its not ideal
array(5642) { [0]=> string(19) "2021-02-10 09:04:48" [1]=> string(19) "2021-02-10 09:04:54" [2]=> string(19) "2021-02-10 09:05:00" [3]=> string(19) "2021-02-10 09:05:06" [4]=> string(19) "2021-02-10 09:05:12" [5]=> string(19) "2021-02-10 09:05:18" [6]=> string(19) "end" [7]=> string(19) "2021-02-10 09:06:18" [8]=> string(19) "2021-02-10 09:06:24" }
$burner_time = array(); foreach($Burner_Control_Alm as $new_burner){ $burner_time[] = strtotime($new_burner); } $repl = 'end'; for ($i=1; $i<count($burner_time); $i++) { $value_second = $burner_time[$i]; $value_first = $burner_time[$i-1] === $repl ? $burner_time[$i-2] : $burner_time[$i-1]; if ($value_second > $value_first + 6) { array_splice($burner_time, $i++, 0, $repl); } print_r($burner_time); } Say I have this array: Code: [Select] array( 0 => time 1 => time 2 => to 3 => to 4 => to 5 => fly ); and 2 variables - $occMin and $occMax as number of occurrences. If: Code: [Select] $occMin = 2; $occMax = 3; I want to produce sorted list with most occurrences at the top: Code: [Select] array( 0 => array('to', 3), 1 => array('time', 2) ); My face just melted at the thought of even beginning to tackle this. Is this somewhat possible? would it require a monster function or am I missing some native php functions which could lend a hand? Hi guys, I need your help. I am trying to insert the rows in the mysql database as I input the values in the url bar which it would be like this: Code: [Select] www.mysite.com/testupdate.php?user=tester&pass=test&user1=tester&email=me@shitmail.com&ip=myisp However i have got a error which i don't know how to fix it. Error: Column count doesn't match value count at row 1 <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbusername'); define('DB_PASSWORD', 'mydbpassword'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $username = clean($_GET['user']); $password = clean($_GET['pass']); $adduser = clean($_GET['user1']); $email = clean($_GET['email']); $IP = clean($_GET['ip']); if($username == '') { $errmsg_arr[] = 'username is missing'; $errflag = true; } if($password == '') { $errmsg_arr[] = 'PASSWORD is missing'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $sql = "INSERT INTO `members` (`username`,`email`,`IP`) VALUES ('$adduser','$email','$IP')"; if (!mysql_query($sql,$link)) { die('Error: ' . mysql_error()); } echo "The information have been updated."; } ?> Here's the name of the columns i have got in my database: Code: [Select] username IP I have input the correct columns names, so I can't correct the problem I am getting. Please can you help? |