Make first item in array appear at the last item - php
Just to note I dont mean to reverse the whole array, just one item which is at the beginning of the array to also appear at the end of the array
I have an example of an array but i dont know how i can make mine to look the same.
I wanted to create a polygon using co-ordinate, and i found an example on the net that draws the polygon using an array. Below is the array that i found from the site
$polygon = array("-50 30","50 70","100 50","80 10","110 -10","110 -30","-20 -50","-30 -40","10 -10","-10 10","-30 -20","-50 30");
Condition is that the first and last array must be the same. And their example is from static numbers.
In my case i have dynamic list of coordinates from the database and i would wish it to generate something like the example given above.
Here is what i have tried but it does not seem to work. My code now
$query_points = mysqli_query($link,"SELECT * FROM ec");
$row_points = mysqli_fetch_assoc($query_points);
$query_lastpoint = mysqli_query($link,"SELECT * FROM ec"
$row_lastpoint = mysqli_fetch_assoc($query_lastpoint);
$longitude_last = $row_lastpoint['longitude'];
$latitude_last = $row_lastpoint['latitude'];
$polygon= array();
while ($row_points = mysqli_fetch_assoc($query_points))
{
$longitude_xx = $row_points['longitude'];
$latitude_yy = $row_points['latitude'];
$xx_yy = ''.$longitude_xx.' '.$latitude_yy.'';
$polygon[] = $xx_yy;
}
$polygon[] = "$longitude_last $latitude_last";
Its like from the source they use the variable directly as $polygon while mine is $polygon[]; and i cant say $polygon = $polygon[];
Any Assistance?
You can just append it again, if its already there it wont make a differnce
$array[]= $array[0];
otherwise you can do
if (end($array) != reset($array)) { //add the first element here
http://php.net/manual/en/function.end.php
http://php.net/manual/en/function.reset.php
Related
Conditional unset from Guzzle response
I've seen a few questions and the ones worth referencing How can i delete object from json file with PHP based on ID How do you remove an array element in a foreach loop? How to delete object from array inside foreach loop? Unset not working in multiple foreach statements (PHP) The last two from the list are closer to what I'm intending to do. I've got a variable names $rooms which is storing data that comes from a particular API using Guzzle $rooms = Http::post(...); If I do $rooms = json_decode($rooms); this is what I get If I do $rooms = json_decode($rooms, true); this is what I get Now sometimes the group exists in the same level as objectId, visibleOn, ... and it can assume different values So, what I intend to do is delete from $rooms when group isn't set (so that specific value, for example, would have to be deleted) group doesn't have the value bananas. Inspired in the last two questions from the initial list foreach($rooms as $k1 => $room_list) { foreach($room_list as $k2 => $room){ if(isset($room['group'])){ if($room['group'] != "bananas"){ unset($rooms[$k1][$k2]); } } else { unset($rooms[$k1][$k2]); } } } Note that $room['group'] needs to be changed to $room->group depending on if we're passing true in the json_decode() or not. This is the ouput I get if I dd($rooms); after that previous block of code Instead, I'd like to have the same result that I've shown previously in $rooms = json_decode($rooms);, except that instead of having the 100 records it'd give only the ones that match the two desired conditions.
If I am not totally wrong, then this should do the trick for you: $rooms = json_decode($rooms); $rooms->results = array_values(array_filter($rooms->results, function($room) { return property_exists($room, 'group') && $room->group != "banana"; })); Here is a verbose and commented version of this one above: $rooms = json_decode($rooms); // first lets filter our set of data $filteredRooms = array_filter($rooms->results, function($room) { // add your criteria for a valid room entry return property_exists($room, 'group') // the property group exists && $room->group == "banana"; // and its 'banana' }); // If you want to keep the index of the entry just remove the next line $filteredRooms = array_values($filteredRooms); // overwrite the original results with the filtered set $rooms->results = $filteredRooms;
Looking for an element in an array in PHP
I don't know if I'm managing this array in the best way. The array I have is this: $bass = $_POST['bass']; $selected_scale = $_POST['scale']; $major_scales = array ( array("C","D","E","F","G","A","B","C","D","E","F","G","A","B",), array("C#","D#","E#","F#","G#","A#","B#","C#","D#","E#","F#","G#","A#","B#",), array("Db","Eb","F","Gb","Ab","Bb","C","Db","Eb","F","Gb","Ab","Bb","C",), array("D","E","F#","G","A","B","C#","D","E","F#","G","A","B","C#"), array("D#","E#","F##","G#","A#","B#","C##","D#","E#","F##","G#","A#","B#","C##"), array("Eb","F","G","Ab","Bb","C","D","Eb","F","G","Ab","Bb","C","D"), array("E","F#","G#","A","B","C#","D#","E","F#","G#","A","B","C#","D#"), array("E#","F##","G##","A#","B#","C##","D##","E#","F##","G##","A#","B#","C##","D##"), array("Fb","Gb","Ab","Bbb","Cb","Db","Eb","Fb","Gb","Ab","Bbb","Cb","Db","Eb"), array("F","G","A","Bb","C","D","E","F","G","A","Bb","C","D","E"), array("F#","G#","A#","B","C#","D#","E#","F#","G#","A#","B","C#","D#","E#"), array("Gb","Ab","Bb","Cb","Db","Eb","F","Gb","Ab","Bb","Cb","Db","Eb","F"), array("G","A","B","C","D","E","F#","G","A","B","C","D","E","F#"), array("G#","A#","B#","C#","D#","E#","F##","G#","A#","B#","C#","D#","E#","F##"), array("Ab","Bb","C","Db","Eb","F","G","Ab","Bb","C","Db","Eb","F","G"), array("A","B","C#","D","E","F#","G#","A","B","C#","D","E","F#","G#"), array("A#","B#","C##","D#","E#","F##","G##","A#","B#","C##","D#","E#","F##","G##"), array("Bb","C","D","Eb","F","G","A","Bb","C","D","Eb","F","G","A"), array("B","C#","D#","E","F#","G#","A#","B","C#","D#","E","F#","G#","A#"), array("B#","C##","D##","E#","F##","G##","A##","B#","C##","D##","E#","F##","G##","A##"), array("Cb","Db","Eb","Fb","Gb","Ab","Bb","Cb","Db","Eb","Fb","Gb","Ab","Bb") ); $bass is a string, like the one inside the arrays. The $selected_scale is just a number. What I'm trying to do is to find the $bass in one of those array in the position of $selected_scale. Basically, $bass = $major_scales[$selected_scale]. Therefore I want to create a loop in order to get the elements after that. But I don't know how to manage in this case the situation. I've looked everything in internet and try various solutions without success. I'd like to know how can I do it. Thanks
Try to use next loop: // if value exists in mentioned index if (in_array($bass,$major_scales[$selected_scale])){ // index of that value in that array $tmp_ind = array_search($bass,$major_scales[$selected_scale]); // length of the array $len = count($major_scales[$selected_scale]); // store values after this value $res = []; for ($i=$tmp_ind;$i<$len;$i++){ $res[$i] = $major_scales[$selected_scale][$i]; } } print_r($res); Demo1 If you need to find value by index $selected_scale in one of these arrays and also store values after this position: foreach($major_scales as $ar){ if ($ar[$selected_scale] == $bass){ // length of the array $len = count($ar); // store values after this value $res = []; for ($i=$selected_scale;$i<$len;$i++){ $res[$i] = $ar[$i]; } } } print_r($res); Demo2
Nested foreach not filling my dropdown correctly
I'm attempting to make 1 array out of 2 existing arrays (which cannot be modified). In order to do this I'm creating the array in a foreach which is nested in another foreach. The code I used: $language_option = array(); foreach(Languages::getFullSelectOptionsList() as $country_description_1 => $country_code){ foreach(Languages::getFullSelectOptionsList(TRUE) as $country_description_2 => $country_code){ $language_option[$country_code] = $country_description_1.' - '.$country_description_2; } } In this code "Languages::getFullSelectOptionsList()" returns an array with the 1st country descriptions. And "Languages::getFullSelectOptionsList(TRUE)" returns an array with the 2nd country descriptions. This is what my code does: dropdown results But what I'd like it to do is: dropdown wished results As you can see in the first picture only the last array value of "country_description_1" is used instead of using them all. Are there any errors in my code, is this not possible to do or is there an easier way of doing this? Thanks.
Here you can get reference of this code. But This will not work because you need to specify the values where $first_array[$i] $language_option = array(); $first_array = Languages::getFullSelectOptionsList(); $second_array = Languages::getFullSelectOptionsList(TRUE); for($i=0;$i<count($first_array); $i++){ $language_option[$country_code] = $first_array[$i].' - '.$second_array[$i]; } Instead of $first_array[$i].' - '.$second_array[$i] put code according to your array structure to get description or code (key value).
fetch data from model that is called in loop
I have a controller function in CodeIgniter that looks like this: $perm = $this->job_m->getIdByGroup(); foreach($perm as $pe=>$p) { $pId = $p['id']; $result = $this->job_m->getDatapermission($pId); } $data['permission'] = $result; What I need to do is list the data in the result in the view, but I get only the last value while using this method. How can I pass all the results to the view?
Store it in an array. Like this: foreach($perm as $pe=>$p){ $result[] = $this->job_m->getDatapermission($p['id']); }
Because $result is not an array... try this: $result=array(); foreach($perm as $pe=>$p) { $pId = $p['id']; $result[] = $this->job_m->getDatapermission($pId); } $data['permission'] = $result;
Note: My answer uses a counter to enable the display of a single group result when needed. Guessing from your need to loop and display the value of $result, possibly, it is an array or object returned by $query->result(). Things could be a bit complex. Example: if $perm is an array of 5 items( or groups), the counter assigns keys 1 - 5 instead of 0 - 4 as would [] which could be misleading. Using the first view example, you could choose to display a single group value if you wants by passing it via a url segment. Making the code more flexible and reusable. E.g. You want to show just returns for group 2, in my example, $result[2] would do just that else next code runs. See my comments in the code. $perm = $this->job_m->getIdByGroup(); $counter = 1; foreach($perm as $pe=>$p) { $pId = $p['id']; $result[$counter] = $this->job_m->getDatapermission($pId); $counter++; } $data['permission'] = $result; As mentioned above Note: I Added a Counter or Key so you target specific level. If the groups are: Men, Women, Boys, Girls, Children; you'd know women is group two(2) If you desire to display values for just that group, you don't need to rewrite the code below. Just pass the group key would be as easy as telling it by their sequence. To display all the loop without restrictions, use the second view example. To use both, use an if statement for that. ###To access it you could target a specific level like if(isset($permission)){ foreach($permission[2] as $key => $value){ echo $value->columnname; } ###To get all results: foreach($permission as $array){ foreach($array as $key => $value){ echo $value->columnname; } } }
Create PHP array's on the fly
I am having the worst time trying to get this to work. In the following code, I am gathering data from a database query and trying to build a muti-dimensional array object that will keep totals and tally up some information in a specific way. The problem is that instead of getting a value that is incrementing as it should, the value seems to be suffering from the last value it was assigned problem. Here is the code: $REVIEWS = array(); $USER_REVIEWS = array(); $USER_IMGREVS = array(); pseudo-code: loop here which iterates over the DB results creating $date - which is into this function as its called for each day of month $p1user - which is one of the users (there are 3) 'levels' of users $hr - is the hour which is built from the transaction's timestamp $hr = date('H', $row['P1TIMESTAMP']); $p1user = $row['P1USER']; $REVIEWS[$date] += 1; $USER_REVIEWS[$date][$p1user][$hr] += 1; $USER_IMGREVS[$date][$p1user][$hr] += $row['F5']; print "PASS1<br/>\n"; print "Value of Total Reviews: [".$REVIEWS[$date]."]<br/>\n"; print "Value of User Reviews: [".$USER_REVIEWS[$date][$p1user][$hr]."]<br/>\n"; print "Value of Reviewed Images: [".$USER_IMGREVS[$date][$p1user][$hr]."]<br/>\n"; print "<br/><br/>\n"; So - the 'total reviews' increments by one, as it should, for each time i print this. SO far so good. The next two arrays will only print the last values they were assigned, and will not be added together like they should. Why not? I have attempted to do this another way by literally creating the arrays one by one and assigning them in whole to the array containing them - but that also does not seem to work. Any insights?
i don't know how you initilize your array, maybe this will help: // replace this 2 lines: $USER_REVIEWS[$date][$p1user][$hr] += 1; $USER_IMGREVS[$date][$p1user][$hr] += $row['F5']; // with this code: if (!isset($USER_REVIEWS[$date])) $USER_REVIEWS[$date] = array(); if (!isset($USER_REVIEWS[$date][$p1user])) $USER_REVIEWS[$date][$p1user] = array(); if (!isset($USER_REVIEWS[$date][$p1user][$hr])) $USER_REVIEWS[$date][$p1user][$hr] = 0; $USER_REVIEWS[$date][$p1user][$hr] += 1; if (!isset($USER_IMGREVS[$date])) $USER_IMGREVS[$date] = array(); if (!isset($USER_IMGREVS[$date][$p1user])) $USER_IMGREVS[$date][$p1user] = array(); if (!isset($USER_IMGREVS[$date][$p1user][$hr])) $USER_IMGREVS[$date][$p1user][$hr] = 0; $USER_IMGREVS[$date][$p1user][$hr] += $row['F5'];
Sir, I dont understand very well why your coed is not working, but in my first test, I would change these lines: $count = 1; $USER_REVIEWS[$count][$p1user][$hr] += 1; $USER_IMGREVS[$count][$p1user][$hr] += $row['F5']; $count++; Please, check if this code helps you anyway.
Your print statements for those values rely on the value of $p1user: print "Value of User Reviews: [".$USER_REVIEWS[$date][$p1user][$hr]."]<br/>\n"; print "Value of Reviewed Images: [".$USER_IMGREVS[$date][$p1user][$hr]."]<br/>\n"; If you want to print it for all users you should loop over all possible users rather than just using $p1user. Either that or add them up if you want their sum. Edit: Something that was bugging me was your data structure. It doesn't seem to represent your data very well. In your loop why don't you build up useful information that you store at the base of the review array?