Split array into two arrays by value even or odd 5 - php

I want to split the following array into odd and even array by e.g [SUBJECT_CODE] => 05
Array
(
[0] => Array
(
[0] => English
[subject_name] => English
[1] => E-I
[subject_abr] => E-I
[2] =>
[ENROL_NO] =>
[3] => 2013
[YEAR_] => 2013
[4] => 1
[EXAM_CODE] => 1
[5] => 42701
[ROLL_NO] => 42701
[6] => 01
[SUBJECT_CODE] => 01
)
[1] => Array
(
[0] => English
[subject_name] => English
[1] => E-II
[subject_abr] => E-II
[2] => 027-B/FMSGUK-2011
[ENROL_NO] => 027-B/FMSGUK-2011
[3] => 2013
[YEAR_] => 2013
[4] => 1
[EXAM_CODE] => 1
[5] => 42701
[ROLL_NO] => 42701
[6] => 02
[SUBJECT_CODE] => 02
)
[2] => Array
(
[0] => Urdu
[subject_name] => Urdu
[1] => U-I
[subject_abr] => U-I
[2] =>
[ENROL_NO] =>
[3] => 2013
[YEAR_] => 2013
[4] => 1
[EXAM_CODE] => 1
[5] => 42701
[ROLL_NO] => 42701
[6] => 05
[SUBJECT_CODE] => 05
)
)

For this, a simple foreach should suffice. Consider this example:
// $values is your original array
$new_values = array();
foreach ($values as $key => $value) {
if($value['SUBJECT_CODE'] & 1) {
$new_values['odd'][] = $value;
} else {
$new_values['even'][] = $value;
}
}
echo '<pre>';
print_r($new_values);
echo '</pre>';

Step 1: Traverse through the array
$odd = array();
$even = array();
foreach ($arr as $key => $value) {
if ($key % 2 == 0) {
$even[] = $value;
}
else {
$odd[] = $value;
}
}
}
The odd value are stored in the $odd array and even in the $even.
You can print_r($odd)

Related

Array issue with echoing the resutls

