-2

I want to use PHP's array_values() function solve the problem described here with json_encode and numerical indexes.

I have a PHP array with mixed things, like this (Laravel dump)

array:5 [
  "@context" => "https://schema.org"
  "@type" => "HowTo"
  "name" => "N"
  "supply" => array:1 [
    1 => array:2 [
      "@type" => "HowToSupply"
      "name" => "S"
    ]
  ]
  "step" => array:1 [
    0 => array:5 [
      "@type" => "HowToStep"
      "name" => null
      "url" => null
      "text" => "D"
      "image" => array:4 [
        "@type" => "ImageObject"
        "url" => null
        "width" => null
        "height" => null
      ]
    ]
  ]
]

When I use array_values(), it outputs this:

array:5 [
  0 => "https://schema.org"
  1 => "HowTo"
  2 => "N"
  3 => array:1 [
    1 => array:2 [
      "@type" => "HowToSupply"
      "name" => "S"
    ]
  ]
  4 => array:1 [
    0 => array:5 [
      "@type" => "HowToStep"
      "name" => null
      "url" => null
      "text" => "D"
      "image" => array:4 [
        "@type" => "ImageObject"
        "url" => null
        "width" => null
        "height" => null
      ]
    ]
  ]
]

I want to keep the items that already have keys and want to use array_values on those with numerical keys.

How could I do this?

Many thanks in advance!

PS: my real problem is that the json_encode() of the first array outputs the numerical keys, sth I don't want. People on this site suggested to use array_values() to fix this, but that gives the above problem. If you can help me with the original problem, that's of course also great

ralphjsmit
  • 477
  • 2
  • 16
  • So your only problem here is that `supply` starts with `1` instead of `0`? Then you need to use `array_values` *only* on that subarray. – El_Vanja Mar 30 '21 at 17:12
  • If you could alleviate this at the root (in the code that builds the array shown in your dump), it would be better than solving it here, after it has already been built. – El_Vanja Mar 30 '21 at 17:15
  • This might be useful: https://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential This looks like a method you can use to detect if your value is an associative array (in sequential order) or not. – WOUNDEDStevenJones Mar 30 '21 at 17:17
  • [From the docs](https://www.php.net/manual/en/function.array-values.php) "*array_values() returns all the values from the array and indexes the array numerically.*" so it does exactly what you want - gives a values without keys. – biesior Mar 30 '21 at 17:18
  • _“If you can help me with the original problem”_ - there is no helping with that. If the keys are _not_ numeric, consecutive and starting at zero, then there is no way to encode this as an array in JavaScript, it has to use object syntax then, and include the values of the keys. If json_encode did not behave the way it does here, then it would be _falsifying_ the original data, and of course no one can want that, it would make the function useless. – CBroe Mar 31 '21 at 07:56
  • Hey everyone, thanks for thinking along! @El_Vanja good that you spotted that. I didn't notice it that the array didn't start with 0 but with 1, sth which was because of a little error in the code that produces this array. Solving that meant that the array started again with 0 and that the problem didn't happen. Thanks for the link WOUNDEDStevenJones and for the explanation biesior and CBroe – I didn't see this initially but now I do. – ralphjsmit Mar 31 '21 at 08:42

1 Answers1

0

The answer lay in the fact that the array keys of my first array started with 1 rather than 0. Fixing that in the code that produced this initial array helped with it.

ralphjsmit
  • 477
  • 2
  • 16