find the minimum and maximum value in array group php - php

Array
(
[0] => Array
(
[package] => LTE_15AGB
[value] => Array
(
[0] => 52690
[1] => 24700
[2] => 43972
[3] => 506417
[4] => 488125
[5] => 935918
[6] => 1322816
[7] => 1189040
[8] => 2805279
[9] => 2764825
[10] => 1688294
[11] => 1228812
[12] => 2232345
[13] => 3356143
[14] => 1193213
[15] => 167589
[16] => 1373104
[17] => 691411
[18] => 1398647
[19] => 5
)
)
[1] => Array
(
[package] => LTE_15AGB_NT
[value] => Array
(
[0] => 953370
[1] => 151168
[2] => 37605
[3] => 428769
[4] => 755222
[5] => 1146719
[6] => 494289
[7] => 889002
[8] => 307200
[9] => 127972
[10] => 2764815
[11] => 1426224
[12] => 262669
[13] => 648757
[14] => 1485
[15] => 1202
[16] => 998
[17] => 1
)
)
)
This is what I have tried:
$tmp = array();
foreach($arrayName as $arg){
$tmp[$arg['package']][] = $arg['value'];
}
$output = array();
foreach($tmp as $type => $labels){
$output[] = array( 'package' => $type, 'value' => $labels, );
}
print_r(($output))

Try this:
foreach ($your_array as $subarr) {
echo $subarr[package]." minimum = ";
echo min($subarr[value])." and maximum = ";
echo max($subarr[value])."<br>";
}
this will output each package name together with the minimum and maximum values.

Simply use the min() and max() functions.
Your code:
foreach(array_column($array, 'value') as $key => $values){
echo PHP_EOL . 'SubArray '. $key .' min = '. min($values) . ' and max value = '. max($values);
}
output is:
SubArray 0 min = 5 and max value = 3356143
SubArray 1 min = 1 and max value = 2764815
References:
PHP min() manual
PHP max()) manual
PHP array_column
manual
Live demo: https://eval.in/941702

#Arebhy Sri, You should search about array in php, It's like basic problem.
$finalArray = [];
foreach ($mainArr as $subArr){
$array = $subArr['value'];
sort($array);
$tempArray['minimum'] = reset($array);
$tempArray['maximum'] = end($array);
$tempArray['package'] = $subArr['package'];
$finalArray[] = $tempArray;
}
$finalArray //you can use
I am using simple foreach and two functions of array reset() and end().
reset(): Returns the value of the first array element, or FALSE if the array is empty.
end(): Returns the value of the last element or FALSE for empty array.

Related

get count of entries in multi-dimensional array with comma separated values- php

How to get count of entries in an array below?
I tried Count arrays comma separated values and didnot get desired solution.Please suggest a method.
For below sample count is 11.
$sample= Array (
[0] => Array (
[0] => Array ( [attendance] => 2012SD71,2010SD94 )
[1] => Array ( [attendance] => 2003SD18,2003SD19 )
[2] => Array ( [attendance] => 2003SD23,2003SD28 ))
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] => Array (
[0] => Array ( [attendance] => 2012SD81,2010SD84 )
[1] => Array ( [attendance] => 2003SD18,2003SD19,2004SD14 ) ) [14] =>
[15] =>
);
A simple recursive function should work that uses explode() and array_merge():
function recurse($array,&$new)
{
foreach($array as $key => $value) {
if(is_array($value)) {
recurse($value,$new);
}
else {
if(!empty($value)) {
$exp = array_filter(explode(',',$value));
$new = array_merge($new,$exp);
}
}
}
}
# Create a storage array
$new = array();
# Run the recursive function
recurse($sample,$new);
# Show count
print_r(count($new));

Build an array from 3 arrays with PHP

