如何将数据从数组推入对象?

时间:2018-09-06 11:31:45

标签: arrays oop object foreach push

这是我的对象$classes

object(Doctrine\ORM\Mapping\ClassMetadata)#2984 (40) {
  ["name"]=>
  string(18) "App\Entity\Members"
  ["namespace"]=>
  string(10) "App\Entity"
  ["rootEntityName"]=>
  string(18) "App\Entity\Members"
  ["customGeneratorDefinition"]=>
  NULL
  ["customRepositoryClassName"]=>
  string(29) "App\Repository\UserRepository"
  ["isMappedSuperclass"]=>
  bool(false)
  ["isEmbeddedClass"]=>
  bool(false)
  ["parentClasses"]=>
  array(0) {
  }
  ["subClasses"]=>
  array(0) {
  }
  ["embeddedClasses"]=>
  array(0) {
  }
  ["namedQueries"]=>
  array(0) {
  }
  ["namedNativeQueries"]=>
  array(0) {
  }
  ["sqlResultSetMappings"]=>
  array(0) {
  }
  ["identifier"]=>
  array(1) {
    [0]=>
    string(2) "id"
  }
  ["inheritanceType"]=>
  int(1)
  ["generatorType"]=>
  int(4)
  ["fieldMappings"]=>
  array(5) {
    ["id"]=>
    array(9) {
      ["fieldName"]=>
      string(2) "id"
      ["type"]=>
      string(7) "integer"
      ["scale"]=>
      int(0)
      ["length"]=>
      NULL
      ["unique"]=>
      bool(false)
      ["nullable"]=>
      bool(false)
      ["precision"]=>
      int(0)
      ["id"]=>
      bool(true)
      ["columnName"]=>
      string(2) "id"
    }
    ["username"]=>
    array(8) {
      ["fieldName"]=>
      string(8) "username"
      ["type"]=>
      string(6) "string"
      ["scale"]=>
      int(0)
      ["length"]=>
      int(25)
      ["unique"]=>
      bool(true)
      ["nullable"]=>
      bool(false)
      ["precision"]=>
      int(0)
      ["columnName"]=>
      string(8) "username"
    }
    ["password"]=>
    array(8) {
      ["fieldName"]=>
      string(8) "password"
      ["type"]=>
      string(6) "string"
      ["scale"]=>
      int(0)
      ["length"]=>
      int(64)
      ["unique"]=>
      bool(false)
      ["nullable"]=>
      bool(false)
      ["precision"]=>
      int(0)
      ["columnName"]=>
      string(8) "password"
    }
    ["email"]=>
    array(8) {
      ["fieldName"]=>
      string(5) "email"
      ["type"]=>
      string(6) "string"
      ["scale"]=>
      int(0)
      ["length"]=>
      int(191)
      ["unique"]=>
      bool(true)
      ["nullable"]=>
      bool(false)
      ["precision"]=>
      int(0)
      ["columnName"]=>
      string(5) "email"
    }
    ["isActive"]=>
    array(8) {
      ["fieldName"]=>
      string(8) "isActive"
      ["type"]=>
      string(7) "boolean"
      ["scale"]=>
      int(0)
      ["length"]=>
      NULL
      ["unique"]=>
      bool(false)
      ["nullable"]=>
      bool(false)
      ["precision"]=>
      int(0)
      ["columnName"]=>
      string(9) "is_active"
    }
  }
  ["fieldNames"]=>
  array(5) {
    ["id"]=>
    string(2) "id"
    ["username"]=>
    string(8) "username"
    ["password"]=>
    string(8) "password"
    ["email"]=>
    string(5) "email"
    ["is_active"]=>
    string(8) "isActive"
  }
  ["columnNames"]=>
  array(5) {
    ["id"]=>
    string(2) "id"
    ["username"]=>
    string(8) "username"
    ["password"]=>
    string(8) "password"
    ["email"]=>
    string(5) "email"
    ["isActive"]=>
    string(9) "is_active"
  }
  ["discriminatorValue"]=>
  NULL
  ["discriminatorMap"]=>
  array(0) {
  }
  ["discriminatorColumn"]=>
  NULL
  ["table"]=>
  array(1) {
    ["name"]=>
    string(7) "members"
  }
  ["lifecycleCallbacks"]=>
  array(0) {
  }
  ["entityListeners"]=>
  array(0) {
  }
  ["associationMappings"]=>
  array(0) {
  }
  ["isIdentifierComposite"]=>
  bool(false)
  ["containsForeignIdentifier"]=>
  bool(false)
  ["idGenerator"]=>
  object(Doctrine\ORM\Id\IdentityGenerator)#3899 (1) {
    ["sequenceName":"Doctrine\ORM\Id\IdentityGenerator":private]=>
    NULL
  }
  ["sequenceGeneratorDefinition"]=>
  NULL
  ["tableGeneratorDefinition"]=>
  NULL
  ["changeTrackingPolicy"]=>
  int(1)
  ["isVersioned"]=>
  NULL
  ["versionField"]=>
  NULL
  ["cache"]=>
  NULL
  ["reflClass"]=>
  object(ReflectionClass)#3915 (1) {
    ["name"]=>
    string(18) "App\Entity\Members"
  }
  ["isReadOnly"]=>
  bool(false)
  ["namingStrategy":protected]=>
  object(Doctrine\ORM\Mapping\UnderscoreNamingStrategy)#2435 (1) {
    ["case":"Doctrine\ORM\Mapping\UnderscoreNamingStrategy":private]=>
    int(0)
  }
  ["reflFields"]=>
  array(5) {
    ["id"]=>
    object(ReflectionProperty)#3916 (2) {
      ["name"]=>
      string(2) "id"
      ["class"]=>
      string(18) "App\Entity\Members"
    }
    ["username"]=>
    object(ReflectionProperty)#3917 (2) {
      ["name"]=>
      string(8) "username"
      ["class"]=>
      string(18) "App\Entity\Members"
    }
    ["password"]=>
    object(ReflectionProperty)#3918 (2) {
      ["name"]=>
      string(8) "password"
      ["class"]=>
      string(18) "App\Entity\Members"
    }
    ["email"]=>
    object(ReflectionProperty)#3919 (2) {
      ["name"]=>
      string(5) "email"
      ["class"]=>
      string(18) "App\Entity\Members"
    }
    ["isActive"]=>
    object(ReflectionProperty)#3920 (2) {
      ["name"]=>
      string(8) "isActive"
      ["class"]=>
      string(18) "App\Entity\Members"
    }
  }
  ["instantiator":"Doctrine\ORM\Mapping\ClassMetadataInfo":private]=>
  object(Doctrine\Instantiator\Instantiator)#2989 (0) {
  }
}

