-1

i have an array and i want to translate form this

array(3) {
  [0]=>
  string(3) "tim"
  [1]=>
  string(5) "laura"
  [2]=>
  string(3) "mel"
}

to this:

array( "tim", "laura", "mel" );

I already tried

array_keys()

doens't work.

Also

array_push()

I always receive the array with indexes [0], [1], [2].. can anyone help?

EDIT: I have to send this array to an API Call.

When i try hardcoded

$array = array(3) { [0]=> string(3) "tim", [1]=> string(5) "laura", [2]=> string(3) "mel" }

The API will get send an invalid error

If i try

$array = [ "tim", "laura", "mel" ];

i will work.

I also don't the any difference but the API does....

pennylane
  • 362
  • 2
  • 5
  • 18
  • https://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array – J.Wennergren Aug 25 '21 at 17:43
  • @J.Wennergren your suggestion makes no sense. The array is already "flat", it is not multidimensional. Read mine and Kiko's comments. – ADyson Aug 25 '21 at 17:54
  • Are you perhaps trying to output a comma separated string? – waterloomatt Aug 25 '21 at 18:10
  • no the API will receive an array(), string not work only array without the [0] => etc.. i tried several times hardcoded arrays – pennylane Aug 25 '21 at 18:39
  • 1
    its `$array = array(3) { [0]=> string(3) "tim", [1]=> string(5) "laura", [2]=> string(3) "mel" }` a wrong sintax.. its only expanded output. – Pavlo Mezhevikin Aug 25 '21 at 18:46
  • 1
    it will be more useful to see how you collect your array. – Pavlo Mezhevikin Aug 25 '21 at 18:48
  • 1
    Please post the relevant code that shows the array and how you're posting it to the API. – waterloomatt Aug 25 '21 at 19:28
  • Agreed, what you're claiming in the edit doesn't make much sense. `The API will get send an invalid error`... actually your own PHP would crash if you literally wrote that as PHP code...it would not get as far as calling any APIs - because it's not valid PHP, it looks like the output from var_dump...but that's just a text format for debugging, not usable code. `$array = [ "tim", "laura", "mel" ];` might work if that's then JSON-encoded before sending to the API, but it's just a different way of writing `$array = array( "tim", "laura", "mel" );`...the resulting data structure is identical. – ADyson Aug 25 '21 at 21:06
  • I'm now starting to wonder if you're getting very confused between what is valid PHP code to declare and populate an array variable, and what is a text-based representation of that variable after going through an encoding process (such as JSON or XML or var_dump output). You can write code to populate a variable and use it within your program, but to then put that data elsewhere (e.g. to transmit it in a HTTP API request or store it in a file) you have to encode it into a _representation_ of that data, either in text or binary format... – ADyson Aug 25 '21 at 21:08
  • ...Now, you haven't yet shown us the code you use to actually make the API request but I would assume your $array is then encoded into a format such as JSON before being sent in the request. There are many such encoding formats. JSON, XML, and form-url-encoded are the most popular ones to use with HTTP, at least partly because they are easy to decode again at the other end of the wire. – ADyson Aug 25 '21 at 21:12

1 Answers1

0

These two things are the same.

The second one is how you would write the first one in short form syntax. If you don't specify any keys, then numbered keys starting at 0 are assigned automatically by PHP.

$arr = array( "tim", "laura", "mel" );
var_dump($arr);

outputs

array(3) {
  [0]=>
  string(3) "tim"
  [1]=>
  string(5) "laura"
  [2]=>
  string(3) "mel"
}

This:

$arr = array( 0 => "tim", 1 => "laura", 2 => "mel" );

specifies the keys explicitly, but the outcome is identical to the first example.

Live demo: http://sandbox.onlinephpfunctions.com/code/9430def190dcec4dff70a3ab60cc54ee2f3ad326

Therefore, you've already got what you're asking for, so the question doesn't really make sense. You can't have an array which doesn't have any indexes (even if you don't specify them) - there needs to be a way to identify/access each item. Indexes are useful, and at worst they're completely harmless. I can't think of a reason to not want them, but perhaps you can explain your thinking.

ADyson
  • 51,527
  • 13
  • 48
  • 61
  • if i send the array( 0 => "tim", 1 => "laura", 2 => "mel" ) to an API i get an error. Only ["tim", "laura", "mel"] works.. i know its the same but the API will only work when i send the second statement (i try hard coded) – pennylane Aug 25 '21 at 18:34
  • 1
    `["tim", "laura", "mel"]` isn't the same as what you asked for in the question. That looks like JSON - which is commonly used in API requests. So you can just use `json_encode()` to output the array in that format. JSON is just a text format. It's not the same as the PHP syntax question you appeared to be asking. IMO your downvote is unjustified because in the question you asked for something different than what you actually wanted. You never mentioned the `["tim", "laura", "mel"]` format until now. – ADyson Aug 25 '21 at 20:50