Laravel nested array sorting by first value - php

while developing Laravel app I stuck on one problem with array sorting. At the beginning, I used partition problem to split my array into equal chunks by time, but now I have a problem sorting by the id of the array.
My array:
array(2) {
[0]=>
array(3) {
[0]=>
array(8) {
[0]=>
int(4) <----- this is value i need to sort by
[1]=>
int(15)
[2]=>
string(64) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[1]=>
array(8) {
[0]=>
int(6)
[1]=>
int(10)
[2]=>
string(64) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[2]=>
array(8) {
[0]=>
int(2)
[1]=>
int(8)
[2]=>
string(85) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
}
[1]=>
array(3) {
[0]=>
array(8) {
[0]=>
int(3)
[1]=>
int(14)
[2]=>
string(158) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[1]=>
array(8) {
[0]=>
int(5)
[1]=>
int(12)
[2]=>
string(158) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[2]=>
array(8) {
[0]=>
int(1)
[1]=>
int(7)
[2]=>
string(55) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
}
}
I need to sort by the first value of each array, so called my id.
Output I need:
array(2) {
[0]=>
array(3) {
[0]=>
array(8) {
[0]=>
int(1)
[1]=>
int(15)
[2]=>
string(64) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[1]=>
array(8) {
[0]=>
int(2)
[1]=>
int(10)
[2]=>
string(64) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[2]=>
array(8) {
[0]=>
int(3)
[1]=>
int(8)
[2]=>
string(85) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
}
[1]=>
array(3) {
[0]=>
array(8) {
[0]=>
int(4)
[1]=>
int(14)
[2]=>
string(158) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[1]=>
array(8) {
[0]=>
int(5)
[1]=>
int(12)
[2]=>
string(158) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[2]=>
array(8) {
[0]=>
int(6)
[1]=>
int(7)
[2]=>
string(55) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
}
}
maybe any of you guys have any solutions how i could solve this problem, so far i tried a lot of sorting functions, none of them seems to work properly for me.
Thank you!

this may not be the best way to do this, but you can try something like this.
$tempArray = array();
$sortedArray = array();
foreach ($yourArray as $key1 => $arr){// create 2D array
foreach ($arr as $key2 => $arr2){
$tempArray[] = $arr2;
}
}
foreach ($tempArray as $keyTemp => $tempElem){
$sortingArray[] = $tempElem[0];//get array value you need to sort
}
asort($sortingArray);// sort array maintaining the key
foreach ($sortingArray as $keySort => $sortElem){//loop through sorted array to push elements to a new array
$sortedArray[] = $tempArray[$keySort];
}
print_r($sortedArray);

Related

Wordpress admin submenu items array returns skipped value's

i returned a array off all my admin submenu items. but this returns like this:
array(2) { [0]=> string(4) "read" [10]=> string(11) "update_core" } array(0) { } array(4) { [5]=> string(10) "edit_posts" [10]=> string(10) "edit_posts" [15]=> string(17) "manage_categories" [16]=> string(16) "manage_post_tags" } array(3) { [5]=> string(12) "upload_files" [10]=> string(12) "upload_files" [15]=> string(17) "manage_categories" } array(2) { [5]=> string(10) "edit_pages" [10]=> string(10) "edit_pages" } array(0) { } array(4) { [5]=> string(10) "edit_posts" [10]=> string(10) "edit_posts" [15]=> string(17) "manage_categories" [16]=> string(17) "manage_categories" } array(3) { [5]=> string(10) "edit_posts" [10]=> string(10) "edit_posts" [15]=> string(17) "manage_categories" } array(0) { } array(8) { [0]=> string(18) "edit_theme_options" [1]=> string(19) "edit_us_page_blocks" [2]=> string(19) "edit_us_page_blocks" [3]=> string(19) "edit_us_page_blocks" [4]=> string(19) "edit_us_page_blocks" [5]=> string(14) "manage_options" [6]=> string(14) "manage_options" [7]=> string(14) "manage_options" } array(4) { [5]=> string(13) "switch_themes" [6]=> string(9) "customize" [7]=> string(18) "edit_theme_options" [10]=> string(18) "edit_theme_options" } array(2) { [5]=> string(16) "activate_plugins" [10]=> string(15) "install_plugins" } array(3) { [5]=> string(10) "list_users" [10]=> string(12) "create_users" [15]=> string(4) "read" } array(7) { [5]=> string(10) "edit_posts" [10]=> string(6) "import" [15]=> string(6) "export" [20]=> string(23) "view_site_health_checks" [25]=> string(27) "export_others_personal_data" [30]=> string(26) "erase_others_personal_data" [50]=> string(13) "setup_network" } array(5) { [0]=> string(14) "manage_options" [1]=> string(14) "manage_options" [2]=> string(14) "manage_options" [3]=> string(14) "manage_options" [5]=> string(10) "edit_posts" } array(7) { [10]=> string(14) "manage_options" [15]=> string(14) "manage_options" [20]=> string(14) "manage_options" [25]=> string(14) "manage_options" [30]=> string(14) "manage_options" [40]=> string(14) "manage_options" [45]=> string(22) "manage_privacy_options" } array(3) { [0]=> string(14) "manage_options" [1]=> string(14) "manage_options" [2]=> string(14) "manage_options" } array(0) { } array(0) { } array(2) { [0]=> string(14) "manage_options" [1]=> string(4) "read" }
php code:
$subMenuItems = wp_list_pluck($submenu_list, 1);
var_dump($subMenuItems);
i know this array is long, is it me or is this broken
i hope that someone can explain why the array items are not like [0] => .... [1] => .... but random
According to the documentation:
wp_list_pluck( array $list, int|string $field, int|string $index_key = null ): array
Array of found values. If $index_key is set, an array of found values with keys corresponding to $index_key. If $index_key is null, array keys from the original $list will be preserved in the results.
You need to add a third parameter, if you want to conserve indexed keys order.
Regards,

How do I check a json object for the existence of a key with php server side?

Ok, So I am working with the reddit api using the php sdk wrapper by #jcleblanc ( https://github.com/jcleblanc/reddit-php-sdk )
That information is not necessarily relevant to the question but I want to give you a frame of reference so you understand what I am looking to do.
The api returns what I believe is a mixed json object as a response when I make calls to reddit. The response length varies and is not consistent. The only this that is consistent is if the action was successful, it returns the key pair
["success"]=> bool(true)
I want to basically search whatever is returned by the api for the key "success" and I can simply evaluate for true or false. Can anybody help with some code to do that?
Here is a sample complete return:
object(stdClass)#1171 (2) { ["jquery"]=> array(29) { [0]=> array(4) { [0]=> int(0) [1]=> int(1) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(4) "body" } } [1]=> array(4) { [0]=> int(1) [1]=> int(2) [2]=> string(4) "attr" [3]=> string(4) "find" } [2]=> array(4) { [0]=> int(2) [1]=> int(3) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(7) ".status" } } [3]=> array(4) { [0]=> int(3) [1]=> int(4) [2]=> string(4) "attr" [3]=> string(4) "hide" } [4]=> array(4) { [0]=> int(4) [1]=> int(5) [2]=> string(4) "call" [3]=> array(0) { } } [5]=> array(4) { [0]=> int(5) [1]=> int(6) [2]=> string(4) "attr" [3]=> string(4) "html" } [6]=> array(4) { [0]=> int(6) [1]=> int(7) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(0) "" } } [7]=> array(4) { [0]=> int(7) [1]=> int(8) [2]=> string(4) "attr" [3]=> string(3) "end" } [8]=> array(4) { [0]=> int(8) [1]=> int(9) [2]=> string(4) "call" [3]=> array(0) { } } [9]=> array(4) { [0]=> int(1) [1]=> int(10) [2]=> string(4) "attr" [3]=> string(8) "redirect" } [10]=> array(4) { [0]=> int(10) [1]=> int(11) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(66) "https://www.reddit.com/r/mysubreddit/comments/12hyas2/my_no_link_post/" } } [11]=> array(4) { [0]=> int(1) [1]=> int(12) [2]=> string(4) "attr" [3]=> string(4) "find" } [12]=> array(4) { [0]=> int(12) [1]=> int(13) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(11) "*[name=url]" } } [13]=> array(4) { [0]=> int(13) [1]=> int(14) [2]=> string(4) "attr" [3]=> string(3) "val" } [14]=> array(4) { [0]=> int(14) [1]=> int(15) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(0) "" } } [15]=> array(4) { [0]=> int(15) [1]=> int(16) [2]=> string(4) "attr" [3]=> string(3) "end" } [16]=> array(4) { [0]=> int(16) [1]=> int(17) [2]=> string(4) "call" [3]=> array(0) { } } [17]=> array(4) { [0]=> int(1) [1]=> int(18) [2]=> string(4) "attr" [3]=> string(4) "find" } [18]=> array(4) { [0]=> int(18) [1]=> int(19) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(12) "*[name=text]" } } [19]=> array(4) { [0]=> int(19) [1]=> int(20) [2]=> string(4) "attr" [3]=> string(3) "val" } [20]=> array(4) { [0]=> int(20) [1]=> int(21) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(0) "" } } [21]=> array(4) { [0]=> int(21) [1]=> int(22) [2]=> string(4) "attr" [3]=> string(3) "end" } [22]=> array(4) { [0]=> int(22) [1]=> int(23) [2]=> string(4) "call" [3]=> array(0) { } } [23]=> array(4) { [0]=> int(1) [1]=> int(24) [2]=> string(4) "attr" [3]=> string(4) "find" } [24]=> array(4) { [0]=> int(24) [1]=> int(25) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(13) "*[name=title]" } } [25]=> array(4) { [0]=> int(25) [1]=> int(26) [2]=> string(4) "attr" [3]=> string(3) "val" } [26]=> array(4) { [0]=> int(26) [1]=> int(27) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(1) " " } } [27]=> array(4) { [0]=> int(27) [1]=> int(28) [2]=> string(4) "attr" [3]=> string(3) "end" } [28]=> array(4) { [0]=> int(28) [1]=> int(29) [2]=> string(4) "call" [3]=> array(0) { } } } ["success"]=> bool(true) }
The code you gave mean you were calling json_decode($response). This return an object.
It should be:
$resArray = json_decode($response, true)
Which return an associated array.
Then to use:
if (isset($resArray['success'])) {
//your logic here
}
Your object has two properties: jquery and success.
All you need to get the success property is this:
$success = $your_object->success;
As long as the returned object has a success property, that's all you should need to do, regardless of how many other properties it has, or the size of any of its other properties.
If you aren't sure if the object will have the success property, you can use
$success = !empty($your_object->success);
which will evaluate to true if the property exists and is true, and false otherwise.
You might be able to just throw it into
json_decode
If it complains about the format you can do some direct string manipulation beforehand and then you'll be able to reference it directly as an associative array.
Use var_dump to get familiar with the structure after you get a successful parse.

PHP insert data from excel file - Loop error

I'm trying to get the data that I uploaded from excel using foreach and for loop, but the weird thing is when I upload excel file w/ 12 rows it successfully inserted, but when I upload excel w/ 13 rows and up its only get the last row and I'm getting the error Uninitialized Offset :8.
this is the array when I uploaded excel w/ 12 rows.
array(12)
{
[0]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } }
[1]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } }
[2]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(20) [4]=> string(3) "box" [5]=> string(5) "A1016" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } }
[3]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(5) "chico" [3]=> float(7) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "ads" } }
[4]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(125) [2]=> string(9) "pineapple" [3]=> float(8) [4]=> string(3) "box" [5]=> string(5) "A1013" [6]=> float(1102) [7]=> float(42112) [8]=> string(3) "ads" } }
[5]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } }
[6]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } }
[7]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(20) [4]=> string(3) "box" [5]=> string(5) "A1016" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } }
[8]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(5) "chico" [3]=> float(7) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "ads" } }
[9]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(125) [2]=> string(9) "pineapple" [3]=> float(8) [4]=> string(3) "box" [5]=> string(5) "A1013" [6]=> float(1102) [7]=> float(42112) [8]=> string(3) "ads" } }
[10]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } }
[11]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(4) "a1a1" } } }
this is when 13 rows it only get the last row.
array(1)
{
[0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(4) "a1a1" } }
below is my code.
var_dump ($dataRow);
if (isset($dataRow)) {
echo '<pre>';
foreach($dataRow as $key => $dataVal) {
foreach($dataVal as $key => $dataSume) {
$fArray = $dataSume;
for($x=0; $x < 1; $x++ ){
$refs = strlen($fArray[0]);
$this->varOrder->addCount($fArray[0],$fArray[1],$fArray[2],$fArray[3],$fArray[4],$fArray[5],$fArray[6],$fArray[7],$fArray[8],$flag2);
}
}
}
echo '</pre>';
}
$data['mode'] = 'Empty';
$data['message'] = 'Data Successfully Uploaded!';
$this->session->unset_userdata($dataRow);
$this->session->unset_userdata('Count');
$this->load->view('home_view', $data);
My question is why I'm only get the last row when I'm start inserting 13 rows and up, is this on my foreach or for loop syntax?

