katie hudson
katie hudson

Reputation: 2893

Getting data from Arrays

I am making an API call and receive some data. 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. 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. 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.

The output I am after is something like this

[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?

Thanks

Upvotes: 0

Views: 65

Answers (2)

Mattijs
Mattijs

Reputation: 372

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. 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.

Upvotes: 1

reyad
reyad

Reputation: 1432

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:

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

Upvotes: 1

Related Questions