如何在php中组合数组
我希望此数组位于中(请参阅,我添加了一个键名属性)如何在php中组合数组,php,arrays,Php,Arrays,我希望此数组位于中(请参阅,我添加了一个键名属性) 这样的事情应该行得通,如果不行,请告诉我。让数组名为$arr。首先存储$arr中的属性值,然后从主数组中取消设置。现在循环数组并在数组中添加新的属性,使用&作为参考,这样主数组也会更改 在线检查:,感谢@ranjeethingh提供阵列 Array ( [items] => Array ( [0] => stdClass Object (
这样的事情应该行得通,如果不行,请告诉我。让数组名为$arr。首先存储
$arr
中的属性值
,然后从主数组中取消设置。现在循环数组并在数组中添加新的属性
,使用&
作为参考,这样主数组也会更改
在线检查:,感谢@ranjeethingh提供阵列
Array
(
[items] => Array
(
[0] => stdClass Object
(
[id] => 1
[order_id] => 1
[product_id] => 1
[user_id] => 2
[photographer_id] => 3
[attributes_id] => 2,3
[product_type] => 0
[status] => pending
[is_deleted] => 0
[created_at] => 2016-05-15 04:08:21
[updated_at] => 2016-05-16 12:11:23
[username] => user
[photographer] => photographer
[product_name] => sdf
[attributes] => Attributes 2, Attributes 3
)
[1] => stdClass Object
(
[id] => 2
[order_id] => 1
[product_id] => 2
[user_id] => 2
[photographer_id] => 3
[attributes_id] => 1,3
[product_type] => 1
[status] => pending
[is_deleted] => 0
[created_at] => 2016-05-15 04:08:21
[updated_at] => 2016-05-16 12:11:23
[username] => user
[photographer] => photographer
[product_name] => qqqqqqqqqq
[attributes] => Attributes 1, Attributes 3
)
)
)
$arr=array(
'items'=>数组(
排列(
'id'=>'1',
“订单号”=>“1”,
“产品标识”=>“1”,
'user_id'=>'2',
“摄影师id”=>“3”,
'attributes_id'=>'2,3',
“产品类型”=>“0”,
'状态'=>'挂起',
'是否已删除'=>'0',
“创建于”=>“2016-05-15 04:08:21”,
“更新时间:2016-05-16 12:11:23”,
“用户名”=>“用户”,
“摄影师”=>“摄影师”,
“产品名称”=>“sdf”,
),
排列(
'id'=>'1',
“订单号”=>“1”,
“产品标识”=>“1”,
'user_id'=>'2',
“摄影师id”=>“3”,
'attributes_id'=>'2,3',
“产品类型”=>“0”,
'状态'=>'挂起',
'是否已删除'=>'0',
“创建于”=>“2016-05-15 04:08:21”,
“更新时间:2016-05-16 12:11:23”,
“用户名”=>“用户”,
“摄影师”=>“摄影师”,
“产品名称”=>“sdf”,
),
'attributesValue'=>数组(
‘属性2,属性3’,
'属性1,属性3'
)
)
);
$attribute=$arr['items']['attributesValue'];
未设置($arr['items']['attributesValue']);
foreach($arr['items']作为$key=>&$value){
$values['attributes']=$attribute[$key];
}
回声';
印刷费($arr);
类似的方法应该有效,如果不行,请告诉我。让数组名为$arr。首先存储$arr
中的属性值
,然后从主数组中取消设置。现在循环数组并在数组中添加新的属性
,使用&
作为参考,这样主数组也会更改
在线检查:,感谢@ranjeethingh提供阵列
Array
(
[items] => Array
(
[0] => stdClass Object
(
[id] => 1
[order_id] => 1
[product_id] => 1
[user_id] => 2
[photographer_id] => 3
[attributes_id] => 2,3
[product_type] => 0
[status] => pending
[is_deleted] => 0
[created_at] => 2016-05-15 04:08:21
[updated_at] => 2016-05-16 12:11:23
[username] => user
[photographer] => photographer
[product_name] => sdf
[attributes] => Attributes 2, Attributes 3
)
[1] => stdClass Object
(
[id] => 2
[order_id] => 1
[product_id] => 2
[user_id] => 2
[photographer_id] => 3
[attributes_id] => 1,3
[product_type] => 1
[status] => pending
[is_deleted] => 0
[created_at] => 2016-05-15 04:08:21
[updated_at] => 2016-05-16 12:11:23
[username] => user
[photographer] => photographer
[product_name] => qqqqqqqqqq
[attributes] => Attributes 1, Attributes 3
)
)
)
$arr=array(
'items'=>数组(
排列(
'id'=>'1',
“订单号”=>“1”,
“产品标识”=>“1”,
'user_id'=>'2',
“摄影师id”=>“3”,
'attributes_id'=>'2,3',
“产品类型”=>“0”,
'状态'=>'挂起',
'是否已删除'=>'0',
“创建于”=>“2016-05-15 04:08:21”,
“更新时间:2016-05-16 12:11:23”,
“用户名”=>“用户”,
“摄影师”=>“摄影师”,
“产品名称”=>“sdf”,
),
排列(
'id'=>'1',
“订单号”=>“1”,
“产品标识”=>“1”,
'user_id'=>'2',
“摄影师id”=>“3”,
'attributes_id'=>'2,3',
“产品类型”=>“0”,
'状态'=>'挂起',
'是否已删除'=>'0',
“创建于”=>“2016-05-15 04:08:21”,
“更新时间:2016-05-16 12:11:23”,
“用户名”=>“用户”,
“摄影师”=>“摄影师”,
“产品名称”=>“sdf”,
),
'attributesValue'=>数组(
‘属性2,属性3’,
'属性1,属性3'
)
)
);
$attribute=$arr['items']['attributesValue'];
未设置($arr['items']['attributesValue']);
foreach($arr['items']作为$key=>&$value){
$values['attributes']=$attribute[$key];
}
回声';
印刷费($arr);
以下是一个工作示例,说明了(我认为)您希望实现的行为:
其思想是将属性值从数组中分离出来,并将此数组中的索引与数组的项子数组中的索引相匹配
<?php
$arr = [
"items" => [
(object)[ "id"=> 1 ],
(object)[ "id"=> 2 ],
"attributeValues" => [
"Attribute for id 1",
"Attribute for id 2"
]
]
];
$attributes = $arr["items"]["attributeValues"];
unset($arr["items"]["attributeValues"]);
foreach ($attributes as $key => $value) {
if (isset($arr["items"][$key])) {
$arr["items"][$key]->attributes = $value;
}
}
print_r($arr);
以下是一个你想要达到的行为(我认为)的工作示例:
其思想是将属性值从数组中分离出来,并将此数组中的索引与数组的项子数组中的索引相匹配
<?php
$arr = [
"items" => [
(object)[ "id"=> 1 ],
(object)[ "id"=> 2 ],
"attributeValues" => [
"Attribute for id 1",
"Attribute for id 2"
]
]
];
$attributes = $arr["items"]["attributeValues"];
unset($arr["items"]["attributeValues"]);
foreach ($attributes as $key => $value) {
if (isset($arr["items"][$key])) {
$arr["items"][$key]->attributes = $value;
}
}
print_r($arr);
我想你的阵列应该是这样的
<?php
$your_arr = array(
'items' => array(
array(
'id' => '1',
'order_id' => '1',
'product_id' => '1',
'user_id' => '2',
'photographer_id' => '3',
'attributes_id' => '2,3',
'product_type' => '0',
'status' => 'pending',
'is_deleted' => '0',
'created_at' => '2016-05-15 04:08:21',
'updated_at' => '2016-05-16 12:11:23',
'username' => 'user',
'photographer' => 'photographer',
'product_name' => 'sdf' ,
),
array(
'id' => '1',
'order_id' => '1',
'product_id' => '1',
'user_id' => '2',
'photographer_id' => '3',
'attributes_id' => '2,3',
'product_type' => '0',
'status' => 'pending',
'is_deleted' => '0',
'created_at' => '2016-05-15 04:08:21',
'updated_at' => '2016-05-16 12:11:23',
'username' => 'user',
'photographer' => 'photographer',
'product_name' => 'sdf' ,
),
'attributesValue' => array(
0 => 'Attributes 2, Attributes 3'
1 => 'Attributes 1, Attributes 3'
)
)
);
我想你的数组应该是这样的
<?php
$your_arr = array(
'items' => array(
array(
'id' => '1',
'order_id' => '1',
'product_id' => '1',
'user_id' => '2',
'photographer_id' => '3',
'attributes_id' => '2,3',
'product_type' => '0',
'status' => 'pending',
'is_deleted' => '0',
'created_at' => '2016-05-15 04:08:21',
'updated_at' => '2016-05-16 12:11:23',
'username' => 'user',
'photographer' => 'photographer',
'product_name' => 'sdf' ,
),
array(
'id' => '1',
'order_id' => '1',
'product_id' => '1',
'user_id' => '2',
'photographer_id' => '3',
'attributes_id' => '2,3',
'product_type' => '0',
'status' => 'pending',
'is_deleted' => '0',
'created_at' => '2016-05-15 04:08:21',
'updated_at' => '2016-05-16 12:11:23',
'username' => 'user',
'photographer' => 'photographer',
'product_name' => 'sdf' ,
),
'attributesValue' => array(
0 => 'Attributes 2, Attributes 3'
1 => 'Attributes 1, Attributes 3'
)
)
);
使用此代码,它对您很有帮助:)
使用此代码,它对您很有帮助:)
你能发布你的实际数组吗?不是一个明确的问题…请指定你想要实现的行为。根据上下文,我们可以假设您希望将attributesValue
中的每个条目与items
排除attributesValue
中的每个条目相匹配,但不清楚是否是这样。明白了,请检查我的答案。您可以发布实际数组吗?问题不清楚……请指定您想要实现的行为。根据上下文,我们可以假设您希望将attributesValue
中的每个条目与items
中的每个条目进行匹配,排除attributesValue
,但不清楚是否是这样。明白了,请检查我的答案。我认为循环应该经过$your_arr[“items”]
忽略“AttributeValue”键。很好,我的答案呢?我认为循环应该越过$your_arr[“items”]
忽略“AttributeValue”键。很好,我的答案呢?很好,我的答案呢?很好,我的答案呢?欢迎来到StackOverflow!请考虑对代码添加一些解释。谢谢大家!@Aurasphere谢谢:)欢迎来到StackOverflow!请考虑对代码添加一些解释。谢谢大家!@(谢谢:)
<?php
$new_arr = array();
foreach($your_arr as $key => $item) {
$new_arr[$key] = $item;
if(isset($your_arr['attributesValue'][$key])) {
$new_arr[$key]['attributes'] = $your_arr['attributesValue'][$key];
}
}
echo '<pre>';
print_r($new_arr);
?>
function add_attr($items, $attr) {
$items->attributesValue = $attr;
return $items;
}
$attribute = $items['attributesValue'];
unset($items['attributesValue']);
$new_array = array_map(add_attr, $items, $attribute);
print_r($new_array);
$var = array();
foreach($array['items'] as $k => $v)
{
$var[$k] = $v;
$var[$k]['attributesValue'] = $array['items']['attributesValue'][$k];
}
unset($var['attributesValue']);
print_r($var);