0

I have a multi dimensional array that consists of 3 arrays inside. See below my minimum runnable example:

<?php

$array1 = array("Bitcoin", "Ethereum", "Bitcoin Cash");
$array2 = array("BTC", "ETH", "BTC");
$array3 = array("10000", "3000", "6666");


//Multi Dimensional Array
$multi = array($array1, $array2, $array3);
print_r($multi);

foreach($multi as $k =>$a){
  $multi[$k] = json_decode(json_encode($a));
}

print_r($multi);

// $json_data = json_encode($multi);
file_put_contents('data/myfile.json', $multi);    

However, I get the following output in my output file:

ArrayArrayArray

Any suggestions how to get the data like the following:

{
"Bitcoin", 
"BTC", 
"10000"
},
{
"Ethereum", 
"ETH", 
"3000"
},
{
"Bitcoin Cash", 
"BTC", 
"6666"
},
2
  • Does all three arrays always contain the same amount of items as each other? Commented Dec 8, 2017 at 13:19
  • 1
    Btw, the json you want is invalid. {} are for objects { key: 'value', key2: 'value2', ... } while [] is for arrays ['item1', 'item2', ...] Commented Dec 8, 2017 at 13:21

2 Answers 2

1

First off, your expected result isn't valid json. It should be arrays [] instead of {}.

Below is how you can format the data like you want it.
However, it assumes that the three arrays always are of equal length:

$array1 = array("Bitcoin", "Ethereum", "Bitcoin Cash");
$array2 = array("BTC", "ETH", "BTC");
$array3 = array("10000", "3000", "6666");

$new = [];

// Restructure your array to get one item from each array
foreach ($array1 as $index => $value) {
    $new[] = [
        $value,
        $array2[$index],
        $array3[$index]
    ];
}

$multi = json_encode($new);

This would produce the format you want, but in valid json format:

[
    [
        "Bitcoin",
        "BTC",
        "10000"
    ],
    [
        "Ethereum",
        "ETH",
        "3000"
    ],
    [
        "Bitcoin Cash",
        "BTC",
        "6666"
    ]
]

Demo: https://3v4l.org/39kuA

Sign up to request clarification or add additional context in comments.

Comments

1
{
"Bitcoin", 
"BTC", 
"10000"
},
{
"Ethereum", 
"ETH", 
"3000"
},
{
"Bitcoin Cash", 
"BTC", 
"6666"
},

Is not valid JSON format. '{' and '}' means that it's map (object), so there must be 'key:value' rows like:

{
0: "Bitcoin"
}

Proper JSON for array uses '[' and ']'.


<?php
$array1 = array("Bitcoin", "Ethereum", "Bitcoin Cash");
$array2 = array("BTC", "ETH", "BTC");
$array3 = array("10000", "3000", "6666");

//Multi Dimensional Array
$multi = array();
foreach($array1 as $key => $v) {
    $multi[] = [$array1[$key], $array2[$key], $array3[$key]];
}

file_put_contents('data/myfile.json', json_encode($multi)); 

Produces:

[["Bitcoin","BTC","10000"],["Ethereum","ETH","3000"],["Bitcoin Cash","BTC","6666"]]

Formated:

[
    [
        "Bitcoin",
        "BTC",
        "10000"
    ],
    [
        "Ethereum",
        "ETH",
        "3000"
    ],
    [
        "Bitcoin Cash",
        "BTC",
        "6666"
    ]
]

4 Comments

That's not the result the OP is asking for, though.
Tags are 'php json multidimensional-array', not 'php weird-format miltidimensional-array'. He wanted to format PHP to JSON arrays, so I did that. Updated answer with information about proper format.
That's not what I mean. I'm 100% game on the invalid json. But look at the data inside. The OP wants to combine the three arrays.
Ops. Code fixed.

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.