Home php Enumerate an array of objects in PHP

Enumerate an array of objects in PHP

Author

Date

Category

already broke the head, I can not lead to the right mind.
There is an array of objects

$ parameters = [
{Name: "Immovable_Type", Value: "Apartment"}
{Name: "Count_Room", Value: "1"},
{Name: "Count_Room", Value: "2"},
{Name: "Count_Room", Value: "3"}
{name: "cost_from", Value: ""},
{Name: "Cost_to", Value: ""},
{Name: "City", Value: "637640"}
];

I need to get an array of type:

$ parameters = [
"IMMOVABLE_TYPE" = & gt; "Apartment",
"Count_Room" = & gt; "1,2,3"},
"COST_FROM" = & gt; "",
"Cost_TO" = & gt; "",
"City" = & gt; "637640"
];

Guru Programming, Help)


Answer 1, Authority 100%

In theory of the usual cycle, there should be enough and slight checks:

$ parameters = [
  ['name' = & gt; "Immovable_type", 'value' = & gt; "Apartment"],
  ['name' = & gt; "Count_Room", 'Value' = & gt; "1"],
  ['name' = & gt; "Count_Room", 'Value' = & gt; "2"],
  ['name' = & gt; "Count_Room", 'Value' = & gt; "3"],
  ['name' = & gt; "Cost_from", 'Value' = & gt; ""],
  ['name' = & gt; "Cost_to", 'Value' = & gt; ""],
  ['name' = & gt; "City", 'Value' = & gt; "637640"]
];
$ result = [];
Foreach ($ Parameters AS $ Parameter) {
  if (! isset ($ result [$ parameter ['name']])) {
    $ result [$ parameter ['name']] = $ parameter ['value'];
  } else {
    $ Result [$ Parameter ['name']]. = ','. $ parameter ['value'];
  }
}
VAR_DUMP ($ result);

Result:

array (5) {
 ["Immovable_type"] = & gt;
 String (9) "Apartment"
 ["Count_Room"] = & gt;
 String (5) "1,2,3"
 ["COST_FROM"] = & gt;
 String (0) ""
 ["COST_TO"] = & gt;
 String (0) ""
 ["CITY"] = & gt;
 String (6) "637640"
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions