【问题标题】:Php array_push function in javajava中的php array_push函数
【发布时间】:2015-01-25 16:37:02
【问题描述】:

我正在尝试将以下 php 代码重写为 java

   foreach ($configValues as $data) {
    $temp['config_key'] = is_null($data->getConfigKey()) ? '' : $data->getConfigKey();
    $temp['config_value'] = is_null($data->getConfigValue()) ? '' : $data->getConfigValue();
    array_push($configArray, $temp);
}
foreach ($configPricing as $data) {
    $temp1['config_key'] = is_null($data->getType()) ? '' : $data->getType();
        $temp1['config_value'] = is_null($data->getPrice()) ? '' : $data->getPrice();
        array_push($configArray, $temp1);
    }

上面的代码将一个关联数组推入另一个数组,如何将它写入我以这种方式尝试过的java:

List<String> configList=new ArrayList();

        List<Config> config=configRepository.findAll();
        System.out.println("Config size:"+config.size());
        List<ConfigPricing> configPricing=configPricingRepository.findAll();
        System.out.println("configPricing size:"+configPricing.size());

        Map<String, String> response = new HashMap<String, String>();   
        ListMultimap<String, String> tempHashMap = ArrayListMultimap.create();      
        ArrayList configArray = new ArrayList();    



        for(Config data:config){

            if(data.getConfigKey() !=null && !"".equals(data.getConfigKey()))
            tempHashMap.put("config_key", data.getConfigKey() );

            if(data.getConfigValue() !=null && !"".equals(data.getConfigValue()))
            tempHashMap.put("config_value", data.getConfigValue().isEmpty() ? "" : data.getConfigValue());

        }
//      configArray.add(tempHashMap);

        for(ConfigPricing configPricingData:configPricing){
            if(configPricingData.getType() !=null && !"".equals(configPricingData.getType()))
            tempHashMap.put("config_key", configPricingData.getType().isEmpty() ? "" : configPricingData.getType() );

            if(configPricingData.getPrice() !=null && !"".equals(configPricingData.getPrice()))
            tempHashMap.put("config_value", configPricingData.getPrice().toString().isEmpty() ? "":configPricingData.getPrice().toString());
//          configArray.add(tempHashMap);

        }

但它存储的数据是这样的:

samekey1:valueone,valuetwo ,talue theree, samekey2:valueone,valuetwo ,talue theree,

为此,我想以以下方式存储它们:

samekey1:value
samekey2:value 

samekey1:value 
samekey2:value 

samekey1:value 
samekey2:value 

【问题讨论】:

标签: java php


【解决方案1】:

这可能有用,

// no key
array_push($configArray, $value);
// same as:
$configArray[] = $value;

// key already known
$configArray[$samekey1] = $value;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 2017-04-21
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多