【问题标题】:array picks only the last value to store数组仅选择要存储的最后一个值
【发布时间】:2013-02-28 09:03:37
【问题描述】:

下面的脚本显然使用了http://www.hasoffers.com/wiki/Offer:create 中记录的 API。问题有(至少)两部分:a)如何在一个数组中存储多个数据集。 b) API 是否接受它....


当我运行脚本时,它只将最后一个值存储在“数据”中,我怎样才能让它一次存储更多数据?

下面的代码有例如 2 个值。一种叫LOLO,另一种叫LELE。 输出仅显示值 LELE。

这是代码。

<?php
header('Content-type: application/json');
$base = 'http://api.hasoffers.com/Api?';

$params = array(
'Format' => 'json'
,'Target' => 'Offer'
,'Method' => 'create'
,'Service' => 'HasOffers'
,'Version' => 2
,'NetworkId' => 'demo'
,'NetworkToken' => '....'
,'data' => array(
        'name' => 'LOLO'
        ,'description' => 'test'
        ,'offer_url' => 'http://google.nl'
        ,'preview_url' => 'http://google.nl'
        ,'expiration_date' => '08-08-2013'

        ,'name' => 'LELE'
        ,'description' => 'test'
        ,'offer_url' => 'http://google.nl'
        ,'preview_url' => 'http://google.nl'
        ,'expiration_date' => '08-08-2013' 
)
);

$url = $base . http_build_query( $params );

$result = file_get_contents( $url );


print_r( json_decode( $result) );
?>

这是输出

[request] => stdClass Object
    (
        [Target] => Offer
        [Format] => json
        [Service] => HasOffers
        [Version] => 2
        [Method] => create
        [NetworkId] => demo
        [NetworkToken] => NETU2nzMw8AYS6EGgjFrjGR88GcSiF
        [data] => stdClass Object
            (
                [name] => LELE
                [description] => test
                [offer_url] => http://google.nl
                [preview_url] => http://google.nl
                [expiration_date] => 08-08-2013
            )

    )

【问题讨论】:

  • 你有一个数组,里面只有一个元素,里面的键值重复,所以最后一个被记住了。
  • 但是当我运行脚本时,它会显示 [0]=>... 和 [1]=>... 我如何在不获取此值的情况下执行此操作,因为只有 Hasoffer 平台的 API接受 'data'=>[name].. not 'data'=>[0]=>[name]
  • 答案可能是(并且浏览了我认为是的 api 文档):您必须发送两个单独的请求,然后创建两个单独的报价。
  • 如果为这两个数组设置名称('data' => array('first'=> array('name' =>'LOLO','description'=>'test ' ,'offer_url' => 'google.nl' ,'preview_url' => 'google.nl' ,'expiration_date' => '08-08-2013'), 'second' => array('name' => 'LELE' ,'description' => 'test' ,'offer_url' => 'google.nl' ,'preview_url' => 'google.nl' ,'expiration_date' => '08-08-2013' ))) ?
  • 不,它不起作用.. 是否可以在每个数组的某种循环中设置请求?这样它每次都以新值循环

标签: php arrays json loops


【解决方案1】:
'data' => array(
  array (
    'name' => 'LOLO',
    'description' => 'test',
    'offer_url' => 'http://google.nl',
    'preview_url' => 'http://google.nl'
    'expiration_date' => '08-08-2013'),
  array (
    'name' => 'LELE',
    'description' => 'test',
    'offer_url' => 'http://google.nl',
    'preview_url' => 'http://google.nl',
    'expiration_date' => '08-08-2013'))

【讨论】:

【解决方案2】:
,'data' => array( array(
        'name' => 'LOLO'
        ,'description' => 'test'
        ,'offer_url' => 'http://google.nl'
        ,'preview_url' => 'http://google.nl'
        ,'expiration_date' => '08-08-2013'
        ),
        array(
        ,'name' => 'LELE'
        ,'description' => 'test'
        ,'offer_url' => 'http://google.nl'
        ,'preview_url' => 'http://google.nl'
        ,'expiration_date' => '08-08-2013' 
        )
)

您需要将其添加为多维数组,否则它将覆盖具有相同键的元素。请注意在您的array 中添加的array (....)