PHP Codeigniter-Uninitialized string offset:

I'm trying to upload excel file get the value and save it to database, the weird thing is when my excel file has 13 rows and up then the error Uninitialized Offset is occurred.
if (isset($dataRow)) {
foreach($dataRow as $key => $dataVal) :
foreach($dataVal as $key => $dataSume)
$fArray = $dataSume;
$y = count ($dataSume);
for($x=0; $x < 1; $x++ ){
if ( isset($fArray) ){
$this->varOrder->addCount($fArray[0],$fArray[1],$fArray[2],$fArray[3],$fArray[4],$fArray[5],$fArray[6],$fArray[7],$fArray[8],$flag2);
}
}
endforeach;
this is the value of $dataRow array.
array(12) { [0]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } } [1]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } [2]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(20) [4]=> string(3) "box" [5]=> string(5) "A1016" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } [3]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(5) "chico" [3]=> float(7) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "ads" } } [4]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(125) [2]=> string(9) "pineapple" [3]=> float(8) [4]=> string(3) "box" [5]=> string(5) "A1013" [6]=> float(1102) [7]=> float(42112) [8]=> string(3) "ads" } } [5]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } } [6]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } [7]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(20) [4]=> string(3) "box" [5]=> string(5) "A1016" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } [8]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(5) "chico" [3]=> float(7) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "ads" } } [9]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(125) [2]=> string(9) "pineapple" [3]=> float(8) [4]=> string(3) "box" [5]=> string(5) "A1013" [6]=> float(1102) [7]=> float(42112) [8]=> string(3) "ads" } } [10]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } } [11]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } }
this is the only value that system get when the number of rows is 13.
array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(4) "a1a1" } }
I tried to add if ( isset($fArray) ) before the for loop to catch the offset, but still didn't work.
is this problem is on my foreach or for loop?
thanks in advance!

