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

时间:2019-02-26 12:58:31

标签: php arrays

我有此代码:

$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);

这将输出:

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
)

这需要输出什么: 如果我使用第一个输出以便在数组中具有6个值,我想要什么:

$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

但是与其他$ ItemID一起,它也可以输出:

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
)

我想要的是,如果变量不在数字列表中,那么将1-10设置为空,并且如果数字在数字数组中,则将该数字设置为对应的值变量。例如,[0]处的数字为1,然后将变量$ FreeTextField1设置为$ NumbersArray [0]-> Value。

我一直在进行各种循环,但我只是被卡在数组长度改变的事实所困扰,所以在一个$ itemID处[6]可以是数字10,而在另一个$ ItemID处,该数字可以是6。

我尝试研究此问题,但我什至不知道要在Google中键入什么才能找到此问题,这就是为什么我在这里进行描述。

编辑我试图再次描述它: 是的,我在描述我想要的东西时遇到问题,所以让我再试一次。我得到两个数组作为输出之一,其编号与它所处的位置相对应,例如,您具有FreeTextField0至FreeTextField10。我试图做的是if (Numbers[0] == 0){ $FreeTextField0 = Value[0]},但是我遇到Numbers [0]可以是3或其他问题的问题,因为如果FreeTextField1为空,我不会得到null值,但是什么也没有。

如果我使用第一个输出,那么数组中有6个值,我想要什么:

$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 个答案:

答案 0 :(得分:0)

使用${$var}
如果您想查看有关此类var的文档,可以在这里查看:

http://php.net/manual/en/language.variables.variable.php

答案 1 :(得分:0)

我认为这是您所追求的,但是我不得不说我认为您在这里树错了树。您实际上不应该在脚本中动态创建变量。对我来说,这是一个严重的代码错误,我认为您应该在这里评估您的设计。

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);

更新

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);