This is my parsed array. I parse some elements and placed them into my array $results. I think i am somewhere wrong cause i want to echo everything speacially the array [display] but i can't
Array
(
[Forologiki_Periodos] => 1ος Μήνας 2020
[Hmerologiaki_periodos] => 01/01/2020 - 31/01/2020
[Katastasi_ypoxrewsis] => Έχουν Υποβληθεί Δηλώσεις
[Hmerologiaki_periodos_apo] => 01/01/2020
[Hmerologiaki_periodos_ews] => 31/01/2020
[display] => Array
(
[0] => doDisplayDeclarationsList(document.displayDeclarationsListForm
[1] => vatF2
[2] => 2020
[3] => oneMonth
[4] => 01/01/2020
[5] => 31/01/2020
[6] => 01/01/2020
[7] => 31/01/2020
)
)
Array
(
[Forologiki_Periodos] => 2ος Μήνας 2020
[Hmerologiaki_periodos] => 01/02/2020 - 29/02/2020
[Katastasi_ypoxrewsis] => Έχουν Υποβληθεί Δηλώσεις
[Hmerologiaki_periodos_apo] => 01/02/2020
[Hmerologiaki_periodos_ews] => 29/02/2020
[display] => Array
(
[0] => doDisplayDeclarationsList(document.displayDeclarationsListForm
[1] => vatF2
[2] => 2020
[3] => oneMonth
[4] => 01/02/2020
[5] => 29/02/2020
[6] => 01/02/2020
[7] => 29/02/2020
)
)
Array
(
[Forologiki_Periodos] => 3ος Μήνας 2020
[Hmerologiaki_periodos] => 01/03/2020 - 31/03/2020
[Katastasi_ypoxrewsis] => Έχουν Υποβληθεί Δηλώσεις
[Hmerologiaki_periodos_apo] => 01/03/2020
[Hmerologiaki_periodos_ews] => 31/03/2020
[display] => Array
(
[0] => doDisplayDeclarationsList(document.displayDeclarationsListForm
[1] => vatF2
[2] => 2020
[3] => oneMonth
[4] => 01/03/2020
[5] => 31/03/2020
[6] => 01/03/2020
[7] => 31/03/2020
)
)
foreach ($result as $value){
echo $value;
}
With this foreach i echo only the last element of $value, it should be echo all the 12 values. Also how can i get into the array [display] and echo values ?
You can loop directy on the display as
foreach ($result['display'] as $display) {
echo $display . '<br>';
}
But you can imbricate 2 loops
foreach ($result as $value) {
if (is_array($value)) {
foreach ($value as $display) {
echo '----' . $display . '<br>';
}
} else {
echo $value . '<br>';
}
}
The best solution is to use a function:
function displayArray($array)
{
foreach ($array as $each) {
if (is_array($each)) {
displayArray($each);
}
echo $each . '<br>';
}
}
So you can call the function like this:
displayArray($result);
Pls try this:
foreach (#$result['display'] as $key=>$value){
echo $value;
}

Remove internal array in php

I have to remove an internal array from an array. Actually, the array is obtained by decoding JSON, and can go upto n levels. I need to remove an internal array from an array of its parent based on the key which is dynamic. Below is the code that I have tried referring to answers on php arrays.
$quotationHistory = json_decode($quotationCollection->getHistory(), true);
$quotationId = 5;
foreach ($quotationHistory as $sales_id => $history) {
foreach($history as $quotationIdValue => $values) {
if ($quotationId == $quotationIdValue) {
unset ($history[$quotationIdValue]);
}
}
}
sample:
Array
(
[1] => Array
(
[5] => Array
(
[0] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[1] => Array
(
[0] => 12
[1] => 8490.0000
[2] => 21-10-2016 11:43:40am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[2] => Array
(
[0] => 45
[1] => 8490.0000
[2] => 21-10-2016 11:43:54am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[3] => Array
(
[0] => 11
[1] => 8490.0000
[2] => 21-10-2016 11:44:04am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[4] => Array
(
[0] => 54
[1] => 8490.0000
[2] => 21-10-2016 11:44:16am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
)
)
)
Now, I want to remove the second level data with key = 5
You'd better do the unset on the original array:
$quotationHistory = json_decode($quotationCollection->getHistory(), true);
foreach($quotationHistory as $sales_id => $history) {
foreach($history as $quotationIdValue => $values) {
if($quotationId == $quotationIdValue){
unset($quotationHistory[$sales_id][$quotationIdValue]);
}
}
}
The reason is that the internal array is passed as a copy. But you can also specify an assignation by reference:
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference (official Php doc).
$quotationHistory = json_decode($quotationCollection->getHistory(), true);
foreach($quotationHistory as $sales_id => &$history) {
foreach($history as $quotationIdValue => &$values) {
if($quotationId == $quotationIdValue){
unset($$history[$quotationIdValue]);
}
}
}
You will probably want a recursive iterator since the return can be any level down in the array (presumably that's what you mean by n levels). One note, it would remove any key with the same value so it would remove any key with 5. You would be better to recurse to remove the key based on value rtg:
$arr = array(
'one'=>array(
'one'=>'and a half',
'two'=>array(
'three'=>'three and a half'
)
)
);
function recurse($array,$remove=false)
{
foreach($array as $key => $value) {
if($key != $remove){
if(is_array($value)) {
$new[$key] = recurse($value,$remove);
}
else
$new[$key] = $value;
}
}
return $new;
}
# Run the iterator to remove the key named "three"
print_r(recurse($arr,'three'));
If you need to search by value, this should work:
function recurseValue($array,$remove=false)
{
foreach($array as $key => $value) {
if(is_array($value)) {
$new[$key] = recurseValue($value,$remove);
}
else {
if($value != $remove){
$new[$key] = $value;
}
}
}
return $new;
}
# Run the iterator to remove the key with the value named "Three and a half"
print_r(recurseValue($arr,'three and a half'));
Gives you:
Array
(
[one] => Array
(
[one] => and a half
[two] =>
)
)
This last option will normalize the array to one level so you can loop over it normally:
function recurseArray($array,&$new,$bkey = false)
{
foreach($array as $key => $value) {
if(is_array($value)) {
recurseArray($value,$new,$key);
}
else {
$new[$bkey][] = $value;
}
}
}
$new = array();
recurseArray($arr,$new);
print_r($new);
Gives you:
Array
(
[0] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[1] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[2] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[3] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
)

Create Array From Current Array using PHP

This is the result of array after i build it using array_push function from mssql result.
Array
(
[0] => Array
(
[STICKER] => FALCON
[MONTH] => 1
[JUM] => 65826210.00
)
[1] => Array
(
[STICKER] => FALCON
[MONTH] => 2
[JUM] => 68070573.00
)
[2] => Array
(
[STICKER] => FALCON
[MONTH] => 3
[JUM] => 99053067.60
)
[3] =>
[4] => Array
(
[STICKER] => HRD
[MONTH] => 2
[JUM] => 1521400.00
)
[5] => Array
(
[STICKER] => HRD
[MONTH] => 3
[JUM] => 2093200.00
)
)
I need to convert array above into this structure:
Array
(
[0] => Array
(
[0] =>
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array
(
[0] => FALCON
[1] => 65826210.00
[2] => 68070573.00
[3] => 99053067.60
)
[2] => Array
(
[0] => HRD
[1] => 0
[2] => 1521400.00
[3] => 2093200.00
)
)
Note:
Array[0] values would be 1,2,3 (this is actualy month, i just input up to 3 in order to get the code not too long. but it will be up to 12 (Jan - Dec)).
If from original array, there is none value (example from array[3]), then it will be convert to new array[2]->[1] with value 0.
Any help would be very appreciated.
Thanks all!.
Try this,
If any month is not mentioned from stickers the jum considered as 0,
$array = array(
array('STICKER' => 'FALCON', 'MONTH' => 1, 'JUM' => 65826210.00),
array('STICKER' => 'FALCON', 'MONTH' => 2, 'JUM' => 68070573.00),
array('STICKER' => 'FALCON', 'MONTH' => 3, 'JUM' => 99053067.60),
array(),
array('STICKER' => 'HRD', 'MONTH' => 2, 'JUM' => 1521400.00),
array('STICKER' => 'HRD', 'MONTH' => 3, 'JUM' => 2093200.00),
);
$result[0][] = '';
foreach ($array as $key => $res) {
if (!empty($res)) {
$result[0][$res['MONTH']] = $res['MONTH'];
$result1[$res['STICKER']][$res['MONTH']] = $res['JUM'];
}
}
foreach ($result1 as $key => $res) {
$fin = array();
foreach ($res as $key1 => $re) {
foreach ($result[0] as $key2 => $month) {
if ($month != '') {
if (array_key_exists($month, $res)) {
if (!array_key_exists($month, $fin) && $month == $key1) {
$fin[$month] = $re;
}
} else {
$fin[$month] = 0;
}
}
}
}
$result[] = array_merge(array($key), $fin);
}
echo'<pre>';
print_r($result);
echo'<pre>';
Result:
Array
(
[0] => Array
(
[0] =>
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array
(
[0] => FALCON
[1] => 65826210
[2] => 68070573
[3] => 99053067.6
)
[2] => Array
(
[0] => HRD
[1] => 0
[2] => 1521400
[3] => 2093200
)
)
I hope this is used to achieve your output(you mentioned in above question)!!
try something like this
not the issue with the empty array i could not understand how the program would know that it is STICKER HRD so it will be filled with zero.But you could later check if every month "isset" and if it is not act as it is zero
$arr1=array(
array('STICKER'=>'FALCON','MONTH'=>1,'JUM'=>65826210.00),
array('STICKER'=>'FALCON','MONTH'=>2,'JUM'=>68070573.00),
array('STICKER'=>'FALCON','MONTH'=>3,'JUM'=>99053067.60),
array(),
array('STICKER'=>'HRD','MONTH'=>2,'JUM'=>1521400.00),
array('STICKER'=>'HRD','MONTH'=>3,'JUM'=>2093200.00),
);
$arr2=array();
$arr3=array();
$arr2[0][0]="";
foreach($arr1 as $key => $val){
if(!empty($val)){
$arr2[0][$val['MONTH']]=$val['MONTH'];
//group each STICKER
$arr3[$val['STICKER']][]=$val['JUM'];
}
}
//transfer the grouped data to arr2
foreach($arr3 as $key => $val){
$tmp_arr=array($key);
$arr2[]=array_merge($tmp_arr,$val);
}
print_r($arr2);

How to loop over associative array using foreach loop construct in following scenairo?

I've an array titled $rebate_by_product:
Array
(
[op] => preview
[id] =>
[form_submitted] => yes
[company_id] => 46
[1] => Array
(
[pack] => 10
[quantity] => 20
[volume] => 30
[units] => 9
[amount] => 40
[rebate_start_date] => 2014-05-01
[rebate_expiry_date] => 2014-05-05
[applicable_states] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[rebate_total_count] => 5000
[products] => Array
(
[1] => 9
[2] => 10
)
)
[2] => Array
(
[pack] => 50
[quantity] => 60
[volume] => 70
[units] => 10
[amount] => 80
[rebate_start_date] => 2014-05-06
[rebate_expiry_date] => 2014-05-10
[applicable_states] => Array
(
[0] => 14
[1] => 15
[2] => 16
)
[rebate_total_count] => 10000
[products] => Array
(
[1] => 11
[2] => 8
)
)
[3] => Array
(
[pack] => 100
[quantity] => 200
[volume] => 300
[units] => 7
[amount] => 400
[rebate_start_date] => 2014-05-21
[rebate_expiry_date] => 2014-05-30
[applicable_states] => Array
(
[0] => 26
[1] => 33
[2] => 42
)
[rebate_total_count] => 9999
[products] => Array
(
[1] => 9
[2] => 8
)
)
[multiselect] => 42
)
You can observe from above array that it has few elements which are not array but it has three such elements which are themselves array and even few of its data elements are also arrays so how to loop over this kind of array using foreach loop?
If you just want to print each one the just use foreach loop. Consider this example:
$product_keys = array(); // edited
// loop them, if its an array, loop inside it again
foreach($rebate_by_product as $index => $element) {
if(is_array($element)) {
foreach($element as $key => $value) {
if(is_array($value)) {
// EDITED
if($key == 'products') {
$product_keys = array_merge($product_keys, $value);
}
$value = implode(',', $value);
echo "$key => $value <br/>";
} else {
echo "$key => $value <br/>";
}
}
} else {
echo "$index => $element <br/>";
}
}
// if product items has duplicates check here (edited)
if(count($product_keys) != count(array_unique($product_keys))) {
echo "<script>alert('This array has duplicate products');</script>";
} else {
echo "<script>alert('Products are ok');</script>";
}
Or if you want, you cant just use iterators on this one:
$recursive = new RecursiveIteratorIterator(new RecursiveArrayIterator($rebate_by_product));
foreach($recursive as $key => $value) {
echo "$key => $value <br/>";
}
I'd propose you're use a recursive approach to bring all the entries of the array on the same level and then print this array:
function loopArray($inputVal,$inputKey = "") {
if(is_array($inputVal)) {
$output = array();
foreach($inputVal as $key => $value) {
$output = array_merge($output,loopArray($value,$key));
}
return $output;
} else {
return array($inputKey => $inputVal);
}
}
// Just for presenting:
$yourArray = array(
"1" => "1",
array(
"2.1" => "2.1",
array(
"2.2.1" => "2.2.1"
)
),
"3" => "3",
array(
"4.1" => "4.1"
)
);
$newArray = loopArray($yourArray);
// > array("1" => 1,"2.1" => "2.1","2.2.1" => "2.2.1","3" => "3","4.1" => "4.1")
foreach($newArray as $key => $value) {
echo $key." => ".$value."<br/>";
}
// > 1 => 1
// > 2.1 => 2.1
// > 2.2.1 => 2.2.1
// > 3 => 3
// > 4.1 => 4.1

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.

Categories