0

I have a PHP function that's being called from an angularjs codebase via ajax. The data that's returned contains multidimensional arrays, nested arrays, all kinds of arrays.

This is how the data looks in the network tab of Chrome's dev tools; sorry for the screenshot but I can't seem to get the actual data to paste correctly: enter image description here

What our angular dev wants is for the options to be arrays, not objects. This is the sample he provided:

selects: [
{
  name: 'prod',
  options: [
    {
      text: 'What Flavor?'
      val: 'default_text'
    },
    {
      oop: false,
      text: 'Honey Bbq',
      val: 'HO'
    }]
}
]

This is what my print_r looks like. How can I change the format of my array to give the angular dev what he needs? I've tried using json_encode, but that ends up setting the entire value of options to be a quoted string of json, rather than an array.

[selects] => Array
(
    [0] => Array
        (
            [name] => prod
            [default] => What Flavor?
            [options] => Array
                (

                    [0] => Array
                        (
                            [val] => default_text
                            [text] => What Flavor?
                            [oop] => 
                        )

                    [1] => Array
                        (
                            [val] => HQ
                            [text] => Honey Bbq
                            [oop] => 
                        )
                )


        )

)
EmmyS
  • 11,892
  • 48
  • 101
  • 156
  • 1
    This generally happens when your array is not correctly numerically indexed, like `0 1 3 4` (skipping the 2). (Though in your screenshot it looks ok.) Try wrapping it in `array_values`. – Tobias K. Aug 16 '18 at 18:01
  • I'll give that a shot, thanks. I've never run into this issue before. – EmmyS Aug 16 '18 at 18:05
  • I ran into it before when I removed some values from my arrays using `array_filter` or `unset`. These functions do not reindex after removing the elements. – Tobias K. Aug 16 '18 at 18:10
  • I'm definitely using `unset`; my sample looks good because it's only the first few options - the actual array has 10-15 option nodes, and they do have some numbers missing. I've just never had to work to the spec of a pre-built front end; usually our angular devs build their code based on the data structure we provide. – EmmyS Aug 16 '18 at 18:12
  • Then you should definitely use `array_values`. Otherwise you risk to return either an array or an object depending on whether your indexes are correct, which will break your FE definitely if they assumed it is one of them. – Tobias K. Aug 16 '18 at 18:16
  • This question a duplicate of https://stackoverflow.com/q/11195692/7362396 then. – Tobias K. Aug 16 '18 at 18:17
  • @TobiasK - it's not really a duplicate; the accepted answer for that one was due to the array being non-sequential. Mine (even the final version not shown above) IS sequential; it just starts at 1 instead of 0. And I tried wrapping it in `array_values` and I'm still getting the same output on the json side. – EmmyS Aug 16 '18 at 18:20
  • Can you provide the full `print_r` and `json_encode` of an error case? And the code where you put `array_values`? – Tobias K. Aug 16 '18 at 18:24
  • Unforunately not. This is part of a huge function with multiple loops, plus proprietary data and code. I'll update the OP with what I can provide; I moved the `array_values` to a different place in the function and got a different result, although still wrong. – EmmyS Aug 16 '18 at 18:31
  • If your dev cannot cope with an array of objects ..... sack them – RiggsFolly Aug 16 '18 at 18:36
  • @RiggsFolly: I think that's not the issue. The issue is that he gets an object instead of an array. I from time to time do FE too and would also be mad, because for example I cannot simply `.map` an object in JS. – Tobias K. Aug 16 '18 at 18:40
  • @EmmyS: I hope you can figure out where to put `array_values`. I think it can be solved with it. One more thought: Chrome sometimes displays the indexes even it is an array, just to help you identify elements. So I would also look at the raw JSON instead of just DevTools. – Tobias K. Aug 16 '18 at 18:44
  • Possible duplicate of [json\_encode sparse PHP array as JSON array, not JSON object](https://stackoverflow.com/questions/11195692/json-encode-sparse-php-array-as-json-array-not-json-object) – EmmyS Aug 16 '18 at 21:27

0 Answers0