dongxin9759 2017-04-22 14:28
浏览 18
已采纳

提取对象属性以在数组中使用

I have a JSON string with some values that I unserialized into an object (STDClass/Object).

There are a couple of values in it, but I have to extract exactly 8 of those values into an multidimensional array. This snippet shows the values I'm after as they existed in the JSON:

"picture1":"path/to/image/picture1.jpg"
"picture1_color":"#000000"
"picture2":"path/to/image/picture2.jpg"
"picture2_color":"#111111"
"picture3":"path/to/image/picture3.jpg"
"picture3_color":"#222222"
"picture4":"path/to/image/picture4.jpg"
"picture4_color":"#333333"

I want to extract these values and put them into an array like this:

Array
(
    [0] => Array
        (
            [picture] => path/to/image/picture1.jpg
            [color] => #000000
        )

    [1] => Array
        (
            [picture] => path/to/image/picture2.jpg
            [color] => #111111
        )

and so forth


)

Is this doable programmatically or do I just need to do it by hand?

  • 写回答

2条回答 默认 最新

  • doubu5154 2017-04-22 14:40
    关注

    Working with all conditions you've provided (your original data structure, un-serialization into an object instead of an array, etc.), and assuming they're all non-negotiable, this is how I would extract the pieces you're looking for:

    $json_serialized_input = '{"something":"foo","something_else":"bar","picture1":"path/to/image/picture1.jpg","picture1_color":"#000000","picture2":"path/to/image/picture2.jpg","picture2_color":"#111111","picture3":"path/to/image/picture3.jpg","picture3_color":"#222222","picture4":"path/to/image/picture4.jpg","picture4_color":"#333333"}';  
    
    $input = json_decode($json_serialized_input);
    
    $output = [];
    for ($i = 1; $i <= 4; $i++) {
        $current_picture_property = 'picture' . $i;
        $current_color_property = $current_picture_property . '_color';
    
        $output[] = [
            'picture' => $input->{$current_picture_property},
            'color' => $input->{$current_color_property},
        ];
    }
    
    var_dump($output);
    

    This is just a procedural example of the concepts. In an actual program I'd make a function/method that takes the input and produces the output. That function might even take starting and ending numbers to loop for, and maybe some sort of params for specifying a template for the property names. That all depends on how general purpose your needs are or are not.

    Anyway, the code I gave above should produce:

    array(4) {
      [0]=>
      array(2) {
        ["picture"]=>
        string(26) "path/to/image/picture1.jpg"
        ["color"]=>
        string(7) "#000000"
      }
      [1]=>
      array(2) {
        ["picture"]=>
        string(26) "path/to/image/picture2.jpg"
        ["color"]=>
        string(7) "#111111"
      }
      [2]=>
      array(2) {
        ["picture"]=>
        string(26) "path/to/image/picture3.jpg"
        ["color"]=>
        string(7) "#222222"
      }
      [3]=>
      array(2) {
        ["picture"]=>
        string(26) "path/to/image/picture4.jpg"
        ["color"]=>
        string(7) "#333333"
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!