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?
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"
}
}
I think you just want to do have the JSON as a string:
$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"}]';
Then Decode the JSON string to an array:
print_r(JSON_decode($myData, TRUE));
Which while produce the following PHP array:
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
)
)
I use json_encode and json_decode all the time and it comes in really handy when using PHP with Node.js and js in general.