dongxia527680 2019-02-26 12:58
浏览 61
已采纳

如何从变量数组长度获取数据?

I have this code:

$ItemID = 'a62442e2-ca1f-4fd1-b80d-0d0dc511758e'; 
$GET_FreeTextFields = new \Picqer\Financials\Exact\ItemExtraField($connection);

    $FreeTextFields = $GET_FreeTextFields->filter("ItemID eq guid'$ItemID'", '', '' );
    $FreeTextFields01 = array();
    $FreeTextFields02 = array();
    foreach($FreeTextFields as $GET_FreeTextFields){
        $FreeTextFields01[] = $GET_FreeTextFields->Value;
        $FreeTextFields02[] = $GET_FreeTextFields->Number;
    }
    print_r($FreeTextFields01);
    print_r($FreeTextFields02);

This outputs:

Value Array
(
    [0] => 390
    [1] => 804715
    [2] => WW001
    [3] => WHT/WHT/WHT
    [4] => 39/42
    [5] => 804715         WW00139/42
    [6] => 3pk Quarter Socks
)
Numbers Array
(
    [0] => 3
    [1] => 4
    [2] => 5
    [3] => 6
    [4] => 7
    [5] => 8
    [6] => 10
)

What this needs to output: What i want if i use the first output so with 6 values in the array:

$FreeTextField01 = null
$FreeTextField02 = null
$FreeTextField03 = 390
$FreeTextField04 = 804715
$FreeTextField05 = WW001
$FreeTextField06 = WHT/WHT/WHT
$FreeTextField07 = 39/42
$FreeTextField08 = 804715          WW00139/42
$FreeTextField09 = null
$FreeTextField10 = 3pk Quarter Socks

But with other $ItemID, it can also output:

Value Array
(
    [0] => 10100153
    [1] => 2007
    [2] => 350
    [3] => 804082
    [4] => WW006
    [5] => WHT/NNY/OXGM
    [6] => 35/38
    [7] => 804082         WW00635/38
    [8] => 0,00138857
    [9] => Champion 3pk Quarter Socks
)
Numbers Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)

What i want is that if a variable is not in the numbers list so 1-10, setting it to empty, and if the numbers is in the numbers array setting that number to the corresponding value variable. For example the number at [0] is 1 then set the variable $FreeTextField1 to $NumbersArray[0]->Value.

I keep making all kinds off loops but i just get stuck at the fact that the array length changes so that [6] can be the number 10 at one $itemID, but at another $ItemID, the number can be 6.

I tried researching this but I don't even know what I have to type in google to find this problem, that's why I'm describing it here.

edit i tried describing it a second time: Yeah I'm having problems describing what i want, so let me try again. I get two arrays as output one with numbers that correspond with place it stands, as example you have FreeTextField0 thru FreeTextField10. What i tried to do is if (Numbers[0] == 0){ $FreeTextField0 = Value[0]}, but then I get the problem that Numbers[0] can be 3 or something else because if FreeTextField1 is empty i don't get a null value but nothing.

What i want if i use the first output so with 6 values in the array:

$FreeTextField01 = null
$FreeTextField02 = null
$FreeTextField03 = 390
$FreeTextField04 = 804715
$FreeTextField05 = WW001
$FreeTextField06 = WHT/WHT/WHT
$FreeTextField07 = 39/42
$FreeTextField08 = 804715          WW00139/42
$FreeTextField09 = null
$FreeTextField10 = 3pk Quarter Socks
  • 写回答

2条回答 默认 最新

  • dozabg1616 2019-02-26 13:31
    关注

    I think this is what you're after but I have to say that I think you're barking up the wrong tree here. You really should not dynamically create variables in your script. To me, it is a serious code-smell and I think you should evaluate your design here.

    http://sandbox.onlinephpfunctions.com/code/b245a0218ce174e68508139872f394def5409b05

    <?php
    
    // Test case
    $values = [
        '390',
        '804715',
        'WW001',
        'WHT/WHT/WHT',
        '39/42',
        '804715         WW00139/42',
        '3pk Quarter Socks'
    ];
    
    $numbers = [3, 4, 5, 6, 7, 8, 10];
    
    // Flip the $numbers array and then use the keys to find the corresponding values in the $values array
    $intersection = array_unique(array_intersect_key($values, array_flip($numbers)));
    
    // Fill in the missing keys and use `null` as the value
    $output = $intersection + array_fill_keys(range(1,10), null);
    
    // Sort the final output by the keys
    ksort($output);
    
    // Format the keys to match FreeTextField{00}
    $output = array_combine(
        array_map(function($k){ return 'FreeTextField'.str_pad($k, 2, '0', STR_PAD_LEFT); }, array_keys($output)),
        $output
    );
    
    // Use the extract function to bring all those array keys + values into the symbol table. 
    // You can now use $FreeTextField01 - $FreeTextField10
    extract($output);
    
    var_dump($output);
    

    UPDATE

    http://sandbox.onlinephpfunctions.com/code/244c4f1ed45db398c48b8330c402b375eb358446

    <?php
    
    // Test case
    $input = [
        ['Value' => '390', 'Number' => 3],
        ['Value' => '804715', 'Number' => 4],
        ['Value' => 'WW001', 'Number' => 5],
        ['Value' => 'WHT/WHT/WHT', 'Number' => 6],
        ['Value' => '39/42', 'Number' => 7],
        ['Value' => '804715         WW00139/42', 'Number' => 8],
        ['Value' => '3pk Quarter Socks', 'Number' => 10],
    ];
    
    $intersection = [];
    
    foreach ($input as $config) {
    
        $value = $config['Value'];
        $number = $config['Number'];
    
        $intersection[$number] = $value;
    }
    
    // Fill in the missing keys and use `null` as the value
    $output = $intersection + array_fill_keys(range(1,10), null);
    
    // Sort the final output by the keys
    ksort($output);
    
    // Format the keys to match FreeTextField{00}
    $output = array_combine(
        array_map(function($k){ return 'FreeTextField'.str_pad($k, 2, '0', STR_PAD_LEFT); }, array_keys($output)),
        $output
    );
    
    // Use the extract function to bring all those array keys + values into the symbol table. 
    // You can now use $FreeTextField01 - $FreeTextField10
    extract($output);
    
    var_dump($output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料