Unset an multidimensional array by key - php

$series = array();
while($row = mysql_fetch_assoc($result)) {
$series[$row["data_id"]][] = $row;
}
The output from a print_r on $series yields for two example series:
Array (
[1] => Array ( [0] => Array ( [id] => 1 [data_id] => 1 [time_id] => 1
[data] => 1 ) [1] => Array ( [id] => 2 [data_id] => 1 [time_id] => 2
[data] => 3 ) )
[2] => Array ( [0] => Array ( [id] => 6 [data_id] => 2 [time_id] => 1
[data] => 7 ) [1] => Array ( [id] => 7 [data_id] => 2 [time_id] => 2
[data] => 4 ) )
My question: how do I unset the multidimensional array so it contains only [data] and none of the other keys? I still want $series to contain [1] and [2] but I do not want the respective sub-arrays to contain any other keys other than [data].
In fact, since I am reducing the subarrays to contain a single key, I would really like to get rid of the subarrays altogether so that I have two arrays:
$series[1] = array(1,3) and
$series[2] = array(7,4)

Try this :
$series = array();
while($row = mysql_fetch_assoc($result)) {
$series[$row["data_id"]][] = $row['data'];
}

I think you can loop in your array and build a new one keeping only data details
$array = array ('1' => array ( '0' => array ( 'id' => 1, 'data_id' => 1, 'time_id' => 1, 'data' => 1 ), '1' => array ( 'id' => 2, 'data_id' => 1, 'time_id' => 2, 'data' => 3 ), ),
'2' => array ( '0' => array ( 'id' => 6, 'data_id' => 2, 'time_id' => 1, 'data' => 7 ), '1' => array ( 'id' => 7, 'data_id' => 2, 'time_id' => 2, 'data' => 4 ) ));
$i= 0;
$n= 0;
$series = array();
foreach($array as $dato)
{
$series[$i] = array();
foreach($dato as $data)
{
foreach($data as $key => $value)
{
if($key == 'data')
{
$series[$i][$n] = $value;
$n++;
}
}
}
$n = 0;
$i++;
}
var_dump($series);
This will output
array (size=2)
0 =>
array (size=2)
0 => int 1
1 => int 3
1 =>
array (size=2)
0 => int 7
1 => int 4
Live demo

Related

sum of unique values in array

I have below array $billitems_taxes
[0] => Array
(
[id] => 1
[tax_name] => A
[tax_value] => 12
)
[1] => Array
(
[id] => 2
[tax_name] => A
[tax_value] => 8
)
[2] => Array
(
[id] => 3
[tax_name] => B
[tax_value] => 12
)
and I want output as below, find two common tax_name and do some of same and then create a new array.
[0] => Array
(
[id] => 1
[tax_name] => A
[tax_value] => 20
)
[1] => Array
(
[id] => 3
[tax_name] => B
[tax_value] => 12
)
I tried with below code, but it did not return a correct array.
$return_array = [];
foreach($billitems_taxes as $b)
{
$return_array['tax_name'] = $b->tax_name;
$return_array['tax_value'] += $b->tax_value;
}
First off, you have an array of arrays, not objects.
Then your loop needs to know if it has already seen a this tax name which will already be in the new array to check that I used array_key_exists()
$return_array = [];
foreach($billitems_taxes as $b)
{
if ( array_key_exists($b['tax_name'], $return_array) ) {
$return_array[$b['tax_name']]['tax_value'] += $b['tax_value'];
} else {
$return_array[$b['tax_name']] = $b;
}
}
RESULT
Array(
[A] => Array
([id] => 1
[tax_name] => A
[tax_value] => 20
)
[B] => Array
([id] => 3
[tax_name] => B
[tax_value] => 12
)
)
And if its important for the array to be numerically indexed just add
$return_array = array_values($return_array);
after the end of the loop
You must group by 'tax_name' and must sum 'tax_value'.
$billitems_taxes = [
['id' => 1, 'tax_name' => 'A', 'tax_value' => 12],
['id' => 2, 'tax_name' => 'A', 'tax_value' => 8],
['id' => 3, 'tax_name' => 'B', 'tax_value' => 12]
];
$result = [];
foreach($billitems_taxes as $row){
$groupKey = $row['tax_name'];
if(array_key_exists($groupKey,$result)){
$result[$groupKey]['tax_value'] += $row['tax_value'];
} else {
$result[$groupKey] = $row;
}
}
$result = array_values($result);
echo '<pre>';
var_export($result);
/*
array (
0 =>
array (
'id' => 1,
'tax_name' => 'A',
'tax_value' => 20,
),
1 =>
array (
'id' => 3,
'tax_name' => 'B',
'tax_value' => 12,
),
)
*/
The solution with the external class tableArray is very simple. The result is the same.
$result = tableArray::create($billitems_taxes)
->filterGroupAggregate(['tax_value' => 'SUM'],['tax_name'])
->fetchAll()
;

In for loop how to check current array value with all previous array in php

In for loop how to check current value with each and every previous value using php
my array:
In array list [prolabelpos] =>0 having three times, how to execute a [prolabelpos] =>0 only one time in for loop . how to check current array with all previous value and [prolabelpos] =>0 execute once in the for loop
Array (
[0] => Array ( [productlabel_id] => 6 [prolabelpos] => 0 )
[1] => Array ( [productlabel_id] => 5 [prolabelpos] => 6 )
[2] => Array ( [productlabel_id] => 4 [prolabelpos] => 0 )
[3] => Array ( [productlabel_id] => 3 [prolabelpos] => 5 )
[4] => Array ( [productlabel_id] => 2 [prolabelpos] => 0 )
)
my code:
<?php
$prev = null;
foreach ($result as $key => $value) {
$label_position = $value['prolabelpos'];
if ($prev != $label_position) {
echo "my code";
}
$prev = $label_position;
}
You can approach this in foreach OR array_map
$arr =
Array (
'0' => Array ( 'productlabel_id' => 6, 'prolabelpos' => 0 ),
'1' => Array ( 'productlabel_id' => 5, 'prolabelpos' => 6 ),
'2' => Array ( 'productlabel_id' => 4, 'prolabelpos' => 0 ),
'3' => Array ( 'productlabel_id' => 3, 'prolabelpos' => 5 ),
'4' => Array ( 'productlabel_id' => 2 ,'prolabelpos' => 0 )
);
$traversed = array();
foreach($arr as $value){
if(in_array($value['prolabelpos'], $traversed)){
//This has been traversed before
}else{
/* Apply your Logic */
$traversed[] = $value['prolabelpos'];
}
}
Using array_map
$arr = Array (
'0' => Array ( 'productlabel_id' => 6, 'prolabelpos' => 0 ),
'1' => Array ( 'productlabel_id' => 5, 'prolabelpos' => 6 ),
'2' => Array ( 'productlabel_id' => 4, 'prolabelpos' => 0 ),
'3' => Array ( 'productlabel_id' => 3, 'prolabelpos' => 5 ),
'4' => Array ( 'productlabel_id' => 2 ,'prolabelpos' => 0 )
);
$traversed = array();
array_map(function($v) use (&$traversed){
if(in_array($v['prolabelpos'], $traversed)){
//This has been traversed before
}else{
/* Apply your Logic */
$traversed[] = $v['prolabelpos'];
}
}, $arr);

how to convert associative array to different array?

how to convert associative array to different array?
This is my array
$array=Array (
services => Array ( [0] => 6, [1] => 1, [2] => 3 ),
subservices => Array ( [0] => 'No data',[1] => 2 ,[2] => 'No data' ),
price=> Array ( [0] => 124, [1] => 789, [2] => 895 ),
);
and i want convert to
Array (
[0] => Array ( [services] => 6, [subservices] => 'No data', [price] => 124 )
[1] => Array ( [services] => 1, [subservices] => 2, [price] => 789 )
[2] => Array ( [services] => 3, [subservices] => 'No data', [price] => 895 )
)
How to do?
$outArray=array();
for($i=0;$i<count($sourceArray['services']);$i++)
{
$outArray[]=array('services'=>$sourceArray['services'][$i],'subservices'=>$sourceArray['subservices'][$i],'price'=>$sourceArray['price'][$i]);
}
Here is a dynamic approach. This will also allow for additional values in your sub arrays.
Hope it helps:
$array = array (
'services' => Array ( '0' => 6, '1' => 1, '2' => 3),
'subservices' => Array ( '0' => 'No data', '1' => 2, '2' => 'No data'),
'price' => Array ( '0' => 124, '1' => 789, '2' => 895)
);
//Get array keys.
$keys = array_keys($array);
//Iterate through the array.
for($i = 0; $i < count($array); $i++){
//Iterate through each subarray.
for($j = 0; $j < count($array[$keys[$i]]); $j++){
//Here we are checking to see if you have more data per element than your initial key count.
if($keys[$j]){
$index = $keys[$j];
} else {
$index = $j;
}
//Append results to the output array.
$results[$i][$index] = $array[$keys[$i]][$j];
}
}
echo '<pre>';
print_r($results);
echo '</pre>';
This will output:
Array
(
[0] => Array
(
[services] => 6
[subservices] => 1
[price] => 3
)
[1] => Array
(
[services] => No data
[subservices] => 2
[price] => No data
)
[2] => Array
(
[services] => 124
[subservices] => 789
[price] => 895
)
)

Array merge or push key and values of one array into another array

I have 2 arrays and want to add key & value of one array into each member of another array.
The first array is :
Array
(
[0] => Array
(
[supply_id] => 2
)
[1] => Array
(
[supply_id] => 4
)
[2] => Array
(
[supply_id] => 5
)
)
The second array is :
Array
(
[status] => 1
[t1_id] => 59
)
The result I need is :
Array
(
[0] => Array
(
[supply_id] => 2,
[status] => 1,
[t1_id] => 59,
)
[1] => Array
(
[supply_id] => 4,
[status] => 1,
[t1_id] => 59,
)
[2] => Array
(
[supply_id] => 5,
[status] => 1,
[t1_id] => 59,
)
)
It looks easy I think , but I could not solve it, any body can help me please ?
you can try this:
$res = array();
foreach($secondArray as $k => $v){
$res[$k] = array_merge($secondArray[$k], $firstArray[$k]);
}
Loop through first array then merge values in new array:
$array_1= array
(
0 => array
(
"supply_id" => 2
),
1 => array
(
"supply_id" => 4
),
2 => array
(
"supply_id" => 5
),
);
$array_2=array
(
"status" => 1,
"t1_id" => 59
);
$new_array = array();
foreach ($array_1 as $key => $value) {
$new_array[] = array_merge($value,$array_2);
}
var_dump($new_array);
o/p:
array (size=3)
0 =>
array (size=3)
'supply_id' => int 2
'status' => int 1
't1_id' => int 59
1 =>
array (size=3)
'supply_id' => int 4
'status' => int 1
't1_id' => int 59
2 =>
array (size=3)
'supply_id' => int 5
'status' => int 1
't1_id' => int 59
Here we are using simple foreach loop for achieving desired output.
Try this code snippet here
foreach($firstArray as $key => &$value)
{
$value= array_merge($value,$secondArray);
}
print_r($array);

Merge arrays (PHP)

How combine arrays in this way?
source:
Array
(
[0] => Array
(
[id] => 3
[title] => book
[tval] => 10000
)
[1] => Array
(
[id] => 3
[title] => book
[tval] => 1700
)
[3] => Array
(
[id] => 27
[title] => fruit
[tval] => 3000
)
.......
)
result:
Array
(
[0] => Array
(
[id] => 3
[title] => book
[tval] => 10000,1700
)
[1] => Array
(
[id] => 27
[title] => fruit
[tval] => 3000
)
.......
)
please help to solve this problem,
thanks!!!
sorry for bad english(
This should work:
$result = array();
foreach($array as $elem) {
$key = $elem['id'];
if (isset($result[$key])) {
$result[$key]['tval'] .= ',' . $elem['tval'];
} else {
$result[$key] = $elem;
}
}
This basically groups elements by id, concatenating tvals (separated by ,).
Simply building slightly on user576875's method:
$a = array ( 0 => array ( 'id' => 3,
'title' => 'book',
'tval' => 10000
),
1 => array ( 'id' => 3,
'title' => 'book',
'tval' => 1700
),
3 => array ( 'id' => 27,
'bcalias' => 'fruit',
'tval' => 3000
)
);
$result = array();
foreach ($a as $elem) {
$key = $elem['id'];
if (isset($result[$key])) {
$result[$key]['tval'] .= ',' . $elem['tval'];
} else {
$result[$key] = $elem;
}
}
$result = array_merge($result);
var_dump($result);
gives a result of:
array
0 =>
array
'id' => int 3
'title' => string 'book' (length=4)
'tval' => string '10000,1700' (length=10)
1 =>
array
'id' => int 27
'bcalias' => string 'fruit' (length=5)
'tval' => int 3000
The only real difference is the array_merge() to reset the keys

Categories