array( array(

【讨论】:

  • 你知道这个可以和hasoffers.com/wiki/Offer:create一起工作还是一个通用的答案?
  • 这是一个通用的答案,你需要像这样在php中指定多维数组
【解决方案3】:

或者可以这样存储

$data = array();
$data[0] = array('name' => 'LELE'
    , 'description' => 'test'
    , 'offer_url' => 'http://google.nl'
    , 'preview_url' => 'http://google.nl'
    , 'expiration_date' => '08-08-2013'
);

$data[1] = array('name' => 'LOLO'
    , 'description' => 'test'
    , 'offer_url' => 'http://google.nl'
    , 'preview_url' => 'http://google.nl'
    , 'expiration_date' => '08-08-2013'
);

$params = array(
    'Format' => 'json'
    , 'Target' => 'Offer'
    , 'Method' => 'create'
    , 'Service' => 'HasOffers'
    , 'Version' => 2
    , 'NetworkId' => 'demo'
    , 'NetworkToken' => 'NETU2nzMw8AYS6EGgjFrjGR88GcSiF'
    , 'data' => $data
);

print_r($params);

// 输出

Array
(
    [Format] => json
    [Target] => Offer
    [Method] => create
    [Service] => HasOffers
    [Version] => 2
    [NetworkId] => demo
    [NetworkToken] => NETU2nzMw8AYS6EGgjFrjGR88GcSiF
    [data] => Array
        (
            [0] => Array
                (
                    [name] => LELE
                    [description] => test
                    [offer_url] => http://google.nl
                    [preview_url] => http://google.nl
                    [expiration_date] => 08-08-2013
                )

            [1] => Array
                (
                    [name] => LOLO
                    [description] => test
                    [offer_url] => http://google.nl
                    [preview_url] => http://google.nl
                    [expiration_date] => 08-08-2013
                )

        )

)

【讨论】:

【解决方案4】:

a) 如何在一个数组中存储多个数据集。

$fixed_params = array(
 'Format' => 'json'
 ,'Target' => 'Offer'
 ,'Method' => 'create'
 ,'Service' => 'HasOffers'
 ,'Version' => 2
 ,'NetworkId' => 'demo'
 ,'NetworkToken' => '....'
);

$offer_data = array(
array(
        'name' => 'LOLO'
        ,'description' => 'test'
        ,'offer_url' => 'http://google.nl'
        ,'preview_url' => 'http://google.nl'
        ,'expiration_date' => '08-08-2013'
),
array(
        'name' => 'LELE'
        ,'description' => 'test'
        ,'offer_url' => 'http://google.nl'
        ,'preview_url' => 'http://google.nl'
        ,'expiration_date' => '08-08-2013' 
 )
);

// store all results here 
$result = array();

// iterates two times since $offer_data has two elements.
foraech ($offer_data as $offern => $data ){
 // store offer's data into fixed_params['data'] element.
 $fixed_params['data'] = $data;

 $url = $base . http_build_query( $fixed_params );

 $result[] =  json_decode( file_get_contents( $url ));
}

print_r($result);

b) API 是否接受它....

据我了解 API,它一次只接受一个创建。请参阅http://www.hasoffers.com/wiki/Offer:create,上面写着:创建新报价。

【讨论】:

    【解决方案5】:

    这就是我所做的并且它有效

    <?php
    
    // Bestand openen
    if (($file = fopen("test2.csv", "r")) !== FALSE) {
        // Eerste rij van Excel als value gebruiken
        $header = fgetcsv($file, 1000, ";");
    
    
        // Een loop door Excel file
        while (($data = fgetcsv($file, 1000, ";")) !== FALSE) {
    
    
    
                // combineer de eerste rij met de gegevens
                $combined = array_combine($header,$data);
    
                // Connectie maken met Hasoffers bij elke waarden
                $base = 'http://api.hasoffers.com/Api?';
    
                $params = array(
                    'Format' => 'json'
                    ,'Target' => 'Offer'
                    ,'Method' => 'create'
                    ,'Service' => 'HasOffers'
                    ,'Version' => 2
                    ,'NetworkId' => 'demo'
                    ,'NetworkToken' => '.....'
                    ,'data' => $combined
                );
    
                $url = $base . http_build_query( $params );
    
                $result = file_get_contents( $url );
    
                // Tijdelijk printen
                print_r( json_encode( $result) );
        }
    }
    ?>
    

    我创建了一个包含 CSV 文件的循环。 问题是它只与 hasoffer 连接一次,并且只允许一个值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 2022-01-03
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      相关资源
      最近更新 更多