I want to remove key
and added comma
after each value, I have tired with foreach
but i coundn't sucuss.
$data = array:195 [▼
0 => 26404.759765625
1 => 26390.619140625
2 => 26391.9609375
3 => 26397.6796875
4 => 26421.630859375
5 => 26414.880859375
6 => 26409.33984375
7 => 26410.509765625
8 => 26403.220703125
9 => 26386.029296875
10 => 26400.109375
11 => 26412.369140625
12 => 26408.099609375
13 => 26418.029296875
14 => 26422.80078125
I am trying with below loop :
foreach($data as $key=>$value){
$dataes = $value
}
DD($dataes);
expected result should be like below:
array:195 [▼
26404.759765625,
26390.619140625,
26391.9609375,
26397.6796875,
so on...
To simply get the output as comma separated, you can use implode function.
string implode ( string $glue , array $pieces )
Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.
Try the following code:
// convert the $data array into a string
// It takes elements of the array, and join them with a comma (our input)
// and, returns a string
$string = implode(',', $data);
// just display the string
echo $string;
To echo the data comma separated you can use implode which takes an array and uses the "glue" between each array item and builds a string of it.
echo implode(",<br>", $data);
This will give you one item per line and a comma after each item.
Here you can see the output of the code.
https://3v4l.org/AHvu3C
Notice that this is a command line so html will show up as text.
Running the same code on a PHP interpreter and browser will make it correct html.
Since you have are making an API I assume json_encode is what you need.
Json_encode returns an array as a string that can be received and decoded easily.
echo json_encode( $data);
//[26404.759765625,26390.619140625,26391.9609375,26397.6796875,26421.630859375,26414.880859375,26409.33984375,26410.509765625,26403.220703125,26386.029296875,26400.109375,26412.369140625,26408.099609375,26418.029296875,26422.80078125]
So you need to do:
$dataes[] = implode(",
", $data);
This concatenates the values of the $data in one string (with new lines).
Honestly, I think your code is fine, you don't need to remove those keys.
In php, key-value is array.
If your code like this:
$b=[0 => 26404.759765625, 3 => 26390.619140625, 4 => 26391.9609375]
dd($b)
# In postman, it will show like this.
$data = array:3 [▼
0 => 26404.759765625,
3 => 26390.619140625,
4 => 26391.9609375
]
And you reset those keys, it will become a normal array.
But in postman when you use dd($b)
, it will still show those keys.
$data = array:3 [▼
0 => 26404.759765625,
1 => 26390.619140625,
2 => 26391.9609375
]
And you can use php artisan tinker
, It will show like this:
Be sure your key is integer. I think it will not affect the normal use.