eval.in

Paste #517204

PHP — PHP 5.5.14, pasted 2 years ago (json)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php // http://stackoverflow.com/questions/35318315/how-to-convert-php-object-array-from-one-form-to-another

// include __DIR__ . '/__bootstrap__.php';

$_temp = array
(
    "Title" => Array
        (
            "Title 1",
            "Title 2",
            "Title 3",
            "Title 4",
            "Title 5",
            "Title 6",
            "Title 7",
        ),

    "description" => Array
        (
            "Description 1",
            "Description 2",
            "Description 3",
            "Description 4",
            "Description 5",
            "Description 6",
            "Description 7",
        ),

    "myimage" => Array
        (
            "images/events/568a7048f22f9/sizes/athens-img_XS.jpg",
            "images/collections/56b2294894c08/islamiki.png",
            "images/multimedia/item6.jpg",
            "",
            "image 005",
            "image 006",
            "image 007",
            
        ),
    "newProp1" => Array
        (
            "new Prop 1 - 001",
            "new Prop 1 - 002",
            "new Prop 1 - 003",
            "new Prop 1 - 004",
            "new Prop 1 - 005",
            "new Prop 1 - 006",
            "new Prop 1 - 007",
        ),
    "anotherProp2" => Array
        (
            "another Prop 2 - 001",
            "another Prop 2 - 002",
            "another Prop 2 - 003",
            "another Prop 2 - 004",
            "another Prop 2 - 005",
            "another Prop 2 - 006",
            "another Prop 2 - 007",
        ),

);

$srcObject = (object) $_temp; // input ar required...

// ---------------------------------------------------------------------------
// output in here...
$outArray = convertArray($srcObject);                        
// $outArray = convertArray((object) array());                        

echo '<pre>';
print_r($outArray);
echo '</pre>';
exit;
// ----------------------------------------------------------------------------

function convertArray($srcObject) {
    $propsAsArray = get_object_vars($srcObject);  
    $rowKeys = array_keys($propsAsArray);         

    for ($i = 0, $itemCount = count(current($propsAsArray)); $i < $itemCount; $i++) { 

        $outArray[] = array_combine($rowKeys, 
                                    array_column($propsAsArray, $i)); 
    }

    return (object) $outArray;              
}

Program Output

<pre>stdClass Object
(
    [0] => Array
        (
            [Title] => Title 1
            [description] => Description 1
            [myimage] => images/events/568a7048f22f9/sizes/athens-img_XS.jpg
            [newProp1] => new Prop 1 - 001
            [anotherProp2] => another Prop 2 - 001
        )

    [1] => Array
        (
            [Title] => Title 2
            [description] => Description 2
            [myimage] => images/collections/56b2294894c08/islamiki.png
            [newProp1] => new Prop 1 - 002
            [anotherProp2] => another Prop 2 - 002
        )

    [2] => Array
        (
            [Title] => Title 3
            [description] => Description 3
            [myimage] => images/multimedia/item6.jpg
            [newProp1] => new Prop 1 - 003
            [anotherProp2] => another Prop 2 - 003
        )

    [3] => Array
        (
            [Title] => Title 4
            [description] => Description 4
            [myimage] => 
            [newProp1] => new Prop 1 - 004
            [anotherProp2] => another Prop 2 - 004
        )

    [4] => Array
        (
            [Title] => Title 5
            [description] => Description 5
            [myimage] => image 005
            [newProp1] => new Prop 1 - 005
            [anotherProp2] => another Prop 2 - 005
        )

    [5] => Array
        (
            [Title] => Title 6
            [description] => Description 6
            [myimage] => image 006
            [newProp1] => new Prop 1 - 006
            [anotherProp2] => another Prop 2 - 006
        )

    [6] => Array
        (
            [Title] => Title 7
            [description] => Description 7
            [myimage] => image 007
            [newProp1] => new Prop 1 - 007
            [anotherProp2] => another Prop 2 - 007
        )

)
</pre>

OK (0.016 sec real, 0.023 sec wall, 14 MB, 830 syscalls)

Fork