This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 4 months ago.
There is an array with the following format.
I have used array_chunk function to format like the following array.
Array (
[0] => Array
(
[0] => Array
(
[SGST (2.5%)] => 2.5000
)
[1] => Array
(
[CGST (2.5%)] => 2.5000
)
)
[1] => Array
(
[0] => Array
(
[CGST (6%)] => 6.0000
)
[1] => Array
(
[SGST (6%)] => 6.0000
)
)
)
All I need my array to be displayed in the following format
Array
(
[0] => Array
(
[SGST (2.5%)] => 2.5000
[CGST (2.5%)] => 2.5000
)
[1] => Array
(
[CGST (6%)] => 6.0000
[SGST (6%)] => 6.0000
)
)
Help to create such format.Thanks
try combination of array_map, array_reduce with array_merge
To know more about used function check function doc on php.net
<?php
$array = array (
'0' => array
(
'0' => array
(
'SGST (2.5%)' => '2.5000'
),
'1' => array
(
'CGST (2.5%)' => '2.5000'
)
),
'1' => array
(
'0' => array
(
'CGST (6%)' => '6.0000'
),
'1' => array
(
'SGST (6%)' => '6.0000'
)
)
);
echo '<pre>';
$processed = array_map(function($a) { return array_reduce($a, 'array_merge', array()); }, $array);
print_r($processed);
Output
Array
(
[0] => Array
(
[SGST (2.5%)] => 2.5000
[CGST (2.5%)] => 2.5000
)
[1] => Array
(
[CGST (6%)] => 6.0000
[SGST (6%)] => 6.0000
)
)
Related
I have an array that has some keys that are repeating multiple times. Would like to combine them so that I can have them in one array, within the same array.
I have an array of this type;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
)
)
Array
(
[Shoes] => Array
(
[FUBU] => Array
(
[0] => size6
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => White
)
)
)
I would like an output of the following kind;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)
I have tried array_merge, array_merge_recursive but all are not working.
foreach ($county as $value) { print_r(array_merge($value)); } //$value contains the array
foreach ($county as $value) { print_r(array_merge_recursive($value)); } //$value contains the array
Kindly assist if you have an idea on how to solve this in PHP.
You could do it with a few nested array_walk() calls:
$arr=array(array("Shoes" => array("POLO" => array("Size5"))),
array("Shoes" => array("FUBU" => array("size6"))),
array("Bag" => array("HPBAG" => array("Black"))),
array("Bag" => array("HPBAG" => array("White"))));
$res=[];
array_walk($arr,function($a) use (&$res){
array_walk($a, function($ar,$type) use (&$res){
array_walk($ar, function ($arr,$brand) use (&$res,$type){
$res[$type][$brand]=array_merge($res[$type][$brand]??[],$arr);
});
});
});
print_r($res);
See the demo here: https://rextester.com/RFLQ18142
It produces this result:
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)
I've an array:
Array
(
[_edit_lock] => Array
(
[0] => 1434971582:11
)
[_edit_last] => Array
(
[0] => 11
)
[_wp_page_template] => Array
(
[0] => page-templates/langenfeldDreiSpalterMitSiderbarsRL.php
)
[_wpas_done_all] => Array
(
[0] => 1
)
[hefo_before] => Array
(
[0] => 0
)
[hefo_after] => Array
(
[0] => 0
)
[sharing_disabled] => Array
(
[0] => 1
)
[spacious_page_layout] => Array
(
[0] => left_sidebar
)
[_thumbnail_id] => Array
(
[0] => 2641
)
[ort] => Array
(
[0] => langenfeld
)
)
I want to save the "ort" in a variable.
[ort] => Array
(
[0] => langenfeld
)
My code give me the values of the array but how can I save the values?
My code:
foreach ($gpc as $k){
foreach ($k as $v){
//echo $v;
}
}
I thought something like that:
$ort = $v['ort'];
But that's not working for me. Can someone help?
This code is working properly
$arr = Array
(
'_edit_lock' => Array
(
'0' => "1434971582:11",
),
'_edit_last' => Array
(
'0' => "11",
),
'_wp_page_template' => Array
(
'0' => "page-templates/langenfeldDreiSpalterMitSiderbarsRL.php",
),
'_wpas_done_all' => Array
(
'0' => "1",
),
'hefo_before' => Array
(
'0' => "0",
),
'hefo_after' => Array
(
'0' => "0",
),
'sharing_disabled' => Array
(
'0' => "1",
),
'spacious_page_layout' => Array
(
'0' => "left_sidebar",
),
'_thumbnail_id' => Array
(
'0' => "2641",
),
'ort' => Array
(
'0' => "langenfeld",
),
);
$ort = $arr["ort"];
print_r($ort);
// output
Array
(
[0] => langenfeld
)
if you directly want langenfeld
$ort = $arr['ort'][0];
//this will output - langenfeld
This question already has answers here:
PHP: Check for duplicate values in a multidimensional array
(6 answers)
Closed 7 years ago.
I have an array, last two elements are identical, i just want to check duplicate exist or not.
Array
(
[0] => Array
(
[crop] => CI-000001
[type] => PT-000001
)
[1] => Array
(
[crop] => CI-000001
[type] => PT-000003
)
[2] => Array
(
[crop] => CI-000005
[type] => PT-000014
)
[3] => Array
(
[crop] => CI-000005
[type] => PT-000014
)
)
You need to use array_unique function of PHP as
$ara = Array ( Array ( 'crop' => 'CI-000001', 'type' => 'PT-000001' ), Array
(
'crop' => 'CI-000001',
'type' => 'PT-000003'
), Array
(
'crop' => 'CI-000005',
'type' => 'PT-000014'
), Array
(
'crop' => 'CI-000005',
'type' => 'PT-000014'
)
);
echo "<pre>";
print_r(array_unique($ara,SORT_REGULAR));
echo "</pre>";
Output:
Array
(
[0] => Array
(
[crop] => CI-000001
[type] => PT-000001
)
[1] => Array
(
[crop] => CI-000001
[type] => PT-000003
)
[2] => Array
(
[crop] => CI-000005
[type] => PT-000014
)
)
Try the following code:
$hashes=array();
foreach ($myarray as $key=>$item) {
$hash=sha1(var_export($item, true));
if (isset($hashes($hash)) echo "$key is a duplicate of ".$hashes[$hash];
else $hashes[$hash]=$key;
}
try like this
<?php
$array = array(array('crop' => 'CI-000001','type' => 'PT-000001'), array('crop' => 'CI-000001','type' => 'PT-000003'),array('crop' => 'CI-000005','type' => 'PT-000014'),array('crop' => 'CI-000005','type' => 'PT-000014'));
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
echo "After Remove Duplicate:".'<pre>';
print_r( $array );
echo '</pre>';
?>
Output:-
After Remove Duplicate:
Array
(
[0] => Array
(
[crop] => CI-000001
[type] => PT-000001
)
[1] => Array
(
[crop] => CI-000001
[type] => PT-000003
)
[2] => Array
(
[crop] => CI-000005
[type] => PT-000014
)
)
Demo
I have two arrays one created from a table in my database and one created from a HTML form (lets call them array_db and array_form).
I want to insert array_db to a table in my mySQL database, but before inserting I must merge array_db with array_form and remove duplicates. When there are a duplicates I want the values from array_form to be inserted to array_db
NOTE: array_form are not always the same meaning it could have more or less values than in the below ex.
What is the best way to do this?
Array_db
Array
(
[task_id] => task_id
)
Array
(
[user_id] => user_id
)
Array
(
[customer_id] => customer_id
)
Array
(
[created] => created
)
Array
(
[pickup] => pickup
)
Array
(
[tire_front] => tire_front
)
Array
(
[tire_back] => tire_back
)
Array
(
[tire_reg] => tire_reg
)
Array
(
[tire_indl] => tire_indl
)
Array
(
[tube_front] => tube_front
)
Array
(
[tube_back] => tube_back
)
Array
(
[hub_front] => hub_front
)
Array
(
[hub_back] => hub_back
)
Array
(
[hub_adjust] => hub_adjust
)
Array
(
[rim_front] => rim_front
)
Array
(
[rim_back] => rim_back
)
Array
(
[rim_adjust] => rim_adjust
)
Array
(
[spoke_missing_front] => spoke_missing_front
)
Array
(
[spoke_missing_back] => spoke_missing_back
)
Array
(
[spoke_comp_front] => spoke_comp_front
)
Array
(
[spoke_comp_back] => spoke_comp_back
)
Array
(
[break_adjust_front] => break_adjust_front
)
Array
(
[break_adjust_back] => break_adjust_back
)
Array
(
[break_cable_front] => break_cable_front
)
Array
(
[break_cable_back] => break_cable_back
)
Array
(
[break_pad_front] => break_pad_front
)
Array
(
[break_pad_back] => break_pad_back
)
Array
(
[gear_adj_front] => gear_adj_front
)
Array
(
[gear_adj_back] => gear_adj_back
)
Array
(
[gear_cable_front] => gear_cable_front
)
Array
(
[gear_cable_back] => gear_cable_back
)
Array
(
[gear_shift_front] => gear_shift_front
)
Array
(
[gear_shift_back] => gear_shift_back
)
Array
(
[bicy_chain] => bicy_chain
)
Array
(
[cog_wheel] => cog_wheel
)
Array
(
[cassette] => cassette
)
Array
(
[chainwheel] => chainwheel
)
Array
(
[crankset] => crankset
)
Array
(
[crank] => crank
)
Array
(
[fp_service] => fp_service
)
Array
(
[status] => status
)
Array
(
[service_1] => service_1
)
Array
(
[service_2] => service_2
)
Array
(
[service_3] => service_3
)
Array
(
[service_4] => service_4
)
Array
(
[price_approx] => price_approx
)
Array
(
[price_max] => price_max
)
Array_form
(
[tire_back] => tire_back
[tube_back] => tube_back
[gear_shift_front] => gear_shift_front
[user_id] => 0
[customer_id] => 6
)
As I said in my comment, this should be solveable by first flattening $array_db, eg like this
$flat_array_db = array();
foreach ($array_db as $subArray) {
foreach ($subArray as $key=>$value) {
$flat_array_db[$key] = $value;
}
}
And once you've done that, a simple array_merge($flat_array_db, $array_form) should do the trick.
I have the following two arrays...
1) how could i get only the different key->value one?
2) how can i insert to mysql the second array?
// first array
$aa = Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => LAL
[p_r] => RN
[id] =>
[gender] => m
)
)
[t_b] => Array
(
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] =>
[p_r] =>
)
)
[t_r] => Array
(
[0] => Array
(
[I_r] => 19
)
)
// second array
$bb = Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => NAN
[p_r] => RN
[id] => 1214125
[gender] => m
)
)
[t_b] => Array
(
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] => 21
[p_r] => 25
)
)
[t_r] => Array
(
[0] => Array
(
[I_r] => 19
)
)
I have used the array_diff function but i get NULL.
please some one help?
$aa=(array)$aa;
$bb=(array)$bb;
$result=array_diff($aa,$bb);
It's unclear what you want. Please give an example or your desired output. Here's one possibility:
$ser_aa = array_map(function($e){return serialize($e);}, $aa);
$ser_bb = array_map(function($e){return serialize($e);}, $bb);
$diff = array_diff($ser_aa, $ser_bb);
$out = array_map(function($e){return unserialize($e);}, $diff);
print_r($out);
Output:
Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => LAL
[p_r] => RN
[id] =>
[gender] => m
)
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] =>
[p_r] =>
)
)
)