My array data :
$opt_val =
Array
(
[0] => Array
(
[0] => Array
(
[0] => 0|0|0|P3D
[1] => 0|0|1|P4D
[2] => 0|0|2|P5D
)
[1] => Array
(
[0] => 0|1|0|P3D
[1] => 0|1|1|P4D
[2] => 0|1|2|P5D
)
)
[1] => Array
(
[0] => Array
(
[0] => 1|0|0|P3D
[1] => 1|0|1|P4D
[2] => 1|0|2|P5D
)
[1] => Array
(
[0] => 1|1|0|P3D
[1] => 1|1|1|P4D
[2] => 1|1|2|P5D
)
)
)
I want to join above array with result :
Array
(
[0] => Array
(
[0] => 0|0|0|P3D#0|1|0|P3D (from Array[0][0][0]#Array[0][1][0])
[1] => 0|0|1|P4D#0|1|1|P4D
[2] => 0|0|2|P5D#0|1|2|P5D
)
[1] => Array
(
[0] => 1|0|0|P3D#1|1|0|P3D (from Array[1][0][0]#Array[1][1][0])
[1] => 1|0|1|P4D#1|1|1|P4D
[2] => 1|0|2|P5D#1|1|2|P5D
)
)
My code
for ($ov = 0; $ov < count($opt_val); $ov++) {
for ($ovi = 0; $ovi < count($opt_val[$ov]); $ovi++) {
for ($iv = 0; $iv < count($opt_val[$ov][$iv]); $iv++) {
$im_opt_val[$iv] = implode("#", $opt_val[$ov][$ovi]);
}
$impl_opt_val[$ov] = $im_opt_val;
}
}
Thank you
The following code snippet should do the trick.
<?php
declare(strict_types=1);
$opt_val = [
[
[
'0|0|0|P3D',
'0|0|1|P4D',
'0|0|2|P5D',
],
[
'0|1|0|P3D',
'0|1|1|P4D',
'0|1|2|P5D',
],
],
[
[
'1|0|0|P3D',
'1|0|1|P4D',
'1|0|2|P5D',
],
[
'1|1|0|P3D',
'1|1|1|P4D',
'1|1|2|P5D',
],
],
];
$result = [];
foreach ($opt_val as $outerArrayKey => $outerArray) {
foreach ($outerArray as $innerArray) {
foreach ($innerArray as $innerArrayKey => $innerArrayElement) {
if (!isset($result[$outerArrayKey][$innerArrayKey])) {
$result[$outerArrayKey][$innerArrayKey] = $innerArrayElement;
} else {
$result[$outerArrayKey][$innerArrayKey] .= '#'.$innerArrayElement;
}
}
}
}
var_dump($result);
The output would be:
array(2) {
[0]=>
array(3) {
[0]=>
string(19) "0|0|0|P3D#0|1|0|P3D"
[1]=>
string(19) "0|0|1|P4D#0|1|1|P4D"
[2]=>
string(19) "0|0|2|P5D#0|1|2|P5D"
}
[1]=>
array(3) {
[0]=>
string(19) "1|0|0|P3D#1|1|0|P3D"
[1]=>
string(19) "1|0|1|P4D#1|1|1|P4D"
[2]=>
string(19) "1|0|2|P5D#1|1|2|P5D"
}
}
I hope this work for you:
$finalFinalArray = [];
foreach($outerArray as $key => $innerArray){
$finalArray = [];
$inQueueArray = [];
foreach ($innerArray as $inInnerArray) {
$inQueueArray = array_merge($inQueueArray, $inInnerArray);
}
// Now, inner array is merged
for ($i = 0; $i < count($inQueueArray); $i++){
$finalArray[] = $inQueueArray[$i] + "#" + $inQueueArray[$i+3];
}
$finalFinalArray[] = $finalArray;
}
var_dump($finalFinalArray);
Didn't test it!
Related
This question already has answers here:
How to GROUP BY and SUM PHP Array? [duplicate]
(2 answers)
Closed 9 months ago.
i have bellow array
Array
(
[0] => stdClass Object
(
[gross_weight] => 20.500
[net_weight] => 10.500
[product_id] => 1120280
)
[1] => stdClass Object
(
[gross_weight] => 20.500
[net_weight] => 10.500
[product_id] => 1120281
)
[2] => stdClass Object
(
[gross_weight] => 20.500
[net_weight] => 10.500
[product_id] => 1120281
)
[3] => stdClass Object
(
[gross_weight] => 20.500
[net_weight] => 10.500
[product_id] => 1120280
)
)
i want loop through this records and get the sum of gross_weight and net_weight group by product_id
i tried bellow code but not able to continue from here get the out put
foreach ($my_array as $my_array_data) {
$mark_product_id = $my_array_data->product_id;
$gross_weight_mt = $mrkgdata->gross_weight_mt;
$net_weight_mt = $mrkgdata->net_weight_mt;
}
my desired out put is to get the gross_weight and net_weight for each product id in new array.
Array
(
[0] => stdClass Object
(
[gross_weight] => 41.000
[net_weight] => 21.000
[product_id] => 1120280
)
[1] => stdClass Object
(
[gross_weight] => 41.000
[net_weight] => 21.000
[product_id] => 1120281
)
)
public function getGroupByData($data) {
$groups = array();
foreach ($data as $item) {
$key = $item['product_id'];
if (!array_key_exists($key, $groups)) {
$groups[$key] = array(
'id' => $item['product_id'],
'gross_weight' => $item['gross_weight'],
'net_weight' => $item['net_weight'],
);
} else {
$groups[$key]['gross_weight'] = $groups[$key]['gross_weight'] + $item['gross_weight'];
$groups[$key]['net_weight'] = $groups[$key]['net_weight'] + $item['net_weight'];
}
}
return $groups;
}
use php array as dictionary, and then just add the gross_weight and net_weight with += operator
$json = '
[
{
"gross_weight" : 20500,
"net_weight" : 10500,
"product_id" : 1120280
},
{
"gross_weight" : 20500,
"net_weight" : 10500,
"product_id" : 1120281
},
{
"gross_weight" : 20500,
"net_weight" : 10500,
"product_id" : 1120281
},
{
"gross_weight" : 20500,
"net_weight" : 10500,
"product_id" : 1120280
}
]
';
$x = json_decode($json);
var_dump($x);
$total = array();
foreach($x as $key => $val) {
if(empty($total[$val->product_id])) $total[$val->product_id] = array( "net_weight"=>0, "gross_weight"=>0, "product_id"=>$val->product_id );
$total[$val->product_id]["net_weight"] += $val->net_weight;
$total[$val->product_id]["gross_weight"] += $val->gross_weight;
}
$result = array_values($total);
var_dump($result);
result:
array(4) {
[0]=>
object(stdClass)#8 (3) {
["gross_weight"]=>
int(20500)
["net_weight"]=>
int(10500)
["product_id"]=>
int(1120280)
}
[1]=>
object(stdClass)#7 (3) {
["gross_weight"]=>
int(20500)
["net_weight"]=>
int(10500)
["product_id"]=>
int(1120281)
}
[2]=>
object(stdClass)#6 (3) {
["gross_weight"]=>
int(20500)
["net_weight"]=>
int(10500)
["product_id"]=>
int(1120281)
}
[3]=>
object(stdClass)#5 (3) {
["gross_weight"]=>
int(20500)
["net_weight"]=>
int(10500)
["product_id"]=>
int(1120280)
}
}
array(2) {
[0]=>
array(3) {
["net_weight"]=>
int(21000)
["gross_weight"]=>
int(41000)
["product_id"]=>
int(1120280)
}
[1]=>
array(3) {
["net_weight"]=>
int(21000)
["gross_weight"]=>
int(41000)
["product_id"]=>
int(1120281)
}
}
i've got this array (from a csv file) :
array (
0 => 'entity_id;commission_book;old_price;new_price',
1 => '667;667;667;667',
2 => '668;668;668;668'
)
How to build a new array that looks like :
[0] : (
'entity_id' => '667',
'commission_book' => '667',
'old_price' => '667',
'new_price' => '667',
);
[1] : (
'entity_id' => '668',
'commission_book' => '668',
'old_price' => '668',
'new_price' => '668',
)
In other words, i want to buid 2 objects using the first array, is there any way to perfom that please ? I'm trying for hours now
This is a simply but elegant way to do that:
<?php
$input = [
0 => 'entity_id;commission_book;old_price;new_price',
1 => '667;667;667;667',
2 => '668;668;668;668'
];
$output = [];
// drop header entry
array_shift($input);
// process remaining entries
foreach ($input as $key=>$entry) {
$x = &$output[$key];
list(
$x['entity_id'],
$x['commission_book'],
$x['old_price'],
$x['new_price']
) = explode(';', $entry);
}
print_r($output);
The output of the above is:
Array
(
[0] => Array
(
[new_price] => 667
[old_price] => 667
[commission_book] => 667
[entity_id] => 667
)
[1] => Array
(
[new_price] => 668
[old_price] => 668
[commission_book] => 668
[entity_id] => 668
)
)
Short solution with array_slice and array_combine:
$from_csv = [
0 => 'entity_id;commission_book;old_price;new_price',
1 => '667;667;667;667',
2 => '668;668;668;668'
];
$result = [];
$keys = explode(";", $from_csv[0]); // header fields
foreach(array_slice($from_csv, 1) as $v){
$result[] = array_combine($keys, explode(";", $v));
}
echo '<pre>';
var_dump($result);
// the output:
array(2) {
[0]=>
array(4) {
["entity_id"]=>
string(3) "667"
["commission_book"]=>
string(3) "667"
["old_price"]=>
string(3) "667"
["new_price"]=>
string(3) "667"
}
[1]=>
array(4) {
["entity_id"]=>
string(3) "668"
["commission_book"]=>
string(3) "668"
["old_price"]=>
string(3) "668"
["new_price"]=>
string(3) "668"
}
}
$array = array(
0 => 'entity_id;commission_book;old_price;new_price',
1 => '667;667;667;667',
2 => '668;668;668;668');
$array[0] = explode(";",$array[0]);
$array[1] = explode(";",$array[1]);
$array[2] = explode(";",$array[2]);
$newarray = array();
for ($i=0;$i<count($array[0]);$i++){
$newarray[0][$array[0][$i]] = $array[1][$i];
$newarray[1][$array[0][$i]] = $array[2][$i];
}
echo "<pre>";
var_dump($array);
var_dump($newarray);
echo "</pre>";
I have the following array:
Array
(
[0] => Array
(
[0] => 2015-07-18
[1] => 22 SSH
)
[1] => Array
(
[0] => 2015-07-18
[1] => 80 HTTP
)
[2] => Array
(
[0] => 2015-07-18
[1] => 3389 Remote Desktop
)
[3] => Array
(
[0] => 2015-07-19
[1] => 3389 Remote Desktop
)
[4] => Array
(
[0] => 2015-07-19
[1] => 3389 Remote Desktop
)
)
and need the data counted by day and number of occurrences in the following format:
array(4) {
[0]=>
array(1) {
[0]=> "3389 Remote Desktop"
[1]=> "1,2"
}
[1]=>
array(1) {
[0]=> "22 SSH"
[1]=> "1,0"
}
[2]=>
array(1) {
[0]=> "80 HTTP"
[1]=> "1,0"
}
}
How can I achieve this? I am able to count all occurences but not grouped by date and type like this:
$counts = array();
foreach( $stack_stats_timeline as $value) {
foreach( $value as $k => $v) {
if( !isset( $counts[$k])) $counts[$k] = array();
if( !isset( $counts[$k][$v])) $counts[$k][$v] = 0;
$counts[$k][$v] += 1;
}
}
I think this'll help you to get it work
$result = array();
$count = 1;
foreach ($arr as $key => &$value) {
$hash = $value[1];
$result[$hash][0] = $value[1];
$result[$hash][1][$value[0]] = (isset($result[$hash][1][$value[0]])) ? $count + 1 : $count;
}
print_r(array_values($result));
I am trying to update each language as a multidimensional array to another multidimensional array but it seems to be only saving the last key e.g.(lang_3) but not lang_1 & lang_2. Cracking my head to figure this out. Hope someone can point out my faults. ($country_specs = list of language, $get_code = country code)
$awards = array(
'award_year' => sanitize_array_text_field($_POST['award_year']),
'award_title_user' => sanitize_array_text_field($_POST['award_title_user']),
'award_description_user' => sanitize_array_text_field($_POST['award_description_user'])
);
foreach ($country_specs as $specs => $value) {
if ($value[0] == $get_code ) {
foreach ($value['lang'] as $lang_key => $lang) {
$awards_title = 'award_title_'.$lang_key;
$awards_description = 'award_description_'.$lang_key;
$awards_lang = array(
$awards_title => sanitize_array_text_field($_POST[$awards_title]),
$awards_description => sanitize_array_text_field($_POST[$awards_description])
);
update_user_meta($user_id, 'awards', array_merge($awards,$awards_lang));
}
}
}
Current code output example:
Array (
[award_year] => Array (
[0] => 1999-01
[1] => 2010-02 )
[award_title_user] => Array (
[0] => 2
[1] => tt )
[award_description_user] => Array (
[0] => 2
[1] => ddd )
[award_title_lang3] => Array (
[0] => 2CC
[1] => zz )
[award_description_lang3] => Array (
[0] => 2CCCCCCC
[1] => dzz ) )
Working code as follows.
$awards = array(
'award_year' => sanitize_array_text_field($_POST['award_year']),
'award_title_user' => sanitize_array_text_field($_POST['award_title_user']),
'award_description_user' => sanitize_array_text_field($_POST['award_description_user'])
);
$awards_new_lang = array();
foreach ($country_specs as $specs => $value) {
if ($value[0] == $get_code ) {
foreach ($value['lang'] as $lang_key => $lang) {
$awards_title = 'award_title_'.$lang_key;
$awards_description = 'award_description_'.$lang_key;
$awards_new_lang[$awards_title] = sanitize_array_text_field($_POST[$awards_title]);
$awards_new_lang[$awards_description] = sanitize_array_text_field($_POST[$awards_description]);
}
}
}
$array_merge_new = array_merge($awards, $awards_new_lang);
update_user_meta($user_id, 'awards', $array_merge_new);
I created a new array ($awards_new_lang) and did an array merge with the old array, thus combining both of the arrays together.
Try this code, it outputs as expected, I tried the mimic the variables structures
<?php
$awards = array(
'award_year' => '1999',
'award_title_user' => '2',
'award_description_user' => '2CCCCCCC'
);
$value = array();
$value['lang'] = array(1, 2, 3);
foreach ($value['lang'] as $lang_key) {
$awards_title = 'award_title_'.$lang_key;
$awards_description = 'award_description_'.$lang_key;
$awards_lang = array(
$awards_title => "$lang_key title",
$awards_description => "$lang_key desc"
);
//array merge returns an array, we save the changes here to use them later when the loops are through
$awards = array_merge($awards,$awards_lang);
}
echo '<pre>';
//Final updated version of the array
var_dump($awards);
echo '</pre>';
?>
Outputs:
array(9) {
["award_year"]=>
string(4) "1999"
["award_title_user"]=>
string(1) "2"
["award_description_user"]=>
string(8) "2CCCCCCC"
["award_title_1"]=>
string(7) "1 title"
["award_description_1"]=>
string(6) "1 desc"
["award_title_2"]=>
string(7) "2 title"
["award_description_2"]=>
string(6) "2 desc"
["award_title_3"]=>
string(7) "3 title"
["award_description_3"]=>
string(6) "3 desc"
}
So I'm working on a website with Doctrine as ORM and I get the following array back as a result:
Array (
[0] => Array (
[c_cat_id] => 1
[c_title] => Programas e projetos
[p_menu] => PBA BR 163
[p_page_id] => 1
)
[1] => Array (
[c_cat_id] => 1
[c_title] => Programas e projetos
[p_menu] => Outros projetos
[p_page_id] => 3
)
)
Is it possible to transform this array (in PHP) to something like this:
Array (
[0] => Array (
[c_cat_id] => 1
[c_title] => Programas e projetos
[pages] => Array (
[0] => Array (
[p_page_id] => 1
[p_menu] => PBA BR 163
)
[1] => Array (
[p_page_id] => 3
[p_menu] => Outros projetos
)
)
)
)
Thanks for your help, always eager to learn new ways of doing things and that's why I love StackOverflow ;)
Tested and working:
Code:
$original = array(
array(
"c_cat_id" => "1",
"c_title" => "Programas e projetos",
"p_menu" => "PBA BR 163",
"p_page_id" => "1"),
array(
"c_cat_id" => "1",
"c_title" => "Programas e projetos",
"p_menu" => "Outros projetos",
"p_page_id" => "3"),
array(
"c_cat_id" => "2",
"c_title" => "Another Cat",
"p_menu" => "Outros projetos",
"p_page_id" => "4"),
);
$result = array();
foreach ($original as $row) {
$cat = $row['c_cat_id'];
if (!isset($result[$cat])) {
$result[$row['c_cat_id']] = array(
'c_cat_id'=>$row['c_cat_id'],
'c_title'=>$row['c_title'],
'pages'=>array(),
);
}
unset($row['c_cat_id'],$row['c_title']);
$result[$cat]['pages'][] = $row;
}
var_dump($result);
Result:
array(2) {
[1]=>
array(3) {
["c_cat_id"]=>
string(1) "1"
["c_title"]=>
string(20) "Programas e projetos"
["pages"]=>
array(2) {
[0]=>
array(2) {
["p_menu"]=>
string(10) "PBA BR 163"
["p_page_id"]=>
string(1) "1"
}
[1]=>
array(2) {
["p_menu"]=>
string(15) "Outros projetos"
["p_page_id"]=>
string(1) "3"
}
}
}
[2]=>
array(3) {
["c_cat_id"]=>
string(1) "2"
["c_title"]=>
string(11) "Another Cat"
["pages"]=>
array(1) {
[0]=>
array(2) {
["p_menu"]=>
string(15) "Outros projetos"
["p_page_id"]=>
string(1) "4"
}
}
}
}
It looks like you want to take an Array of pages and turn it into an array of categories with each containing an array of pages.
$inputArray = array(...); // whatever you have originally
$catArray = array();
foreach($inputArray as $page) {
addToCatArray($page);
}
function addToCatArray($page) {
$found = false;
foreach($catArray as $cat) {
if ($cat['c_cat_id'] == $page['c_cat_id'] {
$newPage = array('p_page_id' => $page['p_page_id'], 'p_menu' => $page['p_menu']);
$cat['pages'][] = $newPage;
$found = true;
break;
}
}
if (!$found) { // create a new category
$newCat = array('c_cat_id' => $page['c_cat_id'], 'c_title' => $page['c_title']);
$newPage = array('p_page_id' => $page['p_page_id'], 'p_menu' => $page['p_menu']);
$newCat['pages'] = array($newPage);
$catArray[] = $newCat;
}
}