I've some problem to construct an array.
Array A:
Array
(
[0] => 2015-09-13
[1] => 2015-09-14
[2] => 2015-09-15
[3] => 2015-09-16
[4] => 2015-09-17
[5] => 2015-09-18
[6] => 2015-09-19
)
Array B:
Array
(
[0] => 1
[1] => 8
)
Array C:
Array
(
[0] => Leaves-19
[1] => Shifts-18
[2] => Shifts-18
[3] => Shifts-18
[4] => Shifts-18
[5] => Shifts-18
[6] => Leaves-19
[7] => Leaves-19
[8] => Shifts-12
[9] => Shifts-12
[10] => Shifts-12
[11] => Shifts-12
[12] => Shifts-12
[13] => Leaves-19
)
Desired final output:
Array
(
[0] => 2015-09-13|1|Leaves-19
[1] => 2015-09-14|1|Shifts-18
[2] => 2015-09-15|1|Shifts-18
[3] => 2015-09-16|1|Shifts-18
[4] => 2015-09-17|1|Shifts-18
[5] => 2015-09-18|1|Shifts-18
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
[8] => 2015-09-14|8|Shifts-12
[9] => 2015-09-15|8|Shifts-12
[10] => 2015-09-16|8|Shifts-12
[11] => 2015-09-17|8|Shifts-12
[12] => 2015-09-18|8|Shifts-12
[13] => 2015-09-19|8|Leaves-19
)
I'm lost in for and foreach.
Here's the logic:
1st parameter is a date and it come's form array B. It is repeat
after 6 entries.
2nd parameter is the user id. It changes after 6 entries and pass to the next id.
3rd parameter is an entry of array B.
Oter informations:
The arrays don't have the same length.
Array A, counts 6 entries.
Array B, counts a random entries.
Array C, is Array A x 2.
I already tried to make a for for my array B and after a foreach in array A, but it wasn't functional.
I do not know where I need to start.
Hope I will have any help or cue.
Thanks a lot.
you can use modulus operator
$OutputArray = Array();
for($i=0; $i < max(count($a1),count($a2),count($a3)); $i++){
array_push($OutputArray, $a1[ $i % count($a1) ] . "|" .
$a2[ $i % count($a2) ] . "|" . $a3[ $i % count($a3) ]);
}
print_r($OutputArray);
you get:
Array
(
[0] => 2015-09-13|1|Leaves-19
[1] => 2015-09-14|8|Shifts-18
[2] => 2015-09-15|1|Shifts-18
[3] => 2015-09-16|8|Shifts-18
[4] => 2015-09-17|1|Shifts-18
[5] => 2015-09-18|8|Shifts-18
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
[8] => 2015-09-14|1|Shifts-12
[9] => 2015-09-15|8|Shifts-12
[10] => 2015-09-16|1|Shifts-12
[11] => 2015-09-17|8|Shifts-12
[12] => 2015-09-18|1|Shifts-12
[13] => 2015-09-19|8|Leaves-19
)
if you want in order (expected):
$OutputArray = Array();
$max = max(count($a1),count($a2),count($a3));
for($i=0; $i < $max; $i++){
array_push($OutputArray, $a1[$i%count($a1)] . "|" .
$a2[ $i*count($a2) / $max ] . "|" . $a3[$i%count($a3)]);
}
print_r($OutputArray);
you get:
Array
(
[0] => 2015-09-13|1|Leaves-19
[1] => 2015-09-14|1|Shifts-18
[2] => 2015-09-15|1|Shifts-18
[3] => 2015-09-16|1|Shifts-18
[4] => 2015-09-17|1|Shifts-18
[5] => 2015-09-18|1|Shifts-18
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
[8] => 2015-09-14|8|Shifts-12
[9] => 2015-09-15|8|Shifts-12
[10] => 2015-09-16|8|Shifts-12
[11] => 2015-09-17|8|Shifts-12
[12] => 2015-09-18|8|Shifts-12
[13] => 2015-09-19|8|Leaves-19
)
Try this, it iterates over the biggest array and condition the counting of the minor ones to its desired listing behavior.
<?php
$arrA = ['2015-09-13','2015-09-14','2015-09-15','2015-09-16',
'2015-09-17','2015-09-18','2015-09-19'];
$arrB = [1,8];
$arrC = ['Leaves-19','Shifts-18','Shifts-18','Shifts-18','Shifts-18','Shifts-18',
'Leaves-19','Leaves-19','Shifts-12','Shifts-12','Shifts-12','Shifts-12','Shifts-12',
'Leaves-19'];
$a = $b = 0;
for ($c = 0; $c < count($arrC); $c++) {
$arrC[$c] = $arrA[$a].'|'.$arrB[$b].'|'.$arrC[$c];
$a++;
if ($a == count($arrA)) {
$a = 0;
$b++;
}
};
echo "<pre>";
print_r($arrC);
OUTPUT:
Array
(
[0] => 2015-09-13|1|Leaves-19
[1] => 2015-09-14|1|Shifts-18
[2] => 2015-09-15|1|Shifts-18
[3] => 2015-09-16|1|Shifts-18
[4] => 2015-09-17|1|Shifts-18
[5] => 2015-09-18|1|Shifts-18
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
[8] => 2015-09-14|8|Shifts-12
[9] => 2015-09-15|8|Shifts-12
[10] => 2015-09-16|8|Shifts-12
[11] => 2015-09-17|8|Shifts-12
[12] => 2015-09-18|8|Shifts-12
[13] => 2015-09-19|8|Leaves-19
)
My version:
$arrayA = array('2015-09-13','2015-09-14','2015-09-15','2015-09-16','2015-09-17','2015-09-18','2015-09-19');
$arrayB = array(1,8);
$arrayC = array('Leaves-19','Leaves-18','Leaves-17','Leaves-16','Leaves-15','Leaves-14','Leaves-13','Leaves-12','Leaves-11','Leaves-10','Leaves-9','Leaves-8','Leaves-7','leaves-6');
$output = array();
$count = 0;
foreach($arrayB as $arrayBElement){
foreach($arrayA as $arrayAElement){
$output[] = $arrayC[$count] . '|' . $arrayAElement . '|' . $arrayBElement;
$count++;
}
}
var_dump($output);
E: removed erroneous count
This stinks of a homework problem so I'm going to answer in pseudocode so hopefully you can apply some hints and come to the solution yourself.
It appears that maybe Array B ([1,8]) is a list of indexes at which to start printing out the next index. Your own statement (2nd parameter is the user id. It changes after 6 entries and pass to the next id.) is not accurate based on your desired final output:
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
This moves to the next id at index 7 (count 8) not index 6 (count 7, or one after 6 entries).
So My pseudo solution would be this:
Create a new array by repeating Array A until it matches the length of Array C
Append Array B to Array A by finding the element of Array B that is equal to or less than (1+index):
0 => '1', 1 => '1' ... 7 => '8'
Concatenate Array C to every array with index parity (eg. newArray[ 0 ] . ArrayC[ 0 ])
This gives you the final output you desire.
try this code, I have named your 3 arrays as $arrA, $arrB and $arrC
$arrA = Array(
'2015-09-13',
'2015-09-14',
'2015-09-15',
'2015-09-16',
'2015-09-17',
'2015-09-18',
'2015-09-19'
);
$arrB = array(1,8);
$arrC = array(
'Leaves-19',
'Shifts-18',
'Shifts-18',
'Shifts-18',
'Shifts-18',
'Shifts-18',
'Leaves-19',
'Leaves-19',
'Shifts-12',
'Shifts-12',
'Shifts-12',
'Shifts-12',
'Shifts-12',
'Leaves-19'
);
$output = array();
$indx = 0;
foreach($arrB as $b){
foreach($arrA as $a){
$output[] = $a.'|'.$b.'|'.$arrC[$indx];
$indx++;/*new line addded*/
}
//$indx++;/*remove this line from here*/
}
echo "<pre>";
print_r($output);
echo "</pre>";
With different sizes try guess maxlength
//Create arrays
var Adates= ["2015-09-13", "2015-09-14", "2015-09-15"];
var Bnums= ["1", "2", "3"];
var Cnums= ["Leaves-19", "Shifts-18", "Shifts-18"];
//get size
var maxLenght=0;
if(Adates.length>maxLenght) maxLenght=Adates.length;
if(Bnums.length>maxLenght) maxLenght=Bnums.length;
if(Cnums.length>maxLenght) maxLenght=Cnums.length;
//merge and populate
var OutputArray;
for (i = 0; i < maxLenght; i++) {
OutputArray[i]=Adates[i]+"|"+Bnums[i]+"|"+Cnums[i];
}