这是我的数组$fields

array(5) {
  [0]=>
  object(App\Entity\Fields)#5481 (4) {
    ["id":"App\Entity\Fields":private]=>
    int(1)
    ["field_name":"App\Entity\Fields":private]=>
    string(2) "id"
    ["field_type":"App\Entity\Fields":private]=>
    string(6) "hidden"
    ["field_options":"App\Entity\Fields":private]=>
    string(0) ""
  }
  [1]=>
  object(App\Entity\Fields)#5485 (4) {
    ["id":"App\Entity\Fields":private]=>
    int(2)
    ["field_name":"App\Entity\Fields":private]=>
    string(8) "password"
    ["field_type":"App\Entity\Fields":private]=>
    string(8) "password"
    ["field_options":"App\Entity\Fields":private]=>
    string(0) ""
  }
  [2]=>
  object(App\Entity\Fields)#5486 (4) {
    ["id":"App\Entity\Fields":private]=>
    int(3)
    ["field_name":"App\Entity\Fields":private]=>
    string(4) "role"
    ["field_type":"App\Entity\Fields":private]=>
    string(8) "dropdown"
    ["field_options":"App\Entity\Fields":private]=>
    string(12) "admin,member"
  }
  [3]=>
  object(App\Entity\Fields)#5487 (4) {
    ["id":"App\Entity\Fields":private]=>
    int(4)
    ["field_name":"App\Entity\Fields":private]=>
    string(5) "email"
    ["field_type":"App\Entity\Fields":private]=>
    string(5) "email"
    ["field_options":"App\Entity\Fields":private]=>
    string(0) ""
  }
  [4]=>
  object(App\Entity\Fields)#5488 (4) {
    ["id":"App\Entity\Fields":private]=>
    int(5)
    ["field_name":"App\Entity\Fields":private]=>
    string(9) "is_active"
    ["field_type":"App\Entity\Fields":private]=>
    string(6) "hidden"
    ["field_options":"App\Entity\Fields":private]=>
    string(0) ""
  }
}

我要做的是将field_type的{​​{1}}值合并到$fields的{​​{1}}中。

所以在fieldMappings中,这是我要达到的最终结果:

$classes

这是我的方法:

$classes

我的错误消息是

  

未定义索引:field_name

1 个答案:

答案 0 :(得分:1)

您已经忘记了$ fields是一个数组,并且您尝试访问的索引不会在该数组的索引级别退出,除非将其设置为索引级别..我认为这种方法应该可以解决问题

foreach ($classes->fieldMappings as $fieldMapping) {
foreach($fields as $field ) 
{  if($fieldMapping['fieldName']==$field['field_name']) 
  {
   $fields.push($field['field_type']); 
  }
 }
}