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

问题描述 投票:1回答:2

我有这个代码:

$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。

我一直在制作各种类型的循环,但我只是因为数组长度发生变化而变得困难,因此[6]可以是一个$ itemID的数字10,但是在另一个$ ItemID中,数字可以是6。

我尝试研究这个,但我甚至不知道我要输入谷歌找到这个问题,这就是为什么我在这里描述它。

编辑我试着第二次描述它:是的我在描述我想要的东西时遇到了问题,所以让我再试一次。我得到两个数组作为输出,数字与它所代表的位置相对应,例如你有FreeTextField0通过FreeTextField10。我试图做的是if (Numbers[0] == 0){ $FreeTextField0 = Value[0]},但后来我得到了Numbers [0]可能是3或其他的问题,因为如果FreeTextField1为空我没有得到空值但没有。

我想要的是如果我使用第一个输出,所以在数组中有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
php arrays
2个回答
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);

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

0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.