Do action when for loop is at it's last index - 1

I have an function which retrieves specific data from some other functions.
I loop through the retrieved data and explode every result by the # character, the data looks like this:
Not exploded
string(14) "1#28#92#28#103"
string(17) "551#36#112#36#126"
string(17) "651#45#132#45#150"
string(17) "751#59#159#59#179"
string(17) "851#77#194#91#208"
string(18) "951#99#229#129#245"
string(20) "1051#122#264#166#282"
string(20) "1151#145#300#203#318"
string(20) "1251#168#335#240#355"
string(20) "1351#190#371#278#392"
string(20) "1451#213#406#315#428"
string(20) "1551#236#441#352#465"
string(20) "1651#258#477#389#501"
string(20) "1751#281#512#427#538"
string(20) "1851#304#548#464#575"
string(20) "1951#327#583#501#611"
string(20) "2051#349#618#539#648"
string(20) "2151#372#654#576#685"
string(20) "2251#395#689#613#721"
string(20) "2351#417#725#650#758"
string(20) "2451#440#760#688#795"
string(20) "2551#463#795#725#831"
string(20) "2651#486#831#762#868"
string(20) "2751#508#866#799#905"
string(20) "2851#531#902#837#941"
string(20) "2951#554#937#874#978"
string(21) "3051#576#972#911#1015"
string(22) "3151#599#1008#949#1051"
string(22) "3251#618#1040#982#1084"
string(23) "3351#637#1071#1015#1117"
string(23) "3451#655#1102#1049#1149"
string(23) "3551#674#1133#1082#1182"
string(23) "3651#693#1165#1115#1214"
string(23) "3751#711#1196#1148#1247"
string(23) "3851#730#1227#1181#1279"
string(23) "3951#748#1258#1214#1312"
string(23) "4051#767#1290#1247#1344"
string(23) "4151#785#1321#1280#1377"
string(23) "4251#804#1352#1314#1410"
string(23) "4351#823#1383#1347#1442"
string(23) "4451#841#1415#1380#1475"
string(23) "4551#860#1446#1413#1507"
string(23) "4651#878#1477#1446#1540"
string(23) "4751#897#1508#1479#1572"
string(23) "4851#915#1540#1512#1605"
string(23) "4951#934#1571#1546#1637"
Exploded
array(5) {
[0]=>
string(1) "1"
[1]=>
string(2) "28"
[2]=>
string(2) "92"
[3]=>
string(2) "28"
[4]=>
string(3) "103"
}
array(5) {
[0]=>
string(3) "551"
[1]=>
string(2) "36"
[2]=>
string(3) "112"
[3]=>
string(2) "36"
[4]=>
string(3) "126"
}
array(5) {
[0]=>
string(3) "651"
[1]=>
string(2) "45"
[2]=>
string(3) "132"
[3]=>
string(2) "45"
[4]=>
string(3) "150"
}
array(5) {
[0]=>
string(3) "751"
[1]=>
string(2) "59"
[2]=>
string(3) "159"
[3]=>
string(2) "59"
[4]=>
string(3) "179"
}
array(5) {
[0]=>
string(3) "851"
[1]=>
string(2) "77"
[2]=>
string(3) "194"
[3]=>
string(2) "91"
[4]=>
string(3) "208"
}
array(5) {
[0]=>
string(3) "951"
[1]=>
string(2) "99"
[2]=>
string(3) "229"
[3]=>
string(3) "129"
[4]=>
string(3) "245"
}
array(5) {
[0]=>
string(4) "1051"
[1]=>
string(3) "122"
[2]=>
string(3) "264"
[3]=>
string(3) "166"
[4]=>
string(3) "282"
}
array(5) {
[0]=>
string(4) "1151"
[1]=>
string(3) "145"
[2]=>
string(3) "300"
[3]=>
string(3) "203"
[4]=>
string(3) "318"
}
array(5) {
[0]=>
string(4) "1251"
[1]=>
string(3) "168"
[2]=>
string(3) "335"
[3]=>
string(3) "240"
[4]=>
string(3) "355"
}
array(5) {
[0]=>
string(4) "1351"
[1]=>
string(3) "190"
[2]=>
string(3) "371"
[3]=>
string(3) "278"
[4]=>
string(3) "392"
}
array(5) {
[0]=>
string(4) "1451"
[1]=>
string(3) "213"
[2]=>
string(3) "406"
[3]=>
string(3) "315"
[4]=>
string(3) "428"
}
array(5) {
[0]=>
string(4) "1551"
[1]=>
string(3) "236"
[2]=>
string(3) "441"
[3]=>
string(3) "352"
[4]=>
string(3) "465"
}
array(5) {
[0]=>
string(4) "1651"
[1]=>
string(3) "258"
[2]=>
string(3) "477"
[3]=>
string(3) "389"
[4]=>
string(3) "501"
}
array(5) {
[0]=>
string(4) "1751"
[1]=>
string(3) "281"
[2]=>
string(3) "512"
[3]=>
string(3) "427"
[4]=>
string(3) "538"
}
array(5) {
[0]=>
string(4) "1851"
[1]=>
string(3) "304"
[2]=>
string(3) "548"
[3]=>
string(3) "464"
[4]=>
string(3) "575"
}
array(5) {
[0]=>
string(4) "1951"
[1]=>
string(3) "327"
[2]=>
string(3) "583"
[3]=>
string(3) "501"
[4]=>
string(3) "611"
}
array(5) {
[0]=>
string(4) "2051"
[1]=>
string(3) "349"
[2]=>
string(3) "618"
[3]=>
string(3) "539"
[4]=>
string(3) "648"
}
array(5) {
[0]=>
string(4) "2151"
[1]=>
string(3) "372"
[2]=>
string(3) "654"
[3]=>
string(3) "576"
[4]=>
string(3) "685"
}
array(5) {
[0]=>
string(4) "2251"
[1]=>
string(3) "395"
[2]=>
string(3) "689"
[3]=>
string(3) "613"
[4]=>
string(3) "721"
}
array(5) {
[0]=>
string(4) "2351"
[1]=>
string(3) "417"
[2]=>
string(3) "725"
[3]=>
string(3) "650"
[4]=>
string(3) "758"
}
array(5) {
[0]=>
string(4) "2451"
[1]=>
string(3) "440"
[2]=>
string(3) "760"
[3]=>
string(3) "688"
[4]=>
string(3) "795"
}
array(5) {
[0]=>
string(4) "2551"
[1]=>
string(3) "463"
[2]=>
string(3) "795"
[3]=>
string(3) "725"
[4]=>
string(3) "831"
}
array(5) {
[0]=>
string(4) "2651"
[1]=>
string(3) "486"
[2]=>
string(3) "831"
[3]=>
string(3) "762"
[4]=>
string(3) "868"
}
array(5) {
[0]=>
string(4) "2751"
[1]=>
string(3) "508"
[2]=>
string(3) "866"
[3]=>
string(3) "799"
[4]=>
string(3) "905"
}
array(5) {
[0]=>
string(4) "2851"
[1]=>
string(3) "531"
[2]=>
string(3) "902"
[3]=>
string(3) "837"
[4]=>
string(3) "941"
}
array(5) {
[0]=>
string(4) "2951"
[1]=>
string(3) "554"
[2]=>
string(3) "937"
[3]=>
string(3) "874"
[4]=>
string(3) "978"
}
array(5) {
[0]=>
string(4) "3051"
[1]=>
string(3) "576"
[2]=>
string(3) "972"
[3]=>
string(3) "911"
[4]=>
string(4) "1015"
}
array(5) {
[0]=>
string(4) "3151"
[1]=>
string(3) "599"
[2]=>
string(4) "1008"
[3]=>
string(3) "949"
[4]=>
string(4) "1051"
}
array(5) {
[0]=>
string(4) "3251"
[1]=>
string(3) "618"
[2]=>
string(4) "1040"
[3]=>
string(3) "982"
[4]=>
string(4) "1084"
}
array(5) {
[0]=>
string(4) "3351"
[1]=>
string(3) "637"
[2]=>
string(4) "1071"
[3]=>
string(4) "1015"
[4]=>
string(4) "1117"
}
array(5) {
[0]=>
string(4) "3451"
[1]=>
string(3) "655"
[2]=>
string(4) "1102"
[3]=>
string(4) "1049"
[4]=>
string(4) "1149"
}
array(5) {
[0]=>
string(4) "3551"
[1]=>
string(3) "674"
[2]=>
string(4) "1133"
[3]=>
string(4) "1082"
[4]=>
string(4) "1182"
}
array(5) {
[0]=>
string(4) "3651"
[1]=>
string(3) "693"
[2]=>
string(4) "1165"
[3]=>
string(4) "1115"
[4]=>
string(4) "1214"
}
array(5) {
[0]=>
string(4) "3751"
[1]=>
string(3) "711"
[2]=>
string(4) "1196"
[3]=>
string(4) "1148"
[4]=>
string(4) "1247"
}
array(5) {
[0]=>
string(4) "3851"
[1]=>
string(3) "730"
[2]=>
string(4) "1227"
[3]=>
string(4) "1181"
[4]=>
string(4) "1279"
}
array(5) {
[0]=>
string(4) "3951"
[1]=>
string(3) "748"
[2]=>
string(4) "1258"
[3]=>
string(4) "1214"
[4]=>
string(4) "1312"
}
array(5) {
[0]=>
string(4) "4051"
[1]=>
string(3) "767"
[2]=>
string(4) "1290"
[3]=>
string(4) "1247"
[4]=>
string(4) "1344"
}
array(5) {
[0]=>
string(4) "4151"
[1]=>
string(3) "785"
[2]=>
string(4) "1321"
[3]=>
string(4) "1280"
[4]=>
string(4) "1377"
}
array(5) {
[0]=>
string(4) "4251"
[1]=>
string(3) "804"
[2]=>
string(4) "1352"
[3]=>
string(4) "1314"
[4]=>
string(4) "1410"
}
array(5) {
[0]=>
string(4) "4351"
[1]=>
string(3) "823"
[2]=>
string(4) "1383"
[3]=>
string(4) "1347"
[4]=>
string(4) "1442"
}
array(5) {
[0]=>
string(4) "4451"
[1]=>
string(3) "841"
[2]=>
string(4) "1415"
[3]=>
string(4) "1380"
[4]=>
string(4) "1475"
}
array(5) {
[0]=>
string(4) "4551"
[1]=>
string(3) "860"
[2]=>
string(4) "1446"
[3]=>
string(4) "1413"
[4]=>
string(4) "1507"
}
array(5) {
[0]=>
string(4) "4651"
[1]=>
string(3) "878"
[2]=>
string(4) "1477"
[3]=>
string(4) "1446"
[4]=>
string(4) "1540"
}
array(5) {
[0]=>
string(4) "4751"
[1]=>
string(3) "897"
[2]=>
string(4) "1508"
[3]=>
string(4) "1479"
[4]=>
string(4) "1572"
}
array(5) {
[0]=>
string(4) "4851"
[1]=>
string(3) "915"
[2]=>
string(4) "1540"
[3]=>
string(4) "1512"
[4]=>
string(4) "1605"
}
array(5) {
[0]=>
string(4) "4951"
[1]=>
string(3) "934"
[2]=>
string(4) "1571"
[3]=>
string(4) "1546"
[4]=>
string(4) "1637"
}
I want it so whenever the last index - 1 is being handled by the for loop, the code should do a specific action, this is my own try (function) for it:
public function getDataByVehicle(VehicleType $vehicleType, VehicleOwner $vehicleOwner) {
$data = $vehicleType->resolveVehicleTypeWithData($vehicleType);
$dataKey = $vehicleType->resolveDataKey($data, $vehicleOwner);
$dataLocation = $this->data->$dataKey;
if (is_a($vehicleType, "PassengerCar")) {
for ($dataLocationIndex = 0; $dataLocationIndex < count($dataLocation); $dataLocationIndex++) {
$formattedData = explode("#", $dataLocation[$dataLocationIndex]);
var_dump($formattedData);
//My own try!
if (!$dataLocationIndex == count($dataLocation)) {
// do action
}
}
}
return null;
}
Question
How do I make my code do an specific action when the for loop is at its last index - 1?
Note
I want to use an for loop because i want to be able to check what the next index of the array would be, if it's not exists, take the last one it remembered.
The for-loop is only running while: $dataLocationIndex < count($dataLocation), which means you should check the count($dataLocation) minus 1, because the loop never reaches the count of $dataLocation.
//My own try!
if ($dataLocationIndex === count($dataLocation)-1) {
// do action
}
Hope it worked.

Categories