問題タブ [array-difference]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票する
4 に答える
5713 参照

php - PHP - 配列の値を動的に比較する方法は?

わかりました、次の「挑戦」があります。

私はこのような配列を持っています:

私がする必要があるのは、それらがすべて同じ[ステータス]を持っているかどうかを確認することです. 問題は、2 つ以上の (動的) 配列を内部に持つことができることです。

それらをループ/検索するにはどうすればよいですか?

array_diff は比較する複数の配列をサポートしていますが、どのように行うのですか? :(私が試したループ、またはApache /ブラウザが停止した-または完全に偽のデータが返されました。

0 投票する
1 に答える
171 参照

php - array_diffはPHPでの出力を出力しません

array_diffに問題があります。

法的な電子メールアドレスのあるテキストエリアがあります。これらはmysqlデータベースから取得します。それらをテキストエリアに挿入すると、配列にも保存されます。

フォームが送信されると、テキストエリアのコンテンツを取得して最初の配列と比較する関数があります。このようにして、ユーザーが別のメールアドレスを追加したかどうかを確認できます。

私の問題は、array_diffが異なる要素ではなく、配列全体を出力することです。

比較する前に配列を出力しようとしましたが、どちらも正しい値を持っています。ハードコードされた値を持つダミー配列を試したところ、出力は正しいです。

これを引き起こす原因は何ですか?

0 投票する
1 に答える
1261 参照

php - Combine two PHP mysql_fetch_assoc sets of MySQL multidimensional arrays into one single-dimensional array

Would greatly appreciate any help with this.

I'm running two mysql_query's to retrieve an array of total bulletins for a specified user in a database from one table (bulletins) and signed bulletins from a second table (bulletins_sign_log).

The problem I'm running into is how to determine out of the two arrays, which bulletins still require signature from a user.

Here's the example arrays and my attempts so far:

total bulletins mysql_query PHP code

total bulletins print_r result

signed bulletins mysql_query PHP code

signed bulletins print_r result

This is where I can't figure out how to determine that bulletin # 3 from total bulletins hasn't been signed by the user. I've tried using array_diff() but didn't get the expected result.

comparison array_diff PHP code

comparison print_r result

My ultimate goal is to take the value of the unsigned bulletin_id (bulletin #3 in this case) and place it into a header redirect if greater than 0 such as:

final result

header('location: /sign_bulletin.php?bulletin_id=1');

final (expected) result

header('location: /sign_bulletin.php?bulletin_id=3');


EDIT Adding some additional information after trying @gilden's suggested SQL query.

SQL query that I used in phpMyAdmin

MySQL result from phpMyAdmin

The problem with the result is that it lists bulletin_id #1 as not being signed for username1. However, in the example bulletins_sign_log table below, username1 has a "signed" record for this bulletin_id. It's being flagged because of the matching SQL statement:

to_group_id is not recorded in the bulletins_sign_log table as a "group" cannot sign for something but it's members can. It is only logged in the bulletins table to know which users to send the bulletin too. In this case, to_group_id '0' is the "Everyone" group where all users receive the bulletin.

So my next thought was to just remove OR b.to_group_id = '0' but then I get an empty set. Any ideas on where to go from here with the current database configuration? Changing the DB would mean changing A LOT of code unfortunately then hours or days of debugging possibly.

Would it be possible to go back to the original idea of separate arrays and somehow get them both into an array where duplicate values can be removed? The original method seems to be a working method for getting accurate source information which is a start. This would then leave an array of unique bulletin_id's that have not been signed.

Table structure for bulletins

Table structure for bulletins_sign_log

Thank you in advance for any help you can provide!


FINAL code with @gilden's help - Thanks!

It didn't make sense for the original sender to have to sign his own bulletin so I blocked that as a requirement from the SQL query by adding:

WARNING Be sure to add that to the bottom of the query or it won't work - trust me.

...and here's the final SQL query:

...and the PHP code

0 投票する
3 に答える
3671 参照

arrays - 配列と既知のサブシーケンスの違いに対する効率的なアルゴリズム?

入力配列のサブシーケンスである配列を返すライブラリ関数に配列を渡しています。つまり、1 番目と 2 番目の配列の順序は同じですが、2 番目の配列には 1 番目の配列の要素がいくつ欠けていてもかまいません。どちらの配列にも重複はありません!

次に、入力にはあったが関数の出力には含まれていないすべての要素の新しい配列を作成したいと思います。

些細なことのように聞こえますが、何らかの理由で、特に配列の最後で間違っているようです。

例 1 (典型的):

入力配列 a:

入力配列 b:

出力配列「差分」:

例 2 (最小限、違いが文字列の最後にある場合にいくつかのバグが明らかになります):

入力配列 a:

入力配列 b:

出力配列「差分」:

(JavaScript / jQuery で実装するつもりですが、実際にはオブジェクトの配列を扱うので、疑似コードの汎用アルゴリズムにもっと興味があります。 C/C++ のようなポインターより)

0 投票する
3 に答える
1026 参照

php - array_udiff_assoc()とarray_diff_uassoc()の違いは何ですか?

array_udiff_assoc()との違いは何array_diff_uassoc()ですか?

の場合array_udiff_assoc()、私はこのコードを持っています:

結果

またarray_diff_uassoc()

結果は最初のものと同じです:

違いがあるとしたら、それは何ですか?PHPマニュアルには、それらが相互のエイリアスであるとは記載されていません。

0 投票する
2 に答える
1760 参照

php - PHP の array_diff_assoc() の逆、重複する値を削除

アクセス許可レベルのシステムがあるため、以前に選択したアクセス許可をアレイから削除する必要があります。例えば:

問題: array_diff_assoc() 正確なキーを比較する、[0] = [0]、キーではなく値を比較する必要があり、値を削除せずに、以前に選択したアクセス許可なしですべてのアクセス許可を返す必要があります。

私の配列と戻り値

0 投票する
3 に答える
341 参照

php - array_diff_uassoc の動作が明確でない

まず第一に、私はマニュアルとphpのドキュメントを掘り下げて答えを見つけられなかったことを言及する必要があります. 私が使用するコードは次のとおりです。

私の考えでは、array_diff_uassoc はこれら 2 つの配列のすべての値を比較し、値が存在する場合はキー比較を実行します。このコードの出力は次のとおりです。

ではまず、一部のペア (1 : 0 または 3 : 1) が重複しているのはなぜですか? このアイテムをすでに比較したことを機能が忘れていたということですか?すべての等しい値のペアを比較すると思っていましたが、出力には表示されません。何か不足していますか?

質問は次のとおりです:比較の順序に関して、この関数の正確な動作は何ですか?なぜこの重複が見られるのですか? (私のPHPバージョン、それが役立つ場合:PHPバージョン5.3.6-13ubuntu3.6)

私は本当に混乱していて、それについての良い説明を待っています...

0 投票する
2 に答える
195 参照

php - 複雑な xml の array_diff

4 つのテキスト メッセージを含むこの xml があり、それを配列に変換しました。

XML

配列

配列 ( [smses] => 配列 ( [sms] => 配列 ( [0] => 配列 ( ) [1] => 配列 ( ) [0_attr] => 配列 ( [プロトコル] => 0 [アドレス] => 0800000 [date] => 1328814938421 [type] => 2 [subject] => null [body] => 雨が降る猫と犬、そしてその飼い主 [toa] => null [sc_toa] => null [service_center] = > null [read] => 1 [status] => -1 [locked] => 0 [date_sent] => null [readable_date] => 2012 年 2 月 9 日 10:15:38 PM [contact_name] => Hans Petit ) [1_attr] => Array ( [protocol] => 0 [address] => 08005678 [date] => 1328814938421 [type] => 2 [subject] => null [body] => Hello, Andy.The attachment was' t sent.Please resend. [toa] => null [sc_toa] => null [service_center] => null [read] => 1 [status] => -1 [locked] => 0 [date_sent] =>null [Readable_date] => 2012 年 2 月 9 日 10:15:38 PM [contact_name] => Mary The Great ) [2] => 配列 ( ) [2_attr] => 配列 ([プロトコル] => 0 [アドレス] = > 080091011 [date] => 1328814938421 [type] => 2 [subject] => null [body] => Lorem Ipsum = Good Night. [toa] => null [sc_toa] => null [service_center] => null [ read] => 1 [status] => -1 [locked] => 0 [date_sent] => null [readable_date] => 2012 年 2 月 9 日 10:15:38 PM [contact_name] => Ed Myers ) [3] => Array ( ) [3_attr] => Array ( [protocol] => 0 [address] => +44839202 [date] => 1328815215841 [type] => 1 [subject] => null [body] => 表す変数 [toa] => null [sc_toa] => null [service_center] => +4422500000 [read] => 1 [status] => -1 [locked] => 0 [date_sent] =>null [読み取り可能な日付] => 2012 年 2 月 9 日午後 10:20:15 [連絡先名] => デクスター) [4] => 配列 () [4_attr] => 配列 ([プロトコル] => 0 [アドレス] => + 2273839309 [date] => 1329194575094 [type] => 1 [subject] => null [body] => 気軽にどうぞ [toa] => null [sc_toa] => null [service_center] => +4422500000 [read] = > 1 [ステータス] => -1 [ロック済み] => 0 [送信日] => null [読み取り可能日] => 2012 年 2 月 14 日 7:42:55 AM [連絡先名] => ミゲル ) ) ) [smses_attr] =>配列 ( [カウント] => 4 ) )+4422500000 [読み取り] => 1 [ステータス] => -1 [ロック済み] => 0 [送信日] => null [読み取り可能日付] => 2012 年 2 月 14 日 7:42:55 AM [連絡先名] => ミゲル ) ) ) [smses_attr] => 配列 ( [count] => 4 ) )+4422500000 [読み取り] => 1 [ステータス] => -1 [ロック済み] => 0 [送信日] => null [読み取り可能日付] => 2012 年 2 月 14 日 7:42:55 AM [連絡先名] => ミゲル ) ) ) [smses_attr] => 配列 ( [count] => 4 ) )

私は1つの値のみを変更した別のxml doc 2を持っています.1つ目はaddress="0800000"で、2つ目はaddress="0900000"です。

私が期待していたものではありません。

配列 ( )

私は何かを期待していた

0 投票する
3 に答える
1408 参照

php - 2つの2D配列間で連想行を比較しているときに、array_diff_assoc()は間違った違いを示します

私は2つの配列を持っており、違いを取得するためにarray_diff_assoc() php関数を使用していますが、常に違いとして通信セットを返しますが、新しいqセットである必要があります。

配列--

array_diff_assoc()opを使用した後

0 投票する
2 に答える
580 参照

php - array_diff()は空の配列キーを残します

ここで何かが足りないかもしれarray_diff()ませんが、配列の最後に空のkesysが残っているようです。

このサンプルデータでは-

私はこれを残されています-

私の理解では、これは正しくありません([0]は設定を解除し、[1]だけを残す必要があります)。私は$term_ids = array_values($term_ids);キーを正しくするためにアフターを使用していますが、これは必要ではないようですので、私よりも多くのことを知っている誰かが明確にすることができるかどうか疑問に思います。