If I have this array:
//$myarray
Array (
[0] => Array (
[b163cb25371a1b7c550d8f69fe211cc8] => Array (
[unique_key] => 14f74cf38563b889386beeec511033e2
[thwepof_options] => Array (
[order_date] => Array (
[name] => order_date
[value] => 1
[label] => Szállítási nap
[options] =>
)
)
)
)
)
How can I write a sorting method which sort the values by order_date [value]
I've tried first to get the array column like this but didn't got any return value:
<?php
$days = array_column($myarray[0]['thwepof_options'], 'value', 'order_date');
?>
You are skipping the 2nd level key.
Here's a working version of your code:
<?php
$myarray = Array (
0 => Array (
'b163cb25371a1b7c550d8f69fe211cc8' => Array (
'unique_key' => '14f74cf38563b889386beeec511033e2',
'thwepof_options' => Array (
'order_date' => Array (
'name' => 'order_date',
'value' => 1,
'label' => 'Szállítási nap',
'options' => null
)
)
)
)
);
$days = array_column($myarray[0]['b163cb25371a1b7c550d8f69fe211cc8']['thwepof_options'], 'value', 'order_date');
print_r($days);
Also, this thing looks to me like it would make more sense as an object.
Related
I'm using print_r(array_unique($array, SORT_REGULAR)); on the array below but it does not work.
I'm trying to filter out the redundant data.
Notice that [Order] and its key value pairs are all the same. But [Transaction] and its key value pairs are unique.
I need to get the [Order] element data and combine it with the 3 different [Transaction] elements.
My array
Array
(
[0] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
)
[1] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
)
[2] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
The final array I need will look something like this.
Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[0] => Array
(
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
[1] => Array
(
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
[2] => Array
(
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
I can flatten the original array and then use array_unique, but wanted to see if there is a better way to accomplish what I need.
my code:
$myarray = array(
0 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11211", "TransactionPrice" => 91.17)
),
1 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11212", "TransactionPrice" => 180.41)
),
2 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11213", "TransactionPrice" => 209.99)
)
);
print_r(array_unique($myarray, SORT_REGULAR));
If you want to determine how many unique values of the Order element there are in your array, you need to apply array_unique only to the Order elements, which you can do using array_column:
$unique_orders = count(array_unique(array_column($myarray, 'Order'), SORT_REGULAR));
You can process your array using a list of keys which have non-unique values to generate an array, while other keys will have just a single value:
$non_unique_keys = ['Transaction'];
$output = array();
foreach (array_keys($myarray[0]) as $key) {
if (in_array($key, $non_unique_keys)) {
$output[$key] = array_column($myarray, $key);
}
else {
$output[$key] = $myarray[0][$key];
}
}
print_r($output);
Example Output:
Array (
[Order] => Array (
[PO] => TR11214
[OrderID] => 242856952012
)
[Sales Tax] => Array (
[PO] => TR11214
[SalesTaxAmount] => 0
)
[Transaction] => Array (
[0] => Array (
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
[1] => Array (
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
[2] => Array (
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
Demo on 3v4l.org
array_unique() is intended for single dimensional arrays. If you want to use it on a multi-dimentional array, you should consider using usort() instead. Then you'll need to iterate through the array in reverse manually, searching for duplicates and removing them.
I thought I have some understanding of arrays. But it looks like I have no understanding. Or my head don't want to work. I have arrays:
[0] => Array
(
[key] => Person 1
[values] => Array
(
[0] => Array
(
[0] => 1436821440000,12
)
)
)
[1] => Array
(
[key] => Person 2
[values] => Array
(
[0] => Array
(
[0] => 1437562620000,24
)
)
)
[2] => Array
(
[key] => Person 3
[values] => Array
(
[0] => Array
(
[0] => 1437080040000,10
)
)
)
[3] => Array
(
[key] => Person 1
[values] => Array
(
[0] => Array
(
[0] => 1437082860000,1
)
)
)
[4] => Array
(
[key] => Person 3
[values] => Array
(
[0] => Array
(
[0] => 1437081840000,9
)
)
)
And here is what I want to achieve:
[0] => Array
(
[key] => Person 1
[values] => Array
(
[0] => Array
(
[0] => 1436821440000,12
[1] => 1437082860000,1
)
)
)
[1] => Array
(
[key] => Person 2
[values] => Array
(
[0] => Array
(
[0] => 1437562620000,24
)
)
)
[2] => Array
(
[key] => Person 3
[values] => Array
(
[0] => Array
(
[0] => 1437080040000,10
[1] => 1437081840000,9
)
)
)
How could I remove duplicates and merge data?
The sample input doesn't offer any variation in the data contained in the deept subarrays, so I'll demonstrate the utility of my snippet by adding a little more complexity in the data.
Effectively, each time a new key is encountered, you declare a new row containing the key element and a values element with an empty array. To identify that key as "encountered", the new row is assigned to the result array using the key value as the first level key.
Now that the values subarray is guaranteed to be declared, an inner loop will comb through the deeper arrays and push one or more (all) deep values into the respective values subarray -- in a flat fashion.
Code: (Demo)
$array = [
['key' => 'Person 1', 'values' => [['1436821440000,12']]],
['key' => 'Person 2', 'values' => [['1437562620000,24']]],
['key' => 'Person 3', 'values' => [['1437080040000,10']]],
['key' => 'Person 1', 'values' => [['1437082860000,1'], ['1437082860000,2', '1437082860000,3']]],
['key' => 'Person 3', 'values' => [['1437081840000,9']]],
];
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['key']])) {
$result[$row['key']] = ['key' => $row['key'], 'values' => []];
}
foreach ($row['values'] as $values) {
array_push($result[$row['key']]['values'], ...$values);
}
}
var_export(
array_values($result)
);
Output:
array (
0 =>
array (
'key' => 'Person 1',
'values' =>
array (
0 => '1436821440000,12',
1 => '1437082860000,1',
2 => '1437082860000,2',
3 => '1437082860000,3',
),
),
1 =>
array (
'key' => 'Person 2',
'values' =>
array (
0 => '1437562620000,24',
),
),
2 =>
array (
'key' => 'Person 3',
'values' =>
array (
0 => '1437080040000,10',
1 => '1437081840000,9',
),
),
)
$result_arr = array();
foreach ($arr as $sub_arr)
{
$result_arr = array_merge($result_arr, $sub_arr);
$result_arr = array_unique($result_arr);
}
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
Please see my DEMO and answer my question: why date in values html on offset 2 not as date and it is a number?
DEMO: http://codepad.viper-7.com/r9FYnb
$data = array();
$data_1 = $_POST['data_1'];
$static = $_POST["static"];
foreach($static as $idx=>$val){
$data[] = array(
'data_1' => json_encode(Array($data_1[$idx*2],$data_1[$idx]*2+1)),
'static' => $static[$idx]
);
}
This is output:
Array
(
[0] => Array
(
[data_1] => ["2011\/8\/02",4023] **//4023 !?**
[static] => 12
)
[1] => Array
(
[data_1] => ["2011\/8\/09",4023] **// 4023!?**
[static] => 34
)
[2] => Array
(
[data_1] => ["2011\/8\/16",4023] **// 4023 !?**
[static] => 56
)
)
I'm not certain what you are trying to do here, but I see an inconsistency between how you manipulate $idx
'data_1' => json_encode(Array($data_1[$idx*2],$data_1[$idx]*2+1)),
// -----^^^^^^^^-------^^^^^^^^^^^^
For the second offset, perhaps you intend to modify $idx inside the []
'data_1' => json_encode(Array($data_1[$idx*2],$data_1[($idx*2)+1])),
// ---------------------^^^^^^^^^^^^
Sample output after modifying your demo:
Array
(
[0] => Array
(
[data_1] => ["2011\/8\/02","2011\/8\/08"]
[static] => 12
)
[1] => Array
(
[data_1] => ["2011\/8\/09","2011\/8\/15"]
[static] => 34
)
Found a question which already explains this - How to sort an array of associative arrays by value of a given key in PHP?
I have an array (A1) of arrays (A2). I would like to sort all A2s inside A1 based on a key:value pair in A2
The structure is like this
A1
->A2-1
->K1:Some Value
->K2:ValB
->A2-2
->K1:Other Value
->K2:ValB1
->A2-3
->K1:Some Value
->K2:ValB2
->A2-4
->K1:Other Value
->K2:ValB3
I would like to sort the arrays in A1 so that all A2s for which K1's value is Other Value are bunched together and all A2s for which K1's value is Some Value are bunched together
So after the sorting, the final order of arrays in A1 should be A2-2, A2-4, A2-1, A2-3. Is there a function in PHP using which I can do this? Or do I have to parse through the entire array and do the sorting?
Thanks.
http://php.net/manual/en/function.array-multisort.php
If that doesn't fit, there's a lot of other array functions http://www.php.net/manual/en/ref.array.php
Have a try with:
$A1 = array(
'A2-1' => array(
'K1' => 'Some Value',
'K2' => 'ValB',
),
'A2-2' => array(
'K1' => 'Other Value',
'K2' => 'ValB1',
),
'A2-3' => array(
'K1' => 'Some Value',
'K2' => 'ValB2',
),
'A2-4' => array(
'K1' => 'Other Value',
'K2' => 'ValB3',
)
);
function mySort($a, $b) {
if ($a['K1'] == $b['K1']) {
return strcmp($a['K2'], $b['K2']);
} else {
return strcmp($a['K1'], $b['K1']);
}
}
uasort($A1, 'mySort');
print_r($A1);
output:
Array
(
[A2-2] => Array
(
[K1] => Other Value
[K2] => ValB1
)
[A2-4] => Array
(
[K1] => Other Value
[K2] => ValB3
)
[A2-1] => Array
(
[K1] => Some Value
[K2] => ValB
)
[A2-3] => Array
(
[K1] => Some Value
[K2] => ValB2
)
)
You need something like:
usort($array, function($a, $b)
{
return strcmp($a['k1'], $b['k1']);
});
You may need to replace strcmp with a different sorting function (or operators) because it's unclear exactly what you are doing.
strategy:
1. find unique values, put them aside.
2. loop through unique values
2.1 loop origial array
2.2 store sub array in $out if unique val = original val
<?php
$i=0;
$a3d = array(
$i++ => array(0=>'Some Value',1=>'ValB'),
$i++ => array(0=>'Other Value',1=>'ValB1'),
$i++ => array(0=>'Some Value',1=>'ValB2'),
$i++ => array(0=>'Zome moar',1=>'ValB4'),
$i++ => array(0=>'Other Value',1=>'ValB3'),
$i++ => array(0=>'Zome Moar',1=>'ValB4'),
);
print_r($a3d);
foreach ($a3d as $a2d){
$uniq[]= $a2d[0];
}
$uniq = array_unique($uniq);
sort($uniq);
print_r($uniq);
foreach ($uniq as $v){
foreach ($a3d as $kk => $a2d){
if ($a2d[0] == $v){
$out[]= $a2d;
unset($a3d[$kk]); // <--avoid rechecking elements
}
}
}
print_r(count($a3d));
print_r($out);
?>
$ php sort_array.php
Array
(
[0] => Array
(
[0] => Some Value
[1] => ValB
)
[1] => Array
(
[0] => Other Value
[1] => ValB1
)
[2] => Array
(
[0] => Some Value
[1] => ValB2
)
[3] => Array
(
[0] => Zome moar
[1] => ValB4
)
[4] => Array
(
[0] => Other Value
[1] => ValB3
)
[5] => Array
(
[0] => Zome Moar
[1] => ValB4
)
)
Array
(
[0] => Other Value
[1] => Some Value
[2] => Zome Moar
[3] => Zome moar
)
0Array
(
[0] => Array
(
[0] => Other Value
[1] => ValB1
)
[1] => Array
(
[0] => Other Value
[1] => ValB3
)
[2] => Array
(
[0] => Some Value
[1] => ValB
)
[3] => Array
(
[0] => Some Value
[1] => ValB2
)
[4] => Array
(
[0] => Zome Moar
[1] => ValB4
)
[5] => Array
(
[0] => Zome moar
[1] => ValB4
)
)