Php 将阵列重新组合在一起
我正在尝试编写一个类,允许您在基于php MVC的项目中建立两个模型之间的关系。我想创建一个自定义对象,该对象中只有您想要的字段,因此我写了以下内容:Php 将阵列重新组合在一起,php,arrays,object,Php,Arrays,Object,我正在尝试编写一个类,允许您在基于php MVC的项目中建立两个模型之间的关系。我想创建一个自定义对象,该对象中只有您想要的字段,因此我写了以下内容: class CreateRelationShipObjects { private $modelObjects; public function __construct($modelObjects){ if(is_array($modelObjects) && isset($modelObjec
class CreateRelationShipObjects {
private $modelObjects;
public function __construct($modelObjects){
if(is_array($modelObjects) && isset($modelObjects)){
$this->modelObjects = $modelObjects;
}
}
public function createRelationShip($columns){
if(is_array($columns)){
foreach($columns as $custom_name=>$column){
foreach($this->modelObjects as $name => $modelObject){
$this->checkForColumnInModelObject($modelObject, $column, $custom_name);
}
}
}
}
private function checkForColumnInModelObject($modelObject, $column, $custom_name){
$relationship = array();
if(property_exists($modelObject, $column)){
$value_returned = $modelObject->$column;
$relationship = array($column => $value_returned);
var_dump($relationship);
}
//return $this->toObj($relationship);
}
private function toObj($releationshipArray){
//var_dump((Object)$releationshipArray);
}
}
我们的想法是,您可以这样使用该类:
$relationship = new CreateRelationShipObjects(array(
'equipment_ip_models' => $equipmentIpModels,
'site_models' => $siteModel,
'equipment_models' => $equipmentModel
));
$relationship->createRelationShip(array(
'equipment_model' => 'model',
'equipment_manufacture' => 'manufacture',
'equipment_ip' => 'ipAddress',
'site_name' => 'name',
'site_id' => 'code'
));
在上面的课程中,我们将查看为专栏提供的每个模型。然后返回一个与您在createRelationShip
问题是,我用我的var\u dump
返回一个如下数组:
array(1) {
["model"]=>
string(13) "4168.21.33.03"
}
array(1) {
["manufacture"]=>
string(6) "ALLGON"
}
// ... And so on
该数组应该如下所示:
array(
'equipment_model' => '4168.21.33.03',
'equipment_manufacture' => 'ALLGON',
// .. and so on ...
);
有人能告诉我我做错了什么吗?也许这样效果更好?如果修改这两个函数
public function createRelationShip($columns){
if(is_array($columns)){
$relationship = array();
foreach($columns as $custom_name=>$column){
foreach($this->modelObjects as $name => $modelObject){
$relationship += $this->checkForColumnInModelObject($modelObject, $column, $custom_name);
}
}
var_dump($relationship);
}
}
private function checkForColumnInModelObject($modelObject, $column, $custom_name){
if(property_exists($modelObject, $column)){
$value_returned = $modelObject->$column;
return array($column => $value_returned);
}
return array();
}
下面的课程基本上就是我需要做的。我花了整整一个晚上,但是我写了它。它接收我传递给它的东西,然后还给我一个我想要的东西
class RelationshipObject {
/**
* Stores the models
*
* @var type
*/
private $modelObjects;
/**
* Takes in the objects
*
* @param array $objects
*/
public function __construct($objects) {
$this->modelObjects = $objects;
}
/**
* Creates a custom object based on the array passed in.
*
* @param array $outputKeysToInputKeys
* @return obj
*/
public function create(array $outputKeysToInputKeys) {
$result = array();
foreach ($outputKeysToInputKeys as $outputKey => $inputKey) {
foreach ($this->modelObjects as $object) {
if (property_exists($object, $inputKey))
$result[$outputKey] = $object->$inputKey;
}
}
return $this->to_obj($result);
}
/**
* Converts an array to an object.
*
* @param array $array
* @return obj
*/
public function to_obj($array){
return (Object)$array;
}
}
Neal请看更新的OP,这是第一个
var_dump
为你提供数组的var_dump
ed。我还更新了我希望得到的,而不是我得到的。