我想创建一个像这样的对象数组:
[
'A' => { 'name':'name1', 'family':'family1'},
'B' => { 'name':'name2', 'family':'family2'},
'C' => { 'name':'name3', 'family':'family3'},
'D' => { 'name':'name4', 'family':'family4'},
'E' => { 'name':'name5', 'family':'family5'},
'F' => { 'name':'name6', 'family':'family6'},
];
我有一个数组常量名称和其他数组常量系列,因此我使用了foreach循环。但是结果是惊人的。为什么循环每个,更改所有名称和所有家庭?
$name = [
'A'=>'name1',
'B'=>'name2',
'C'=>'name3',
'D'=>'name4',
'E'=>'name5',
'F'=>'name6'
];
$family = [
'A'=>'family1',
'B'=>'family2',
'C'=>'family3',
'D'=>'family4',
'E'=>'family5',
'F'=>'family6'
];
$key = array_keys( $name );
$myArray = array_fill_keys( $key, new stdClass() );
foreach( $myArray as $k => $info ){
$myArray[$k]->name = $name[$k];
$myArray[$k]->family = $family[$k];
}
这是结果
[
'A' => { 'name':'name6', 'family':'family6'},
'B' => { 'name':'name6', 'family':'family6'},
'C' => { 'name':'name6', 'family':'family6'},
'D' => { 'name':'name6', 'family':'family6'},
'E' => { 'name':'name6', 'family':'family6'},
'F' => { 'name':'name6', 'family':'family6'},
];
答案 0 :(得分:0)
这会更简单
$name = [
'A'=>'name1',
'B'=>'name2',
'C'=>'name3',
'D'=>'name4',
'E'=>'name5',
'F'=>'name6'
];
$family = [
'A'=>'family1',
'B'=>'family2',
'C'=>'family3',
'D'=>'family4',
'E'=>'family5',
'F'=>'family6'
];
foreach( $name as $k => $info ){
$t = new stdClass();
$t->name = $info;
$t->family = $family[$k];
$myArray[$k] = $t;
}
结果
Array
(
[A] => stdClass Object
(
[name] => name1
[family] => family1
)
[B] => stdClass Object
(
[name] => name2
[family] => family2
)
[C] => stdClass Object
(
[name] => name3
[family] => family3
)
[D] => stdClass Object
(
[name] => name4
[family] => family4
)
[E] => stdClass Object
(
[name] => name5
[family] => family5
)
[F] => stdClass Object
(
[name] => name6
[family] => family6
)
)
然后,如果对它执行json_encode(),则会得到所需的内容
echo json_encode($myArray);
{
"A": {
"name": "name1",
"family": "family1"
},
"B": {
"name": "name2",
"family": "family2"
},
"C": {
"name": "name3",
"family": "family3"
},
"D": {
"name": "name4",
"family": "family4"
},
"E": {
"name": "name5",
"family": "family5"
},
"F": {
"name": "name6",
"family": "family6"
}
}
此行
$myArray = array_fill_keys( $key, new stdClass() );
使用对单个对象的引用填充数组。
因此,当您将数据加载到数组中的对象时,您将用一些数据填充单个对象。该对象在您所有发生的数组中都被引用(指向)。因此,您只会看到在所有数组事件中循环的最后一次迭代中加载的数据。
$name = [
'A'=>'name1',
'B'=>'name2',
'C'=>'name3',
'D'=>'name4',
'E'=>'name5',
'F'=>'name6'
];
$family = [
'A'=>'family1',
'B'=>'family2',
'C'=>'family3',
'D'=>'family4',
'E'=>'family5',
'F'=>'family6'
];
$key = array_keys( $name );
$myArray = array_fill_keys( $key, new stdClass() );
// looks like it should fill only the object in the first 'A' occurance
$myArray['A']->one = 1;
print_r($myArray);
结果
Array
(
[A] => stdClass Object ( [one] => 1 )
[B] => stdClass Object ( [one] => 1 )
[C] => stdClass Object ( [one] => 1 )
[D] => stdClass Object ( [one] => 1 )
[E] => stdClass Object ( [one] => 1 )
[F] => stdClass Object ( [one] => 1 )
)
答案 1 :(得分:0)
将您的foreach
循环更新为如下所示:
foreach( $myArray as $k => $info ){
$myArray[$k] = ['name' => $name[$k] , 'family' => $family[$k]];
}
答案 2 :(得分:-1)
$myArray = [];
foreach($name as $k => $v){
$myArray[$k] = (object)['name'=>$v, 'famili'=>$family[$k]];
}