I have this array here that contains a group of duplicate ids each id is set as as array key and the sub array contains a list of ids that also has another array within the same array?
DUPLICATES 16
array(16) {
[19503804]=>
array(3) {
[0]=>
string(8) "19501594"
[1]=>
string(8) "15539642"
[2]=>
string(8) "19498944"
}
[19501594]=>
array(3) {
[0]=>
string(8) "19503804"
[1]=>
string(8) "15539642"
[2]=>
string(8) "19498944"
}
[19837033]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19854553"
[3]=>
string(8) "19854565"
[4]=>
string(8) "19854554"
[5]=>
string(8) "19854683"
}
[19854553]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19837033"
[3]=>
string(8) "19854565"
[4]=>
string(8) "19854554"
[5]=>
string(8) "19854683"
}
[19544216]=>
array(3) {
[0]=>
string(8) "19524884"
[1]=>
string(8) "19560234"
[2]=>
string(8) "19540264"
}
[19854565]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19837033"
[3]=>
string(8) "19854553"
[4]=>
string(8) "19854554"
[5]=>
string(8) "19854683"
}
[19854554]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19837033"
[3]=>
string(8) "19854553"
[4]=>
string(8) "19854565"
[5]=>
string(8) "19854683"
}
[15539642]=>
array(3) {
[0]=>
string(8) "19503804"
[1]=>
string(8) "19501594"
[2]=>
string(8) "19498944"
}
[19844271]=>
array(1) {
[0]=>
string(8) "19341140"
}
[19498944]=>
array(3) {
[0]=>
string(8) "19503804"
[1]=>
string(8) "19501594"
[2]=>
string(8) "15539642"
}
[16399898]=>
array(1) {
[0]=>
string(8) "15436391"
}
[15436391]=>
array(1) {
[0]=>
string(8) "16399898"
}
[19341140]=>
array(1) {
[0]=>
string(8) "19844271"
}
[19560234]=>
array(3) {
[0]=>
string(8) "19544216"
[1]=>
string(8) "19524884"
[2]=>
string(8) "19540264"
}
[19854683]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19837033"
[3]=>
string(8) "19854553"
[4]=>
string(8) "19854565"
[5]=>
string(8) "19854554"
}
[19540264]=>
array(3) {
[0]=>
string(8) "19544216"
[1]=>
string(8) "19524884"
[2]=>
string(8) "19560234"
}
}
notice first array?
[19503804]=>
array(3) {
[0]=>
string(8) "19501594"
[1]=>
string(8) "15539642"
[2]=>
string(8) "19498944"
}
the "19501594" "15539642" "19498944" also has another array which need to be unset.so at the end in this array only the unique arrays with unique values are given.
and this is the expected output required.
array(5) {
[19503804]=>
array(3) {
[0]=>
string(8) "19501594"
[1]=>
string(8) "15539642"
[2]=>
string(8) "19498944"
}
[19837033]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19854553"
[3]=>
string(8) "19854565"
[4]=>
string(8) "19854554"
[5]=>
string(8) "19854683"
}
[19544216]=>
array(3) {
[0]=>
string(8) "19524884"
[1]=>
string(8) "19560234"
[2]=>
string(8) "19540264"
}
[19844271]=>
array(1) {
[0]=>
string(8) "19341140"
}
[16399898]=>
array(1) {
[0]=>
string(8) "15436391"
}
}
This is what i had initially coded as proposed by #Dale in the answer below.
$theduplicates = array (
19503804 =>
array (
0 => '19501594',
1 => '15539642',
2 => '19498944',
),
19501594 =>
array (
0 => '19503804',
1 => '15539642',
2 => '19498944',
),
19837033 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19854553',
3 => '19854565',
4 => '19854554',
5 => '19854683',
),
19854553 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19837033',
3 => '19854565',
4 => '19854554',
5 => '19854683',
),
19544216 =>
array (
0 => '19524884',
1 => '19560234',
2 => '19540264',
),
19854565 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19837033',
3 => '19854553',
4 => '19854554',
5 => '19854683',
),
19854554 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19837033',
3 => '19854553',
4 => '19854565',
5 => '19854683',
),
15539642 =>
array (
0 => '19503804',
1 => '19501594',
2 => '19498944',
),
19844271 =>
array (
0 => '19341140',
),
19498944 =>
array (
0 => '19503804',
1 => '19501594',
2 => '15539642',
),
16399898 =>
array (
0 => '15436391',
),
15436391 =>
array (
0 => '16399898',
),
19341140 =>
array (
0 => '19844271',
),
19560234 =>
array (
0 => '19544216',
1 => '19524884',
2 => '19540264',
),
19854683 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19837033',
3 => '19854553',
4 => '19854565',
5 => '19854554',
),
19540264 =>
array (
0 => '19544216',
1 => '19524884',
2 => '19560234',
),
)
foreach($theduplicates as $k => $v){
foreach($v as $d){
if(isset($theduplicates[$d])){
unset($theduplicates[$d]);
}
}
// but it totally cleans theduplicates array
array(0) {
}
For every iteration you want to check they keys that have already been iterated. Therefore you want to keep track of these keys. I would do something like this:
$keys = [];
foreach ($main_array as $k => $set) {
if (in_array($k, $keys)) {
unset($main_array[$k]);
continue;
}
foreach ($set as $val) {
$keys[] = $val;
}
}
var_dump($main_array);
This code will also work, but you have to pass by reference:
foreach ($main_array as &$values) {
foreach ($values as $value) {
if(isset($main_array[$value])) {
unset($main_array[$value]);
}
}
}
If I understand your question (of which I am doubtful) this code should get you on your way
$array = [
0 => [5, 6, 7],
1 => [10],
5 => [10], // to remove
6 => [10], // to remove
7 => [10] // to remove
];
print_r($array);
foreach ($array as $key => $values) {
foreach ($values as $value) {
if(isset($array[$value])) {
unset($array[$value]);
}
}
}
print_r($array);
output..
Array
(
[0] => Array
(
[0] => 5
[1] => 6
[2] => 7
)
[1] => Array
(
[0] => 10
)
[5] => Array
(
[0] => 10
)
[6] => Array
(
[0] => 10
)
[7] => Array
(
[0] => 10
)
)
Array
(
[0] => Array
(
[0] => 5
[1] => 6
[2] => 7
)
[1] => Array
(
[0] => 10
)
)
For some reasons, when I try to do an if for $value['is_set'] == false, I get undefined index error, however, if I do an isset($value['is_set']) I get 1
I did a print_r and got the ff:
Array ( [is_set] => [product] => Array ( [product_ID] => 1 [product_name] => Insulated Terminal Lugs 1.25 - 3Y [product_code] => [unit_ID] => 80 [unit_name] => pc(s) [price] => 50 [qty] => 1 [discount] => 0 [subtotal] => 50 [amount] => 50 ) [set] => Array ( [set_ID] => [set_items] => Array ( [0] => Array ( [amount] => 0 ) ) [amount] => 0 ) [selectedProduct] => Array ( [ID] => 1 [product_name] => Insulated Terminal Lugs 1.25 - 3Y [product_code] => [price] => 50 ) [selectedUnit] => Array ( [ID] => 80 [option_key] => pc(s) ) [amount] => 50 )
why is this happening?
here is my code:
public function create($model, $value, $datetime, $transaction_type, $notes) {
if (($model->general_status == Sales::GEN_STATUS_OPEN || $model->general_status == Sales::GEN_STATUS_CLOSED) && ($model->delivery_status == Sales::DELIVERY_STATUS_DELIVERED)) {
$pth = new ProductTransactionHistory();
//var_dump($value); exit();
if ($value['is_set'] == false) {
$pth->generateSales($transaction_type, $datetime, '(add)', $model, $value, Yii::$app->user->id, $notes);
$pth->stock_in_out = ProductTransactionHistory::STOCK_OUT;
//$pth->save();
$this->saveDebug($pth);
}
}
this function is called by the following code:
$so = Json::decode($request['purchase-orders']);
foreach ($so['orders'] as $key => $value) {
$order->create($model, $value, $datetime);
}
the full var_dump of $so is
array(8) { ["orders"]=> array(2) { [0]=> array(6) { ["is_set"]=> bool(false) ["product"]=> array(10) { ["product_ID"]=> string(1) "1" ["product_name"]=> string(33) "Insulated Terminal Lugs 1.25 - 3Y" ["product_code"]=> string(0) "" ["unit_ID"]=> string(2) "80" ["unit_name"]=> string(5) "pc(s)" ["price"]=> string(2) "50" ["qty"]=> int(1) ["discount"]=> int(0) ["subtotal"]=> int(50) ["amount"]=> int(50) } ["set"]=> array(3) { ["set_ID"]=> bool(false) ["set_items"]=> array(1) { [0]=> array(1) { ["amount"]=> int(0) } } ["amount"]=> int(0) } ["selectedProduct"]=> array(4) { ["ID"]=> string(1) "1" ["product_name"]=> string(33) "Insulated Terminal Lugs 1.25 - 3Y" ["product_code"]=> string(0) "" ["price"]=> string(2) "50" } ["selectedUnit"]=> array(2) { ["ID"]=> string(2) "80" ["option_key"]=> string(5) "pc(s)" } ["amount"]=> int(50) } [1]=> array(5) { ["is_set"]=> bool(true) ["product"]=> array(3) { ["unit_ID"]=> string(0) "" ["unit_name"]=> string(0) "" ["amount"]=> int(0) } ["set"]=> array(3) { ["set_ID"]=> string(1) "1" ["set_items"]=> array(2) { [0]=> array(10) { ["product_ID"]=> int(4) ["product_name"]=> string(33) "Power Miniature Solder Relay 220v" ["product_code"]=> string(5) "LY4NJ" ["unit_ID"]=> int(80) ["unit_name"]=> string(5) "pc(s)" ["qty"]=> int(1) ["price"]=> int(200) ["discount"]=> int(0) ["subtotal"]=> int(200) ["amount"]=> int(200) } [1]=> array(10) { ["product_ID"]=> int(5) ["product_name"]=> string(26) "Relay Socket 14A for LY4NJ" ["product_code"]=> string(6) "PYF14A" ["unit_ID"]=> int(80) ["unit_name"]=> string(5) "pc(s)" ["qty"]=> int(1) ["price"]=> int(20) ["discount"]=> int(0) ["subtotal"]=> int(20) ["amount"]=> int(20) } } ["amount"]=> int(220) } ["selectedSet"]=> array(3) { ["ID"]=> string(1) "1" ["set_name"]=> string(20) "LY4NJ 220v w/ Socket" ["price"]=> string(3) "220" } ["amount"]=> int(220) } } ["has_downpayment"]=> bool(false) ["payment_type"]=> string(4) "cash" ["grandTotal"]=> int(270) ["generalStatus"]=> string(6) "closed" ["statusText"]=> string(6) "Cancel" ["cssStatus"]=> string(14) "btn btn-danger" ["showPayment"]=> bool(false) }
the full error is:
PHP Notice – yii\base\ErrorException
Undefined index: is_set
also this is related to this issue:
Why does accessing array index on boolean value does not raise any kind of error?
but I don't know how to get pass this without using array...
Initially I tried empty($value['is_set']) as an alternative, but it didn't work and I freaked out. I reconsidered that option again and found that it works! I don't know why it didn't work earlier. Thanks for all your support!
I'm trying to replace variables like {{{month}}} in a template to the current month and {{{month+1}}} to current month + 1.
That's not the hardest part of my code, except that the regex I wrote doesn't yield expected results.
$string = '{{{year}}}{{{month+1}}}';
preg_match_all('/{{{(?:([yY])ear|([mM])onth|([dD])ay)(?:(?<operation>[-|+])(?<amount>[1-9]+))?}}}/m', $string, $matches);
var_dump($matches);
Why do I have so much empty array entries?
I was expecting
[0] => array('{{{year}}}', '{{{month+1}}}')
[1] => array('y', 'm')
[2] => array('', '+')
[3] => array('', '1')
What am I doing wrong?
The respond of the above code is:
array(8) {
[0]=>
array(2) {
[0]=>
string(10) "{{{year}}}"
[1]=>
string(13) "{{{month+1}}}"
}
[1]=>
array(2) {
[0]=>
string(1) "y"
[1]=>
string(0) ""
}
[2]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "m"
}
[3]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(0) ""
}
["operation"]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "+"
}
[4]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "+"
}
["amount"]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "1"
}
[5]=>
array(2) {
[0]=>
string(0) ""
[1]=>
string(1) "1"
}
}
You may use a "generic" character class to match the first letters of month, year and day, and then use an alternation with positive look-behinds to make sure we match what we need.
preg_match_all('/{{{([yYmMdD])(?:(?<=[Yy])ear|(?<=[Mm])onth|(?<=[Dd])ay)(?:([-+])([1-9]+))?}}}/m', $string, $matches);
See IDEONE demo
And this is the print_r view:
Array
(
[0] => Array
(
[0] => {{{year}}}
[1] => {{{month+1}}}
)
[1] => Array
(
[0] => y
[1] => m
)
[2] => Array
(
[0] =>
[1] => +
)
[3] => Array
(
[0] =>
[1] => 1
)
)
I need to sort the array by the value of the given key 0 in this case.
So here is my array:
array(11) {
[0]=> array(3) {
[0]=> string(3) "5"
[1]=> string(1) "3"
[2]=> string(2) "21"
}
[1]=> array(3) {
[0]=> string(3) "0.5"
[1]=> string(1) "3"
[2]=> string(3) "may"
}
[2]=> array(3) {
[0]=> string(3) "2.2"
[1]=> string(1) "3"
[2]=> string(16) "sport"
}
}
Result must be sorted by the value of key 0:
array(11) {
[0]=> array(3) {
[0]=> string(3) "0.5"
[1]=> string(1) "3"
[2]=> string(2) "may"
}
[1]=> array(3) {
[0]=> string(3) "2.2"
[1]=> string(1) "3"
[2]=> string(3) "sport"
}
[2]=> array(3) {
[0]=> string(3) "5"
[1]=> string(1) "3"
[2]=> string(16) "21"
}
}
I attempted it with this code:
function sort_by_second($i,$j){return $i[0]-$j[0];};
usort($mas,'sort_by_second');
I don't understand why it doesn't work.
Try below code:
$array = array(
array("5","3","21"),
array("0.5","3","may"),
array("2.2","3","sport"),
);
//print_r($array);
function subval_sort($a,$subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
asort($b);
foreach($b as $key=>$val) {
$c[] = $a[$key];
}
return $c;
}
$result = subval_sort($array, 0);
print_r($result);
Result:
Array
(
[0] => Array
(
[0] => 0.5
[1] => 3
[2] => may
)
[1] => Array
(
[0] => 2.2
[1] => 3
[2] => sport
)
[2] => Array
(
[0] => 5
[1] => 3
[2] => 21
)
)
Demo:
http://3v4l.org/q4ZtL#v430
PHP >= 5.5.0:
array_multisort(array_column($array, 0), SORT_ASC, $array);
in fear of duplicating content, i have looked through so many similar SO questions, but i think i need a bit more than code, to tell me how solve my problem- would be lovely with some explaination too.
How do i turn $list:
array(4) {
[0]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
[1]=>
array(2) {
["title"]=>
string(6) "Zarafa"
["id"]=>
int(34)
}
[2]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
[3]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
}
Into $list:
array(2) {
[0]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
[1]=>
array(2) {
["title"]=>
string(6) "Zarafa"
["id"]=>
int(34)
}
}
By removing duplicate entries?
Use array_unique() with SORT_REGULAR flag.
$new_array = array_unique($array, SORT_REGULAR);
Output should be:
Array
(
[0] => Array
(
[title] => Zambezia
[id] => 31
)
[1] => Array
(
[title] => Zarafa
[id] => 34
)
)
Demo.