7

I want to create objects with {a,b,c,d} and have an array of this objects:

[{1,1,3,5},{3,1,7,7},{3,5,7,1}]

Values a b c and d are generating in a lopp

How can I create that object? And how can I add into my array that objects?

5
  • function json_encode Commented Jun 21, 2016 at 20:32
  • sounds like json_encode and json_decode. But can you tell me a little more about what you need, the description doesn't tell us much of what you need. Commented Jun 21, 2016 at 20:33
  • I want only 1 array with n objects. All the objects have the same attributes: a,b,c,d. I think I cant use json_encode Commented Jun 21, 2016 at 20:38
  • can you show the real structure of this input [{1,1,3,5},{3,1,7,7},{3,5,7,1}](cause it's invalid notation) ? What set of properties have your current objects? Commented Jun 21, 2016 at 20:46
  • What about properties of that objects? Currently it looks like "XY" problem (meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Commented Jun 21, 2016 at 20:53

1 Answer 1

11

There is no way to write object literals in PHP like you can in JavaScript or other languages.

The easiest way you can do this in PHP is using type casting

// associative array
$arr = ["a" => 1, "b" => 2, "c" => 3, "d" => 4];

// cast as object
$obj = (object) $arr;

// Or you could do it all in one line
$obj = (object) ["a" => 1, "b" => 2, "c" => 3, "d" => 4];

Check it out

echo $obj->a; // 1
echo $obj->b; // 2
echo $obj->c; // 3
echo $obj->d; // 4
echo json_encode($obj); // {"a":1,"b":2,"c":3,"d":4}

Your loop might look something like this

$objects = [];
for ($i=0; $i<4; $i++) {
  // i'll just makeup some values for a,b,c,d here since i don't know
  // how you are assigning them
  $objects[] = (object) [
    "a" => $i,
    "b" => $i * 2,
    "c" => $i * $i,
    "d" => rand()
  ];
}
print_r($objects);

Output

Array
(
    [0] => stdClass Object
        (
            [a] => 0
            [b] => 0
            [c] => 0
            [d] => 102971157
        )

    [1] => stdClass Object
        (
            [a] => 1
            [b] => 2
            [c] => 1
            [d] => 167903564
        )

    [2] => stdClass Object
        (
            [a] => 2
            [b] => 4
            [c] => 4
            [d] => 1894248447
        )

    [3] => stdClass Object
        (
            [a] => 3
            [b] => 6
            [c] => 9
            [d] => 929037839
        )

)

And the JSON output

[
  {"a":0,"b":0,"c":0,"d":102971157},
  {"a":1,"b":2,"c":1,"d":167903564},
  {"a":2,"b":4,"c":4,"d":1894248447},
  {"a":3,"b":6,"c":9,"d":929037839}
]

EDIT

how I could order my array by attribute b?

First create two reusable comparators

function ascendingComparator($a, $b) {
  if ($a > $b)      return 1;
  else if ($a < $b) return -1;
  else              return 0;
}

function descendingComparator($a, $b) {
  return -1 * ascendingComparator($a, $b);
}

Then use usort to pass the b property into the comparator

// sort ascending; lowest b value will be first in the array
usort($objects, function($x, $y) {
  return ascendingComparator($x->b, $y->b);
});
echo json_encode($objects);

// OR sort descending; highest b value will be first in the array
usort($objects, function($x, $y) {
  return descendingComparator($x->b, $y->b);
});
echo json_encode($objects);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks!! And then how I could order my array by attribute b ?
What type of value is b? An integer?
Yes, is an integer
@LigaVirtualF1 I've added information on how to sort

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.