简体   繁体   English

从 Arrays 获取数据

[英]Getting data from Arrays

I am making an API call and receive some data.我正在拨打 API 电话并收到一些数据。 I then need to get this data and make my own array from it.然后我需要获取这些数据并从中创建我自己的数组。 I have noticed a little issue though.不过我注意到了一个小问题。 The data in question takes the following structure相关数据采用以下结构

Array
(
    [Cost] => Array
        (
            [ID] => 4233
            [Description] => Something
            [Quantity] => 4
        )

)
Array
(
    [Cost] => Array
        (
            [0] => Array
                (
                    [ID] => 1233
                    [Description] => Something
                    [Quantity] => 1           
                )

            [1] => Array
                (
                    [ID] => 2344
                    [Description] => Something
                    [Quantity] => 2
                )

        )

)

Now the problem is that within the Cost array, sometimes there are additional arrays like in the second example.现在的问题是,在 Cost 数组中,有时会有额外的 arrays,如第二个示例。 My initial code was like so我的初始代码是这样的

if (!empty($costs['Costs'])) {
    foreach ($costs['Costs'] as $field) {
        foreach ($field as $type) {
            print_r(is_array($type));
            $jData['costsInfo'][] = $type;
        }
    }
}

And this works perfectly for Cost with inner arrays like example two.这非常适用于具有内部 arrays 的 Cost,例如示例二。 But when it does not, it assigns the value to a numbered index.但如果没有,它会将值分配给编号索引。 I have tried testing if its an array, but it always seem to return true.我试过测试它是否是一个数组,但它似乎总是返回 true。

The output I am after is something like this我找的output是这样的

[costsInfo] => Array
(
    [0] => Array
        (
            [ID] => 4233
            [Description] => Something
            [Quantity] => 4   
        )

)
[costsInfo] => Array
(
    [0] => Array
        (
            [ID] => 1233
            [Description] => Something
            [Quantity] => 1       
        )

    [1] => Array
        (
            [ID] => 2344
            [Description] => Something
            [Quantity] => 2

        )

)

At the moment I am seeing this此刻我看到这个

[costsInfo] => Array
(
    [0] => 4233
    [1] => Something
    [2] => 4
)
[costsInfo] => Array
(
    [0] => Array
        (
            [ID] => 1233
            [Description] => Something
            [Quantity] => 1       
        )

    [1] => Array
        (
            [ID] => 2344
            [Description] => Something
            [Quantity] => 2

        )

)

So whats the best way to handle if the original array does not have inner arrays?那么如果原始数组没有内部 arrays,最好的处理方法是什么?

Thanks谢谢

Something like this should work:这样的事情应该有效:

$jData['costsInfo'] = array();
if (!empty($costs['Cost']) && is_array($costs['Cost'])) {
    if (array_key_first($costs['Cost']) === 0) {
        $jData['costsInfo'] = $costs['Cost'];
    } else {
        $jData['costsInfo'][] = $costs['Cost'];
    }
}

It's basically just checking what the first key for these Cost arrays is.它基本上只是检查这些Cost arrays 的第一个密钥是什么。 If it's a 0 then we assume that there must be multiple of them, and can add the whole "array collection" to our output.如果它是0那么我们假设它们必须有多个,并且可以将整个“数组集合”添加到我们的 output。

I guess the code is pretty self explanatory.我想代码很容易解释。 So, I didn't provide any description.所以,我没有提供任何描述。 If you have any question, ask me in the comment section.如果你有任何问题,请在评论部分问我。 I'll edit the answer.我会编辑答案。

<?php

// utility of function to check if
// $arr is sequential or not
// i.e. on contains 0, 1, ..., count($arr) -1 neumeric keys
function isSequential($arr) {
    if(count($arr) === 0) return false;
    return array_keys($arr) === range(0, count($arr) - 1);
}

function getCostsInfo($input) {
    $costs = $input["cost"];

    $costsInfo = [];
    
    if(isSequential($costs)) {
        foreach($costs as $cost) {
            $costsInfo[] = $cost;
        }
    } else {
        if(isset($costs["id"]) && isset($costs["description"]) && isset($costs["quantity"])) {
            $costsInfo[] = [
                "id" => $costs["id"],
                "description" => $costs["description"],
                "quantity" => $costs["quantity"]
            ];
        }
    }

    return $costsInfo;
}


$input1 = [
    "cost" => [
        "id" => 4233,
        "description" => "something",
        "quantity" => 4
    ]
];

$input2 = [
    "cost" => [
        [
            "id" => 1233,
            "description" => "something",
            "quantity" => 1
        ],
        [
            "id" => 2344,
            "description" => "something",
            "quantity" => 2
        ]
    ]
];

$costsInfo1 = getCostsInfo($input1);

echo "costsInfo1: ";
var_dump($costsInfo1);

$costsInfo2 = getCostsInfo($input2);
echo "costsInfo2: ";
var_dump($costsInfo2);
?>

And the output is:而 output 是:

costsInfo1: array(1) {
  [0]=>
  array(3) {
    ["id"]=>
    int(4233)
    ["description"]=>
    string(9) "something"
    ["quantity"]=>
    int(4)
  }
}
costsInfo2: array(2) {
  [0]=>
  array(3) {
    ["id"]=>
    int(1233)
    ["description"]=>
    string(9) "something"
    ["quantity"]=>
    int(1)
  }
  [1]=>
  array(3) {
    ["id"]=>
    int(2344)
    ["description"]=>
    string(9) "something"
    ["quantity"]=>
    int(2)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM