2D数组(带数字键)中的组关联行,以在每个组中产生独特值的索引子阵列
我有这个数组:
Array (
[0] => Array ( [2] => 3 )
[1] => Array ( [3] => 5 )
[2] => Array ( [2] => 4 )
[3] => Array ( [3] => 5 )
)
我想要的是合并这样的数组:
Array (
[2] => Array( 3, 4 )
[3] => Array( 5 )
)
这是生成原始数组的代码:
$optionValuesArrayContainer = array();
foreach ($item_options as $item_options_data) :
//$item_options_data["option_sons"] is a json object saved in mysql json
if (count($item_options_data["option_sons"]) > 0) {
foreach (json_decode($item_options_data["option_sons"]) as $item => $value) :
//$newd[$item] = $value;
array_push($optionValuesArrayContainer, array($item => $value));
endforeach;
}
endforeach;
print_r($optionValuesArrayContainer);
更多描述: 在MySQL中,我有多行。 这些行中的一列名为“ option_sons”及其一列JSON,例如:
{
"2": "3",
"3": "5"
}
另一列:
{
"2": "4",
"3": "5"
}
我想合并来自不同行的列以获取一个键的数组,但多个不同的值。
I have this array:
Array (
[0] => Array ( [2] => 3 )
[1] => Array ( [3] => 5 )
[2] => Array ( [2] => 4 )
[3] => Array ( [3] => 5 )
)
What I want is to merge the array like this:
Array (
[2] => Array( 3, 4 )
[3] => Array( 5 )
)
This is the code that generates the original array:
$optionValuesArrayContainer = array();
foreach ($item_options as $item_options_data) :
//$item_options_data["option_sons"] is a json object saved in mysql json
if (count($item_options_data["option_sons"]) > 0) {
foreach (json_decode($item_options_data["option_sons"]) as $item => $value) :
//$newd[$item] = $value;
array_push($optionValuesArrayContainer, array($item => $value));
endforeach;
}
endforeach;
print_r($optionValuesArrayContainer);
more description:
in the mysql, I have multiple rows.
one of the columns in these rows named "option_sons" and its a column of json like:
{
"2": "3",
"3": "5"
}
another column:
{
"2": "4",
"3": "5"
}
I want to merge the columns from different rows to get array of ONE KEY but multiple different VALUES.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解您尝试正确执行的操作,如果其值相同,则根据其键和值合并子阵列,因此您需要删除其中一个。
注意:我尝试使用JSON创建一个原始数组来显示真实的答案。我希望这是熟练的。
我们有子阵列,因此我们需要两个循环,但更重要的是,我们需要从键创建结果数组,例如IF语句,然后合并子阵列并获得独特的值以删除重复值。
If I understand what you try to do correctly it is merging sub-arrays according to their keys and values if their value are the same so you need to remove one of them.
Note: I tried to create an original array with json to show real answer. I hope it is crorect.
We have subarrays so we need two loops but more importantly we need to create result array from our key like the if statement then merge subarrays and get unique values to remove duplicate values.
使用钥匙查找将更有效地与嵌套环内部的合并和调用
array_unique()
。为确保将索引元素推入子阵列中,请使用查找数组,如果确定之前是否遇到过键值对。
通过更谨慎地将其推入结果阵列的内容,无需在循环期间或之后擦拭。
代码:( demo )
Using a key lookup will be more efficient that merging and calling
array_unique()
inside of a nested loop.To ensure that indexed elements are pushed into the subarrays, use a lookup array if determine if a key-value pair has been encountered before.
By being more careful about what is pushed into the result array, there will be no need to mop up during or after the loop.
Code: (Demo)