AnswerBun.com

PHP JsonEncode For MultiDimensional Array

Here’s the two arrays:

Array (
    [0] => https://google.com/
    [1] => https://bing.com/
)
    
Array (
    [0] => Google
    [1] => Bing
)

Here’s the output i want in JSON:

[
    {
        "url": "https://google.com/",
        "name": "Google"
    },
    {
        "url": "https://bing.com/",
        "name": "Bing"
    }
]

I’m not able to get both the array in foreach loop and using json_encode to print them in JSON format.

Stack Overflow Asked by Kalui on December 31, 2020

3 Answers

3 Answers

Note that this solution requires both arrays (in my case $domains and $names) have the entries in the same order.

$domains = [
    'https://google.com/',
    'https://bing.com/'
];

$names = [
    'Google',
    'Bing'
];

$output = [];

// Itterate over the domains
foreach($domains as $key => $value){
    // And push into the $output array
    array_push(
        $output,
        // A new array that contains
        [
            // the current domain in the loop
            "url" => $value,
            // and the name, in the same index as the domain.
            "name" => $names[$key]
        ]
    );

}

// Finally echo the JSON output.
echo json_encode($output);

// The above line will output the following:
//[
//    {
//        "url": "https://google.com/",
//        "name": "Google"
//    },
//    {
//        "url": "https://bing.com/",
//        "name": "Bing"
//    }
//]

Correct answer by KodeFor.Me on December 31, 2020

$urls = [
    'https://google.com/',
    'https://bing.com/'
];

$names = [
    'Google',
    'Bing'
];

$combined = array_map(
  fn($url, $name) => ['url' => $url, 'name' => $name],
  $urls,
  $names
);

echo(json_encode($combined));

Of course, the arrays need to have the same number of elements, in the same order.
See it in action.


Remark

The arrow function (fn($url, $name) => ['url' => $url 'name' => $name]) works only on PHP 7.4 and newer versions.
For older versions use the full syntax for anonymous functions:

function($url $name) { 
  return ['url' => $url, 'name' => $name];
}

Answered by axiac on December 31, 2020

Your array should look like this:

$data = [
    ['url' => 'https://google.com/', 'name' => 'Google'],
    ['url' => 'https://bing.com/', 'name' => 'Bing'],
];

Answered by WoodyDRN on December 31, 2020

Add your own answers!

Related Questions

Can i show all variable results in a div in jQuery?

0  Asked on December 5, 2021 by nippledisaster

   

Comparing two generics in compareTo()

2  Asked on December 5, 2021 by chalupabatmac

       

arranging the json file base on key value

1  Asked on December 5, 2021 by prateek-singh

     

Vue watch method not filtering object

3  Asked on December 5, 2021

   

Passing a variable between HTML pages using JavaScript

2  Asked on December 5, 2021 by robert-little

   

How to avoid code redundancy with JAVA generics

1  Asked on December 5, 2021 by cosimoth

   

Why is my project no longer updating when I write new code?

1  Asked on December 5, 2021 by stephen-horton

       

node https ssl your connection is not private error

2  Asked on December 5, 2021 by andrea-dattero

       

Nested for loop using dict

9  Asked on December 5, 2021 by purwu

     

React Hooks – Input loses focus when 1 character is typed in

4  Asked on December 5, 2021 by dragos-strugar

   

How do I build a C Python Extension for Windows using Cygwin64?

2  Asked on December 5, 2021 by zenith2198

       

Wificonfiguration is deprecated Android 10

1  Asked on December 5, 2021 by ahmad-idrees

         

Wrong tootip in chartjs

1  Asked on December 5, 2021 by thiago-scher

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.