0

I generate json files manually this way:

$output_json = '{
"tabela1": {
    "ilosc_wpisow": ';
    $output_json = $output_json.'"'."$ile_wierszy_1".'",'.'
    "tabela": [';


    for ($i = 1; $i <= $ile_wierszy_1; $i++) {
    $g = $i-1;
    $szukana_wartosc_array2[$g] = str_replace(" ",'\n',$szukana_wartosc_array[$g]); 
    $output_json = $output_json.'
    {
    "bajty_przed": '.'"'.$bajty_przed_array[$g].'",
    ';
    $output_json = $output_json.'"szukana_wartosc": '.'"'.$szukana_wartosc_array2[$g].'",
    ';
    $output_json = $output_json.'"bajty_po": '.'"'.$bajty_po_array[$g].'",
    ';
    $output_json = $output_json.'"nowa_wartosc": '.'"'.$nowa_wartosc_array[$g].'"
    }';
    if ($i!=$ile_wierszy_1) { $output_json = $output_json.','; }
    }
    $output_json = $output_json.'
    ]
},';


        $output_json = $output_json.'
"tabela2": {
    "ilosc_wpisow": ';
    $output_json = $output_json.'"'."$ile_wierszy_2".'",'.'
    "tabela": [';

    for ($i = 1; $i <= $ile_wierszy_2; $i++) {
    $g = $i-1;

        $output_json = $output_json.'
    {
    "szukana_wartosc2": '.'"'.$szukana_wartosc2_array[$g].'",
    ';
    $output_json = $output_json.'"zamien_na": '.'"'.$zamien_na_array[$g].'",
    ';
    $output_json = $output_json.'"przesuniecie": '.'"'.$przesuniecie_array[$g].'"
    }';
    if ($i!=$ile_wierszy_2) { $output_json = $output_json.','; }
    }
    $output_json = $output_json.'
    ]
}
}';

Someone help me create the function json_encode? I tried in different ways to add variables to the array, but every time I have something different on the output.

Output: https://pastebin.com/f8keXaY7

6
  • Show how you're creating an array. If you need help, you can take a valid json that you've built and use json_decode on it to see how to build it. Commented May 5, 2020 at 17:28
  • I'm not sure that I understand the goal. You would like to create a function, which works in the same way as the json_encode? Or you just want to create a simple json? Commented May 5, 2020 at 17:35
  • Your approach is completely wrong. You have 2 inbuilt functions in PHP json_encode and json_decode. Please post the question and expected result clearly, so that we can help you. Commented May 5, 2020 at 17:36
  • https://www.php.net/manual/en/function.json-encode.php There are several examples in this documentation. Take sometime to understand and apply. Commented May 5, 2020 at 17:37
  • you can just create php standart class object and serialize it in the end Commented May 5, 2020 at 17:39

1 Answer 1

1

First Step : Build your json structure in a php array and store it in a variable.

Something like that.

$json_source = [
    'tabela1' => [
        "ilosc_wpisow" => $ile_wierszy_1,
        "tabela"       => array_map(function($key) use ($szukana_wartosc_array, $bajty_przed_array, $bajty_po_array, $nowa_wartosc_array) {
            return [
                "bajty_przed"     => $bajty_przed_array[$key],
                "szukana_wartosc" => str_replace(" ", '\n', $szukana_wartosc_array[$key]),
                "bajty_po"        => $bajty_po_array[$key],
                "nowa_wartosc"    => $nowa_wartosc_array[$key],
            ];
        }, array_keys($szukana_wartosc_array)),

    ],
    'tabela2' => [
        'ilosc_wpisow' => $ile_wierszy_2,
        'tabela'       => array_map(function($key) use ($szukana_wartosc2_array, $zamien_na_array, $przesuniecie_array) {
            return [
                'szukana_wartosc2' => $szukana_wartosc2_array[$key],
                "zamien_na"        => $zamien_na_array[$key],
                "przesuniecie"     => $przesuniecie_array[$key],
            ];
        }, array_keys($szukana_wartosc2_array)),
    ],
];

Second Step : Use json_encode function.

echo json_encode($json_source);

And, If your output is always different, it means that your input is always different.

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

Comments

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.