I would like to arrange Array before found a specific array key. For example
Following is array.
Array(
[0] => Array([package_name] => 10.4)
[1] => Array([final_total] => 10.4)
[2] => Array([package_name] => 10.5)
[3] => Array([package_name] => 4.5)
[4] => Array([final_total] => 15)
[5] => Array([package_name] => 15.2)
[6] => Array([final_total] => 15.2)
[7] => Array([package_name] => 8.4)
[8] => 8.4
)
And I want like array.
(
[0] => Array
(
[package_name] => array([0]=>10.4),
[final_total] => 10.4
)
[1] => Array
(
[package_name] => array(
[0] => 10.5,
[1] => 4.5
),
[final_total] => 15
)
[2] => Array
(
[package_name] => array([0]=>15.2)
[final_total] => 15.2
)
[3] => Array
(
[package_name] => array([0]=>8.4)
[final_total] => 8.4
)
)
So What i want If final_total key is found from array then set previous values(package_name) of final_total in a array.
Above example you can see there are 4 final_total key's of array so i want to set each package_name's value in a array that are previous value of final_total.
Following is my code.
This is my array
$main = array(array('package_name' => 10.4),array('final_total' => 10.4),array('package_name' => 10.5),array('package_name' => 4.5),array('final_total' => 15)
,array('package_name' => 15.2),array('final_total' => 15.2));
Code.
<?php
$newArray = [];
$newPackag=[];
$previousValue='';
$currentKey=0;
$PreviousKey=0;
$i=0;
$main_keys = array_keys($main);
foreach ($main as $key => $value) {
$curtent_item[] = isset($main[$key]['package_name']) ? $main[$key]['package_name'] : '';
$currentKey = $key;
if(#$main[$key]['final_total'] ==#$value['final_total']){
$previousValue = #$value['package_name'];
$newArray[] = $previousValue;
$myarray= array(#$main[$key]['package_name']);
if (array_key_exists("final_total",$main[$key])){
if($PreviousKey ==0){
$PreviousKey = $key+1;
}else{
$PreviousKey = $key;
}
}else{
$keys = array_keys($main);
$position = array_search($key, $keys);
echo "Curent Key =".$currentKey.'PreviousKey'.$PreviousKey.'</br>';
if($currentKey != $PreviousKey){
$nextKey = $keys[$currentKey+1 ];
}
$newPackag1[] = array('package_name'=>#$myarray);
}
$mainArray = array('package'=>$newPackag1);
}
$i++;
}
echo "<pre>NE page";print_r($newPackag1);
echo "<pre>";print_r($main);
anyone has better and correct solution. Above code which i am trying not able to get desire output.
Here is the snippet with modified data(surely will work for your case too),
$result = [];$i= 0;
foreach ($main as $key => $value) {
if (is_array($value)) {
$k = key($value);$v = array_shift($value);
($k != 'final_total' ? $result[$i][$k][] = $v : $result[$i][$k] = $v);
if($k == 'final_total'){
$i++;
}
} elseif (!empty($value)) {
$result[$i]['final_total'] = $value;
}
}
print_r($result);
Demo
Output:-
Array
(
[0] => Array
(
[package_name] => Array
(
[0] => 6.5
[1] => 9
)
[final_total] => 15.5
)
[1] => Array
(
[package_name] => Array
(
[0] => 10.5
)
[final_total] => 10.5
)
[2] => Array
(
[package_name] => Array
(
[0] => 17.1
)
[final_total] => 17.1
)
[3] => Array
(
[package_name] => Array
(
[0] => 9.8
)
[final_total] => 9.8
)
[4] => Array
(
[package_name] => Array
(
[0] => 16
)
[final_total] => 16
)
[5] => Array
(
[package_name] => Array
(
[0] => 10.5
)
[final_total] => 10.5
)
)
Related
I have 2 multidimensional Arrays with Flightdates (Price, Date, Airline,..). I want to merge and remove duplicates that have the same date but I need to keep the cheaper one. I have always the same flight, but different prices.
$array1 = array(
[0] => Array
(
[price] => 191
[date1] => 22-07-2016
[date2] => 30-07-2016
[airline] => Lufthansa
)
[1] => Array
(
[price] => 80
[date1] => 25-07-2016
[date2] => 30-07-2016
[airline] => Easyjet
)
[2] => Array
(
[price] => 243
[date1] => 10-08-2016
[date2] => 36-08-2016
[airline] => Airberlin
)
);
$array2 = array(
[0] => Array
(
[price] => 230
[date1] => 22-07-2016
[date2] => 30-07-2016
[airline] => Lufthansa
)
[1] => Array
(
[price] => 80
[date1] => 25-07-2016
[date2] => 30-07-2016
[airline] => Easyjet
)
[2] => Array
(
[price] => 200
[date1] => 10-08-2016
[date2] => 36-08-2016
[airline] => Airberlin
)
);
Just loop through the first array and match the price in second array with respect to key of first array and push the values into into new array..
First Answer
<?php
$newArray = array();
foreach($array1 as $key =>$val)
{
if($val['price'] <= $array2[$key]['price'])
{
$newArray[] = $val;
}
else
{
$newArray[] = $array2[$key];
}
}
print_r($newArray);
?>
LIVE EXAMPLE : CLICK HERE
Second Answer
$newArray = array();
foreach($array1 as $key => $val)
{
foreach($array2 as $k => $v)
{
if($val['date1'] == $v['date1'] && $val['date2'] == $v['date2'] && $val['airline'] == $v['airline'])
{
if($val['price'] <= $array2[$key]['price'])
{
$newArray[] = $val;
}
else
{
$newArray[] = $v;
}
}
}
}
print_r($newArray);
LIVE EXAMPLE : CLICK HERE
This will give you :
Array
(
[0] => Array
(
[price] => 191
[date1] => 22-07-2016
[date2] => 30-07-2016
[airline] => Lufthansa
)
[1] => Array
(
[price] => 80
[date1] => 22-07-2016
[date2] => 30-07-2016
[airline] => Easyjet
)
[2] => Array
(
[price] => 200
[date1] => 22-07-2016
[date2] => 30-07-2016
[airline] => Airberlin
)
)
I have build this array structure from dig query data.
[10] => Array
(
[id] => 150
[0] => 200.201.202.23
[1] => dns.name1.com
[2] => 200.201.202.24
[3] => dns.name2.com
[4] => 200.201.202.25
[5] => dns.name3.com
) `
I need something like:
[10] => Array
(
[0] => array ( [0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => array ( [0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => array ( [0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
) `
I'm not sure if this is possible?
Heres the code where i create the array:
At the first time from the dig i use array_push() to add content to it.
$temp = array();
$i = 0;
foreach ($digResult as $single){
if (preg_match('/(?:^|\s+)(\d+)(?:\s+|\n+|$)/', $single )){
$temp []["id"]= $single;
$i++;
}else {
$temp[$i][] = $single;
}
}
This will work for you :
<?php
$dataArray = array(10 => array
(
'id' => 150 ,
0 => '200.201.202.23' ,
1 => 'dns.name1.com',
2 => '200.201.202.24',
3 => 'dns.name2.com',
4 => '200.201.202.25',
5 => 'dns.name3.com',
)
);
$newArray = array();
$id = $dataArray[10]['id'];
for($i=0; $i< 6; $i++)
{
$newArray[10][] = array(0=>$dataArray[10][$i],1=>$dataArray[10][$i+1],'id'=> $id);
$i+=1;
}
print_r($newArray);
?>
This will output
Array
(
[10] => Array
(
[0] => Array
(
[0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => Array
(
[0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => Array
(
[0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
)
)
LIVE EXAMPLE : CLICK HERE
Try this:-
<?php
$array = array(
'id' => '150',
'0' => '200.201.202.23',
'1' => 'dns.name1.com',
'2' => '200.201.202.24',
'3' => 'dns.name2.com',
'4' => '200.201.202.25',
'5' => 'dns.name3.com'
);
$i = 0;
$arrayLenght = (count($array)-2);
$newArray = array();
while ($i <= $arrayLenght) {
$newArray[] = array(
"0" => $array[$i++],
"1" => $array[$i++],
"id" => $array['id']
);
}
echo '<pre>';
print_r($newArray);
echo '</pre>';
?>
Output:-
Array
(
[0] => Array
(
[0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => Array
(
[0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => Array
(
[0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
)
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);
I have the array about:
Array
(
[id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[product] => Array
(
[0] => t-shirt
[1] => earing
[2] => clock
)
[price] => Array
(
[0] => 100.00
[1] => 32.00
[2] => 898.00
)
)
I want to do this:
Array
(
[0] => Array
(
[0] => 1
[1] => t-shirt
[2] => 100.00
)
[1] => Array
(
[0] => 2
[1] => earing
[2] => 32.00
)
[2] => Array
(
[0] => 3
[1] => clock
[2] => 898.00
)
)
You can try with:
$input = array( /* your input array */ );
$output = array();
foreach ($input as $data) {
for ($i = 0; $i < count($data); $i++) {
if (!isset($output[$i])) {
$output[$i] = array();
}
$output[$i][] = $data[$i];
}
}
Well or like this.
$test = array(
'id' => array(
1,2,3
),
'product' => array(
'tshirt', 'ewew', 'shorts'
),
'price' => array(
'10.00', '20.00', '30.00'
)
);
$newarray = array();
foreach($test['id'] as $k => $v){
$newarray[$k] = array(
$v, $test['product'][$k], $test['price'][$k]
);
}
echo '<pre>';
print_r($newarray);
Example live
I have below array and i want to fetch [2] => Array with foreach but it's showing me an error.
for example array name is $other
Array
(
[0] => Array
(
[0] => aaaaaaaaaaaa
[1] => bbbbbbbbbbbb
[2] => cccccccccccc
)
[1] => Array
(
[0] => dddddddddddd
[1] => eeeeeeeeeeee
[2] => ffffffffffff
)
[2] => Array
(
[0] => gggggggggggg
[1] => hhhhhhhhhhhh
[2] => iiiiiiiiiiii
)
)
fetch array:
foreach ($other[2] as $value) {
echo $value.'<br/>';
}
How do I print all the values of the second array?
You need to nest further more
foreach ($other as $arr)
{
foreach($arr as $k=>$v)
{
if($k==2)
{
echo $v.'<br/>';
}
}
}
OUTPUT :
cccccccccccc
ffffffffffff
iiiiiiiiiiii
Try Something like this,
<?php
$x = array
(
0 => array
(
0 => 'aaaaaaaaaaaa',
1 => 'bbbbbbbbbbbb',
2 => 'cccccccccccc'
),
1 => array
(
0 => 'dddddddddddd',
1 => 'eeeeeeeeeeee',
2 => 'ffffffffffff'
),
2 => array
(
0 => 'gggggggggggg',
1 => 'hhhhhhhhhhhh',
2 => 'iiiiiiiiiiii'
)
);
$count = count($x);
$w = $count - 1;
var_dump($x[$w]);
?>