Foreach数组返回最后一个值

时间:2020-11-12 17:57:24

标签: php

我正在学习OOP,我想在数组中创建对象列表,但是我的代码返回了最后一个数组 我在这里搜索过,但没有找到解决方案或想法。

例外

        "MerchantDefinedFields":[  
        {  
           "Id":2,
           "Value":"email@test.com"
        },
        {  
           "Id":4,
           "Value":"Web"
        },
        {  
           "Id":9,
           "Value":"NAO"
        },
        {  
           "Id":83,
           "Value":"Field"
        },
        {  
           "Id":84,
           "Value":"Only"
        }
     ]

我的代码

            $MDDs = array(
                array("Id" => 2, "Value" => "email@test.com"), 
                array("Id" => 4, "Value" => "Web"),
                array("Id" => 9, "Value" => "NO"),
                array("Id" => 83, "Value" => "Field"),
                array("Id" => 84, "Value" => "Only")
            );
             
            foreach($MDDs as $MDD){
                $abac = array("Id" => $MDD['Id'], "Value" => $MDD['Value']);
            }

结果

    Array
(
    [Id] => 84
    [Value] => PROPRIO
)

3 个答案:

答案 0 :(得分:2)

您的foreach()每次循环时都会重新设置$abac。因此,在最后一次运行时,会将变量设置为数组中的最后一项。

而不是每次都设置变量,请尝试将key-> value添加到数组(或类似的操作,具体取决于您的需要):

$abac = [];
foreach($MDDs as $MDD){
    $abac[] = array("Id" => $MDD['Id'], "Value" => $MDD['Value']);
}

很难确定确切的正确答案,因为不清楚您要完成什么,但这至少应该为您指明正确的方向。

答案 1 :(得分:0)

简单的答案:-您不需要org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'apiVaultClient': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.vault.core.VaultOperations com.myproject.encryption.ApiVaultClientImpl.vaultOperations; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vaultTemplate' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.core.VaultTemplate org.springframework.vault.config.AbstractVaultConfiguration.vaultTemplate()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientHttpRequestFactoryWrapper' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.config.AbstractVaultConfiguration$ClientFactoryWrapper org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper()] threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1148) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:636) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:934) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.vault.core.VaultOperations com.myproject.encryption.ApiVaultClientImpl.vaultOperations; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vaultTemplate' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.core.VaultTemplate org.springframework.vault.config.AbstractVaultConfiguration.vaultTemplate()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientHttpRequestFactoryWrapper' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.config.AbstractVaultConfiguration$ClientFactoryWrapper org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper()] threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:518) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) ... 22 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vaultTemplate' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.core.VaultTemplate org.springframework.vault.config.AbstractVaultConfiguration.vaultTemplate()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientHttpRequestFactoryWrapper' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.config.AbstractVaultConfiguration$ClientFactoryWrapper org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper()] threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:603) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1057) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:921) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:864) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:490) ... 24 more Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.core.VaultTemplate org.springframework.vault.config.AbstractVaultConfiguration.vaultTemplate()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientHttpRequestFactoryWrapper' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.config.AbstractVaultConfiguration$ClientFactoryWrapper org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper()] threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:181) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:592) ... 36 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientHttpRequestFactoryWrapper' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.config.AbstractVaultConfiguration$ClientFactoryWrapper org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper()] threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:603) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1057) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:305) at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c.clientHttpRequestFactoryWrapper(<generated>) at org.springframework.vault.config.AbstractVaultConfiguration.vaultTemplate(AbstractVaultConfiguration.java:108) at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c.CGLIB$vaultTemplate$12(<generated>) at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c$$FastClassBySpringCGLIB$$1da77a86.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:293) at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c.vaultTemplate(<generated>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:160) ... 37 more Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.config.AbstractVaultConfiguration$ClientFactoryWrapper org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper()] threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:181) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:592) ... 58 more Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration at org.springframework.vault.support.SslConfiguration.unconfigured(SslConfiguration.java:323) at org.springframework.vault.config.AbstractVaultConfiguration.sslConfiguration(AbstractVaultConfiguration.java:225) at org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper(AbstractVaultConfiguration.java:207) at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c.CGLIB$clientHttpRequestFactoryWrapper$13(<generated>) at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c$$FastClassBySpringCGLIB$$1da77a86.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:293) at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c.clientHttpRequestFactoryWrapper(<generated>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:160) 循环即可获得所需的结果 您可以简单地使用内置的php函数将数组转换为JSON

>>> import pyspark >>> from pyspark.sql import SQLContext >>> >>> >>> sc = pyspark.SparkContext('local[*]') >>> sqlContext = SQLContext(sc) >>> df = sqlContext.read.text("file.txt") >>> type(df) <class 'pyspark.sql.dataframe.DataFrame'>

现在要解决您的问题:-

您正在循环中重新分配foreach变量,而不是像那样向其添加值。

$abac = json_encode ( $MDDs);

答案 2 :(得分:0)

最好的方法是在$ foreach外部声明$ abac,然后使用array_push方法,如下所示:

$abac = array();

foreach($MDDs as $MDD)
{
    array_push($abac, array("Id" => $MDD['Id'], "Value" => $MDD['Value']));
}
print_r($abac);
相关问题