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