Filter all other values except a specific URL from array of URLs PHP

I want filter array in PHP.
suppose, i have many URLs in a single array.
eg; $someArray ie;
Array (
[0] => javascript:signin('https://login.alibaba.com')
[2] =>http://www.alibaba.com
[3] => http://www.alibaba.com/Products?cd=buyhome
[4] => http://www.alibaba.com/today_new/catalogs/0.html
[5] => http://www.alibaba.com/help/search-for-products.html
[6] => http://resources.alibaba.com/trade_safe/home.htm
[7] => http://us.my.alibaba.com/product/buyoffer/myalibaba/post_buying_lead_no_member.htm [8] => http://www.alibaba.com/sell/sell.htm
[9] => http://sourcing.alibaba.com/rfq_search_list.htm?availability=y&tracelog=sell_br_20111229
[10] => http://www.alibaba.com/help/how_to_sell/join_alibaba.html
[11] => http://us.my.alibaba.com/product/post_product.htm
[12] => http://resources.alibaba.com/
[13] => http://ask.alibaba.com
[14] => http://resources.alibaba.com/forum/trade_related.htm
[15] => http://tradeshow.alibaba.com/
[16] => http://us.my.alibaba.com/
[17] => http://us.my.alibaba.com/mcadmin/inbox/inboxList.htm
[18] => http://us.my.alibaba.com/product/post_product_interface.htm
[20] => http://trademanager.alibaba.com/
[21] => http://www.alibaba.com/trade/servlet/page/static/paid_memberships/index
[22] => http://us.favorite.alibaba.com
[23] => http://www.alibaba.com/trade/help/helpcenter
[24] => http://www.alibaba.com/help
[25] => http://resources.alibaba.com/trade_safe/complaint.html
[26] => http://legal.alibaba.com/legal/site/login/login.htm?site_type=international&language_id=english
[27] => http://www.alibaba.com/help/contact-us.html#askquestion
[28] => javascript:showFeedBackWindow()
[29] => javascript:void(0)
[30] => http://www.example.com/help
[30] => http://www.example.com/about
);
See the bold values.I only want these values in array... means I want to delete all other values except those values have example.com in value.
please help.
I hope this will helps
$someArray = array('http://www.alibaba.com', ' http://us.my.alibaba.com/', 'http://www.example.com/help', 'http://www.example.com/di' );
$goodLink = 'example.com';
foreach ($someArray as $key => $link) {
if(strpos($link, $goodLink) === false) unset($someArray[$key]);
}
Output
Array ( [2] => http://www.example.com/help [3] => http://www.example.com/di )
Use array_filter with a callback function as below
$someArray = array("javascript:signin('https://login.alibaba.com')", "http://www.example.com/help", "http://www.example.com/about", "http://www.alibaba.com");
$new_arr = array_filter($someArray, "filter");
function filter($element) {
if (strpos($element,'example.com') !== false) {
return $element;
}
else {
return;
}
}
print_r($new_arr);
Output
Array
(
[1] => http://www.example.com/help
[2] => http://www.example.com/about
)
Working demo: http://codepad.org/UPVhHvlJ
Use array_map() & strpos(). Example:
$url_array = [
'http://www.google.com/',
'http://www.example.com/help',
'http://www.example.com/about',
'http://www.facebook.com/',
];
$result = array_filter(array_map(function($v){
if(strpos($v, 'example.com') !== false){
return $v;
}
}, $url_array));
print '<pre>';
print_r($result);
print '</pre>';
Reference:
array_map()
array_filter()
strpos()

Multidimensional Array Listing Printing Php

I have following array with php code
I could not find where I am mistaken
I'm trying to filter some of these array results and delete them. When I try to list them I could not succeded
array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'wp-config.php',
'wp-content' =>
array (
'uploads' =>
array (
2013 =>
array (
'05' =>
array (
0 => 'kabeduvarkad-1024x768.jpg',
1 => 'kabeduvarkad-150x150.jpg',
2 => 'kabeduvarkad-300x225.jpg',
3 => 'kabeduvarkad-940x198.jpg',
4 => 'kabeduvarkad.jpg',
5 => 'kabeduvarkad1-1000x288.jpg',
6 => 'kabeduvarkad1-1024x768.jpg',
7 => 'kabeduvarkad1-150x150.jpg',
8 => 'kabeduvarkad1-300x225.jpg',
9 => 'kabeduvarkad1-400x300.jpg',
10 => 'kabeduvarkad1.jpg',
11 => 'kabeduvarkad2-1000x288.jpg',
12 => 'kabeduvarkad2-1024x768.jpg',
13 => 'kabeduvarkad2-150x150.jpg',
14 => 'kabeduvarkad2-300x225.jpg',
15 => 'kabeduvarkad2-400x300.jpg',
16 => 'kabeduvarkad2.jpg',
),
10 =>
array (
),
),
2014 =>
array (
'02' =>
array (
),
),
),
),
'wp-update' =>
array (
0 => 'wp-update.tar',
1 => 'wp-update.tar.gz',
2 => 'wp-update1.tar',
3 => 'wp-update1.tar.gz',
),
4 => 'wp-update.tar.gz',
)
This is my function
function listArrayRecursive($array_name, $ident = ''){
$result = array();
foreach ($array_name as $k => $v){
if (is_array($v)){
$result[] = listArrayRecursive($v, $ident.'/'.$k);
}else{
$result[] = $ident. '/' . $v . '<br>';
}
}
return $result;
}
I have following result
Array
(
[0] => /do-update.php<br>
[1] => /sitemap.xml<br>
[2] => /sitemap.xml.gz<br>
[3] => /wp-config.php<br>
[4] => Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[0] => /wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg<br>
[1] => /wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg<br>
[2] => /wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>
[3] => /wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg<br>
[4] => /wp-content/uploads/2013/05/kabeduvarkad.jpg<br>
[5] => /wp-content/uploads/2013/05/kabeduvarkad1-1000x288.jpg<br>
[6] => /wp-content/uploads/2013/05/kabeduvarkad1-1024x768.jpg<br>
[7] => /wp-content/uploads/2013/05/kabeduvarkad1-150x150.jpg<br>
[8] => /wp-content/uploads/2013/05/kabeduvarkad1-300x225.jpg<br>
[9] => /wp-content/uploads/2013/05/kabeduvarkad1-400x300.jpg<br>
[10] => /wp-content/uploads/2013/05/kabeduvarkad1.jpg<br>
[11] => /wp-content/uploads/2013/05/kabeduvarkad2-1000x288.jpg<br>
[12] => /wp-content/uploads/2013/05/kabeduvarkad2-1024x768.jpg<br>
[13] => /wp-content/uploads/2013/05/kabeduvarkad2-150x150.jpg<br>
[14] => /wp-content/uploads/2013/05/kabeduvarkad2-300x225.jpg<br>
[15] => /wp-content/uploads/2013/05/kabeduvarkad2-400x300.jpg<br>
[16] => /wp-content/uploads/2013/05/kabeduvarkad2.jpg<br>
)
[1] => Array
(
)
)
[1] => Array
(
[0] => Array
(
)
)
)
)
[5] => Array
(
[0] => /wp-update/wp-update.tar<br>
[1] => /wp-update/wp-update.tar.gz<br>
[2] => /wp-update/wp-update1.tar<br>
[3] => /wp-update/wp-update1.tar.gz<br>
)
[6] => /wp-update.tar.gz<br>
)
Expected Result is
Array
(
[0] => /do-update.php<br>
[1] => /sitemap.xml<br>
[2] => /sitemap.xml.gz<br>
[3] => /wp-config.php<br>
[4] => /wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg<br>
[5] => /wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg<br>
[6] => /wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>
[7] => /wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg<br>
[8] => /wp-content/uploads/2013/05/kabeduvarkad.jpg<br>
[9] => /wp-content/uploads/2013/05/kabeduvarkad1-1000x288.jpg<br>
[10] => /wp-content/uploads/2013/05/kabeduvarkad1-1024x768.jpg<br>
[11] => /wp-content/uploads/2013/05/kabeduvarkad1-150x150.jpg<br>
[12] => /wp-content/uploads/2013/05/kabeduvarkad1-300x225.jpg<br>
[13] => /wp-content/uploads/2013/05/kabeduvarkad1-400x300.jpg<br>
[14] => /wp-content/uploads/2013/05/kabeduvarkad1.jpg<br>
...
[110] => /wp-update/wp-update.tar<br>
[111] => /wp-update/wp-update.tar.gz<br>
[112] => /wp-update/wp-update1.tar<br>
[113] => /wp-update/wp-update1.tar.gz<br>
[114] => /wp-update.tar.gz<br>
)
You can do it like this:
<?php
// Dummy data source
$data = array(
'/do-update.php',
'/sitemap.xml',
'/sitemap.xml.gz',
'/wp-config.php',
array(
array(
array(
'/wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg',
'/wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg',
'/wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>'
)
)
)
);
// Helper function
function getFiles($data, &$fileList) {
foreach ($data as $dataItem) {
if (is_array($dataItem))
getFiles($dataItem, $fileList);
else
$fileList[] = $dataItem;
}
}
// Debug
echo "<b>Orignal Array</b>";
var_dump($data);
echo "<hr>";
// Helper function usage
echo "<b>Parsed Array</b>";
$fileList = array();
getFiles($data, $fileList);
var_dump($fileList);
?>
Output:
Ok use this function its working
$all=array();
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($it as $v) {
$all[]=$v;
}
print_r($all);
With $var[] = you basically say that you want to add a new Element to $var with an incremented key.
Your Recursive frunction returns an array.
So this array is assigned as a new element in your array.
But what you want is a flat array.
Instead of adding an array to your array like this:
if (is_array($v)){
$result[] = listArrayRecursive($v, $ident.'/'.$k);
Merge the existing arrays like this:
if (is_array($v)){
$tmpResult = listArrayRecursive($v, $ident.'/'.$k);
$result = array_merge($result, $tmpResult);
You can see a working example here.

Convert associative array into indexed

Ive seen a few examples by using array_values, but cant quite make out how to get it to work...
I have an associative array thats passed via POST, I need to convert it into a indexed array...
My print_r($_POST) gives me this... I need all of this put into an indexed array :)
Array (
[fieldnames] => 36771X21X198|36771X21X199|36771X21X200|36771X21X201|36771X21X202
[36771X21X198] => 3434343
[display36771X21X198] => on
[36771X21X199] => 5656565
[display36771X21X199] => on
[36771X21X200] => 89898989
[display36771X21X200] => on
[36771X21X201] => 90909090
[display36771X21X201] => on
[36771X21X202] => 12121212
[display36771X21X202] => on
[move] => movesubmit
[move2] => ONLINE Submit
[thisstep] => 1
[sid] => 36771
[token] => 1234567890
)
Observe this amazing way to convert your $_POST into a numerically indexed array:
$numerical = array_values($_POST);
but what if you want to preserve your keys? Perhaps you want something like this?
$numerical = array();
$sep = ':';
foreach($_POST as $k=>$v)
{
$numerical[] = $k.$sep.$v;
}
$numerical will then have:
Array
(
[0] => fieldnames:36771X21X198|36771X21X199|36771X21X200|36771X21X201|36771X21X202
[1] => 36771X21X198:3434343
[2] => display36771X21X198:on
[3] => 36771X21X199:5656565
[4] => display36771X21X199:on
[5] => 36771X21X200:89898989
[6] => display36771X21X200:on
[7] => 36771X21X201:90909090
[8] => display36771X21X201:on
[9] => 36771X21X202:12121212
[10] => display36771X21X202:on
[11] => move:movesubmit
[12] => move2:ONLINE Submit
[13] => thisstep:1
[14] => sid:36771
[15] => token:1234567890
)
or, for my final example:
$fieldnames_original = explode('|', $_POST['fieldnames']);
$fieldnames_actual = array();
$values = array();
foreach($_POST as $k=>$v)
{
if($k!='fieldnames')
{
$fieldnames_actual[] = $k;
$values[] = $v;
}
}
which will set 3 arrays:
$fieldnames_original:
Array
(
[0] => 36771X21X198
[1] => 36771X21X199
[2] => 36771X21X200
[3] => 36771X21X201
[4] => 36771X21X202
)
$fieldnames_actual:
Array
(
[0] => 36771X21X198
[1] => display36771X21X198
[2] => 36771X21X199
[3] => display36771X21X199
[4] => 36771X21X200
[5] => display36771X21X200
[6] => 36771X21X201
[7] => display36771X21X201
[8] => 36771X21X202
[9] => display36771X21X202
[10] => move
[11] => move2
[12] => thisstep
[13] => sid
[14] => token
)
and $values:
Array
(
[0] => 3434343
[1] => on
[2] => 5656565
[3] => on
[4] => 89898989
[5] => on
[6] => 90909090
[7] => on
[8] => 12121212
[9] => on
[10] => movesubmit
[11] => ONLINE Submit
[12] => 1
[13] => 36771
[14] => 1234567890
)
function
function array_default_key($array) {
$arrayTemp = array();
$i = 0;
foreach ($array as $key => $val) {
$arrayTemp[$i] = $val;
$i++;
}
return $arrayTemp;
}
Pass the associative array as a parameter and it will convert into the default index of the array. For example: we have Array('2014-04-30'=>43,'2014-04-29'=>41) after the call to the function the array will be Array(0=>43,1=>41).
Recursive assoc to indexed converter tested on a small array.
function assoc2indexedMulti($arr) {
// initialize destination indexed array
$indArr = array();
// loop through source
foreach($arr as $val) {
// if the element is array call the recursion
if(is_array($val)) {
$indArr[] = assoc2indexedMulti($val);
// else add the value to destination array
} else {
$indArr[] = $val;
}
}
return $indArr;
}

Categories