0

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
  • 1
    By hand? How would you go about doing that? With a paper and pen? Commented Apr 22, 2017 at 14:31
  • just extracting it manually i mean ofcourse Commented Apr 22, 2017 at 14:31

2 Answers 2

1

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"
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah this is the way to go. I have to check if the number and picture is existing because sometimes there are only 3 or 2 pictures defined instead of 4. But i can make it work, thanks for the insight.
Ha, I almost suggested a check for whether they're set. Glad it helped!
0

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.

2 Comments

Your output differs slightly from the OP's desired output. Note that he wants the key names of picture and color in the resulting second dimension arrays of the output.
Yep, yours was spot on! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.