如何在一个JSON对象中组合多个数组

时间:2021-06-21 12:19:40

I want to display multiple arrays into one such that each time the object is created it get a key value and then forms a object to be more specific following is a short example.

我想要将多个数组显示为一个这样的数组,每当对象被创建时,它就会得到一个键值,然后形成一个对象,使其更具体,下面是一个简短的例子。

How it is now:

现在是:

name:[{},{},{},{}]
surname:[{},{},{},{}]
phone: [{},{},{},{}]

How I want it to be:

我希望它是:

xyz:[{name:{}, surname{}, phone{}},
     {name:{}, surname{}, phone{}},
     {name:{}, surname{}, phone{}}]

I am exporting this in php so that I can use this JSON object in AngularJs ng-repeat directive.

我将它导出到php中,以便在AngularJs ng-repeat指令中使用这个JSON对象。

following is the code:

下面是代码:

  1. declaring array:

    声明数组:

    $name = array();
    $surname= array();
    $phone= array();
    
  2. assigning values WHICH IS UNDER FOR EACH LOOP

    为每个循环分配值

    $name[] = $values ($values wil have the values for the loop)
    .....
    
  3. testing output

    测试输出

    <?php echo json_encode($name);
        echo json_encode($surname);
        echo json_encode($phone);
    ?>
    

1 个解决方案

#1


1  

A simple way of doing it (run):

一种简单的方法(运行):

$name = array("name 1", "name 2");
$surname = array("surname 1", "surname 2");
$phone = array("phone 1", "phone 2");

$output = array();
for($i = 0; $i < count($name); $i++){
    $output[] = array(
            'name' => $name[$i],
            'surname' => $surname[$i],
            'phone' => $phone[$i],
        );
}

print_r($output);

Output:

输出:

Array
(
    [0] => Array
        (
            [name] => name 1
            [surname] => surname 1
            [phone] => phone 1
        )

    [1] => Array
        (
            [name] => name 2
            [surname] => surname 2
            [phone] => phone 2
        )

)

Note: It assumes all arrays are of same size and have same keys.

注意:它假定所有数组的大小相同,并且具有相同的键。

But maybe it would be better to instead of saving each value to 3 different variables and then running this script, try saving the values already into the array as the desired output (if possible, of course, since we can't know).

但是,与其将每个值保存为3个不同的变量,然后运行这个脚本,不如尝试将已经保存到数组中的值作为所需的输出(如果可能的话,当然,因为我们不知道)。

#1


1  

A simple way of doing it (run):

一种简单的方法(运行):

$name = array("name 1", "name 2");
$surname = array("surname 1", "surname 2");
$phone = array("phone 1", "phone 2");

$output = array();
for($i = 0; $i < count($name); $i++){
    $output[] = array(
            'name' => $name[$i],
            'surname' => $surname[$i],
            'phone' => $phone[$i],
        );
}

print_r($output);

Output:

输出:

Array
(
    [0] => Array
        (
            [name] => name 1
            [surname] => surname 1
            [phone] => phone 1
        )

    [1] => Array
        (
            [name] => name 2
            [surname] => surname 2
            [phone] => phone 2
        )

)

Note: It assumes all arrays are of same size and have same keys.

注意:它假定所有数组的大小相同,并且具有相同的键。

But maybe it would be better to instead of saving each value to 3 different variables and then running this script, try saving the values already into the array as the desired output (if possible, of course, since we can't know).

但是,与其将每个值保存为3个不同的变量,然后运行这个脚本,不如尝试将已经保存到数组中的值作为所需的输出(如果可能的话,当然,因为我们不知道)。