提取对象属性以在数组

时间:2017-04-22 14:28:57

标签: php arrays object

我有一个带有一些值的JSON字符串,我将其反序列化为一个对象(STDClass / Object)。

其中有几个值,但我必须将其中的8个值精确地提取到多维数组中。此代码段显示了我在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"

我想提取这些值并将它们放入这样的数组中:

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

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

and so forth


)

这是可行的,还是我需要手工完成?

2 个答案:

答案 0 :(得分:1)

使用您提供的所有条件(您的原始数据结构,非序列化到对象而不是数组等),并假设它们都是不可协商的,这就是我如何提取碎片你在寻找:

$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);

这只是概念的程序性例子。在一个实际的程序中,我会创建一个接受输入并产生输出的函数/方法。该函数甚至可能需要循环开始和结束数字,也许某些参数用于指定属性名称的模板。这一切都取决于您的需求是如何通用的。

无论如何,我上面给出的代码应该产生:

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 :(得分:0)

我想你只想把JSON作为一个字符串:

$myData = '[{"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"}]';

然后将JSON字符串解码为数组:

print_r(JSON_decode($myData, TRUE));

同时生成以下PHP数组:

Array
(
[0] => stdClass Object
    (
        [picture1] => path/to/image/picture1.jpg
        [picture1_color] => #000000
    )

[1] => stdClass Object
    (
        [picture2] => path/to/image/picture2.jpg
        [picture2_color] => #111111
    )

[2] => stdClass Object
    (
        [picture3] => path/to/image/picture3.jpg
        [picture3_color] => #222222
    )

[3] => stdClass Object
    (
        [picture4] => path/to/image/picture4.jpg
        [picture4_color] => #333333
    )

)

我一直使用json_encode和json_decode,并且在将PHP与Node.js和js一般使用时非常方便。