我的产品有id,颜色和电缆尺寸。我想尊重带有产品ID的电缆尺寸阵列的顺序 像这样:
cable Array
(
[5619] => 10 - Inch
[5552] => 10 - Foot
[8211] => 10 - Inch
[5733] => 10 - Foot
)
所以这个有线电视阵列就像我们的排序图一样,我们在这个例子中的产品应该有10英寸的第一个和10英尺的下一个,它们带有产品ID。
然后我有颜色
product Array
(
[Green] => Array
(
[5552] => 10 - Foot
[5619] => 10 - Inch
)
[Pink] => Array
(
[5733] => 10 - Foot
[8211] => 10 - Inch
)
[Black] => Array
(
[4564] => 10 - Foot
)
)
在这个例子中,当我们有一个以上的颜色尺寸时,它应该根据电缆阵列订购粉色和绿色应该是:
[Pink] => Array
(
[8211] => 10 - Inch
[5733] => 10 - Foot
)
because 8211 came before 5733 in cable array.
[Green] => Array
(
[5619] => 10 - Inch
[5552] => 10 - Foot
)
as well because 5619 came before 5552 in cable array.
我这样做了:
function OrderSizeAndColorByMerging($cablesizes, $productarray) {
foreach ($cablesizes as $id => $size):
foreach ($productarray as $color => $sizes):
if ($sizes[$id]):
$productarray[$color] = $cablesizes;
endif;
endforeach;
endforeach;
return $productarray;
}
但这是错误的,不知道我是如何实现它的。
答案 0 :(得分:1)
foreach( $cable as $key => $value ){
foreach ($product as $keyarray){
if (array_key_exists( $key , $keyarray ))
{
echo array_key_exists( $key , $keyarray );
unset($keyarray[$key]);
array_unshift($arr ,$key=>value);// not sure if this is ever going to work, but i am giving u a general idea (never tested it)
break;
}
}
}
print_r($array2);
答案 1 :(得分:0)
正如QuakeCore在他的代码中提到的,我不得不在颜色内部取消设置数组然后再分配它们。所以现在我的代码正在运行,希望它可以在将来帮助一些同行:
<script type="text/javascript">
$('#template-code').change(function () {
var $that = $(this),
template_code = $that.val(),
code = '',
new_data = '',
text = '',
newCode = '';
// Extract settings from the theme and add them to #result
$('#submit-code').click(function () {
$('#tab-1').addClass('unactive');
$('#tab-2').removeClass('unactive');
$(template_code).find('setting').each(function (i) {
var $this = $(this),
setting_std = $this.text(),
setting_id = $this.attr('id');
code += '<input id="' + setting_id + '" name="' + setting_id + '" type="text" value="' + setting_std + '"><br>';
});
if (code !== '') {
$('#result').html(code);
}
});
// Update old data with the new one
$('#update-code').click(function () {
new_data = $("#settings-form").serializeArray();
$(template_code).find('setting').each(function (i) {
template_code = template_code.replace("<setting", "").replace("id='" + $(this).attr("id") + "'>", "").replace($(this).html(), "{" + i + "}").replace("</setting>", "");
});
$.each(new_data, function (i, new_field) {
template_code = template_code.replace("{" + i + "}", "<setting id='" + new_field.name + "'>" + new_field.value + "</setting>");
});
$('#template-code').val(template_code);
$('#tab-1').removeClass('unactive');
return false;
});
});
</script>