数组在php

时间:2016-10-12 07:10:32

标签: php arrays

$ arr1适用于所有值。这里checked=false

$arr1 = [{"event_id":"2","name":"I have a new message","checked":"false"},{"event_id":"3","name":"A meeting awaits
     me","checked":"false"},{"event_id":"4","name":"Someone visited my profil","checked":"false"},{"event_id"
    :"5","name":"Someone like my profile","checked":"false"},{"event_id":"6","name":"My picture was approved"
    ,"checked":"false"},{"event_id":"7","name":"My picture was rejected","checked":"false"},{"event_id":"8"
    ,"name":"VIP purchase confirmation","checked":"false"}];

$ arr2仅用于检查值。这里checked=true

$arr2 = [{"event_id":"2","name":"I have a new message","checked":"true"},{"event_id":"3","name":"A meeting awaits
     me","checked":"true"},{"event_id":"4","name":"Someone visited my profil","checked":"true"},{"event_id"
    :"5","name":"Someone like my profile","checked":"true"},{"event_id":"7","name":"My picture was rejected"
    ,"checked":"true"},{"event_id":"8","name":"VIP purchase confirmation","checked":"true"}]

现在我想要一个如下所示的数组,其中“checked”:“false”和“checked”:“true”两者都在那里。怎么可能?

$arr3 = [{"event_id":"2","name":"I have a new message","checked":"true"},{"event_id":"3","name":"A meeting awaits
     me","checked":"true"},{"event_id":"4","name":"Someone visited my profil","checked":"true"},{"event_id"
    :"5","name":"Someone like my profile","checked":"true"},{"event_id":"6","name":"My picture was approved"
    ,"checked":"false"},{"event_id":"7","name":"My picture was rejected","checked":"true"},{"event_id":"8"
    ,"name":"VIP purchase confirmation","checked":"true"}];

1 个答案:

答案 0 :(得分:0)

  

你的问题必须是这样的: MERGE JSON OBJECTS

按照以下步骤,将b加入a要容易得多。

  1. 您需要为json_objectssingle array
  2. 执行json_decode()
  3. 然后在执行$arr1后,您需要使用$arr2,因为json_decode()之后您的数据将为array_merge(),因此您可以合并两个数组。< / LI>
  4. 如果您需要再次输出json_decode(),则需要执行最后一步,您需要为array()
  5. 后得到的结果数组执行json_object()

    您的场景示例

    json_encode()

    <强>输出:

    给定JSON字符串

    array_merge()

    合并后的JSON:

    <?php
    $json_first = '{
        "id": "#001",
        "username": "Name One",
        "type": "ADMIN",
        "status": "ACTIVE"   
    }';
    
    $json_second =  '{
        "id": "#002",
        "username": "Name Two",
        "type": "USER",
        "status": "INACTIVE"
    }';
    $user_info[] = json_decode($json_first,TRUE);
    $user_info[] = json_decode($json_second,TRUE);
    $json_merge = json_encode($user_info);
    ?>
    
    <h4>Given JSON String:</h4> 
    <p>
    <div>$json1 = <?php echo $json1; ?></div>
    <div>$json2 = <?php echo $json2; ?></div>
    </p>
    <h4>Output:</h4> 
    <div><?php echo $json_merge; ?></div>
    
      

    删除重复 ARRAY MERGE

    如果您需要合并数组并删除重复值,则可以选择{"id": "#001", "username": "Name One", "type": "ADMIN", "status": "ACTIVE" } {"id": "#002", "username": "Name Two", "type": "USER","status": "INACTIVE" } 函数。

    这将删除所有副本,然后您可以执行您需要的操作。

    [{"id": "#001", "username": "Name One", "type": "ADMIN", "status": "ACTIVE" },{"id": "#002", "username": "Name Two", "type": "USER","status": "INACTIVE" }]
    

    参考:http://php.net/manual/en/function.array-unique.php