Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to encode three php arrays that all have the same amount of values. From there it has to be json_encoded.

Array ( [0] => Title1
        [1] => 320 KB/s 5.0 MB 1:24
        [2] => http://www.example.com/Title1.mp3 )

Array ( [0] => Title2
        [1] => 120 KB/s 2.8 MB 3:29
        [2] => http://www.example.com/Title2.mp3 )

Array ( [0] => Title3
        [1] => 250 KB/s 8.5 MB 4:59
        [2] => http://www.example.com/Title3.mp3 )

The json needs to be a json array like the example below.

{
    "result": [
        {
            "link": "http://www.example.com/Title1.mp3",
            "metadata": "320 KB/s 5.0 MB 1:24",
            "title": "Title1"
        },
        {
            "link": "http://www.example.com/Title2.mp3",
            "metadata": "120 KB/s 2.8 MB 3:29",
            "title": "Title2"
        },
        {
            "link": "http://www.example.com/Title3.mp3",
            "metadata": "250 KB/s 8.5 MB 4:59",
            "title": "Title3"
        }
    ]
}
share|improve this question
    
What's the issue that you're facing? –  Ohgodwhy Jan 2 at 5:36
    
Have you found json_encode helpful here? –  Tim Pierce Jan 2 at 5:38
    
What have you tried till now? –  user2936213 Jan 2 at 5:45

3 Answers 3

In order to get the proper keys for your values in the JSON you will need to define those in your arrays that you have now. Something like this would work:

$joinedArrays = array($array1, $array2, $array3);

$JSONarray = array();
foreach($joinedArrays as $array){
    $JSONarray[] = array("title" => $array[0], "metadata" => $array[1], 
        "link" => $array[2]);
}

echo json_encode($JSONarray);

So first you compile all of the arrays that you have into one array and then in a foreach loop you create a new associative array with the proper keys and values and add that in as a new value in the $JSONarray, thereby creating a multidimensional array. Once you have all of your arrays parsed into new associative arrays, you can run json_encode($JSONarray); to get out a JSON formatted in the style that you have asked for.

If you don't compile the information into an associative array first, you will get out a JSON that looks like this:

[
    result: 
        {
            0:"Title1", 
            1:"320 KB/s 5.0 MB 1:24", 
            2:"http://www.example.com/Title1.mp3"
        },
        {
            0:"Title2", 
            1:"120 KB/s 2.8 MB 3:29", 
            2:"http://www.example.com/Title2.mp3"
        },
        {
            0:"Title3", 
            1:"250 KB/s 8.5 MB 4:59",
            2:"http://www.example.com/Title3.mp3"
        }
]
share|improve this answer
1  
this gives Illegal offset type error. –  user2936213 Jan 2 at 5:57
    
@user2936213 Actually, it does work. Here is an example –  Brayden Winterton Jan 2 at 6:06
    
Yeah but this array is haveing index also [0] [1] [2]. with this it gives error. –  user2936213 Jan 2 at 6:11
    
@user2936213 True, before the json_encode() $JSONarray does have keys [0] [1] [2] for the arrays inside of it, but json_encode() handles this just fine and gives the proper JSON objects requested by the OP –  Brayden Winterton Jan 2 at 6:18

I tried this so i get your resulted json response like that hop you want that too

<?php
$arr1[] = array('title'=>'one','value'=>'one');
$arr1[] = array('title'=>'two','value'=>'two');
$arr1[] = array('title'=>'three','value'=>'three');
$arr['result'] = $arr1;

echo '<pre>';print_r(json_encode($arr));
share|improve this answer

You could just add each array to a new, multidimensional array like this:

$all_data = array();
$all_data[] = $first_array;
$all_data[] = $second_array;
$all_data[] = $third_array;

$json = json_encode($all_data);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.