0

I have array named $result_for_json_all

Array
(
[0] => Array
    (
    )

[1] => Array
    (
        [0] => Array
            (
                [CurrencyAbbreviation] => AUD
                [DateOfCurrencyRate] => 2012-12-11
                [NumberOfInputRow] => 1
                [FinalCurrencyRate] => 0.571
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [CurrencyAbbreviation] => CAD
                [DateOfCurrencyRate] => 2013-12-22
                [NumberOfInputRow] => 4
                [FinalCurrencyRate] => 0.48
            )

        [1] => Array
            (
                [NumberOfInputRow] => 2
                [FinalCurrencyRate] => 
            )

        [2] => Array
            (
                [NumberOfInputRow] => 3
                [FinalCurrencyRate] => 
            )

    )

)

Want to get

Array
(
[0] => Array
            (
                [CurrencyAbbreviation] => AUD
                [DateOfCurrencyRate] => 2012-12-11
                [NumberOfInputRow] => 1
                [FinalCurrencyRate] => 0.571
            )

[1] => Array
            (
                [CurrencyAbbreviation] => CAD
                [DateOfCurrencyRate] => 2013-12-22
                [NumberOfInputRow] => 4
                [FinalCurrencyRate] => 0.48
            )

[2] => Array
            (
                [NumberOfInputRow] => 2
                [FinalCurrencyRate] => 
            )

[3] => Array
            (
                [NumberOfInputRow] => 3
                [FinalCurrencyRate] => 
            )


)

Based on this https://stackoverflow.com/a/9416851/2118559 created code

function flatten($result_for_json_all) {
$arr = array();
foreach($result_for_json_all as $k => $v) {
    if(is_array($v)) {
        $arr = array_merge($arr, flatten($v, $k));
    }
    else{
        $arr[$k] = $v;
    }
}
return $arr;
}

echo '<pre>';
print_r(flatten($result_for_json_all));
echo 'result_for_json_all<pre>';

But get

 Array
 (
[CurrencyAbbreviation] => AUD
[DateOfCurrencyRate] => 2013-12-22
[NumberOfInputRow] => 3
[FinalCurrencyRate] => 
 )

Please, advice what need to correct to get necessary result

2 Answers 2

3
<?php
    $result = array();

    foreach ($result_for_json_all as $outer_elt) {
        foreach ($outer_elt as $inner_elt) {
            $result[] = $inner_elt;
        }
    }

    var_dump($result);
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Hey try something like this:

<?php
// The original data
$org = Array(
  0 => Array(),
  1 => Array(
    0 => Array( "CurrencyAbbreviation" => "AUD" )
  ),
  2 => Array(
    0 => Array( "CurrencyAbbreviation" => "CAD", "DateOfCurrencyRate" => "2013-12-22"),
    1 => Array( "NumberOfInputRow" => 2 )
  )
);
print_r($org);

// The new array transformed
$arr = Array();
foreach ($org as $v1){
  foreach($v1 as $v2){
    $arr[] = $v2; 
  }
}
print_r($arr);
?>

1 Comment

Other than using shorter variable names, how is this any different from my answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.