-5

i have this multidimensional array.

Array
(
    [0] => Array
       (
          [firstName] => Hadi
          [lastName] => Pratama
          [age] => 22
       )

    [1] => Array
       (
          [firstName] => Jefri
          [lastName] => Ronaldo
          [age] => 21
       )

    [2] => Array
       (
          [firstName] => Rizky
          [lastName] => Aulia
          [age] => 20
       )
)

i want to put them into indexed array like this.

Array = ({"firstName":"Hadi","lastName":"Pratama","age":"22"},{"firstName":"Jefri","lastName":"Ronaldo","age":"21"},
{"firstName":"Rizky","lastName":"Aulia","age":"20"});

help me please.

Rahul
  • 18,271
  • 7
  • 41
  • 60
  • [json_encode](https://www.php.net/manual/en/function.json-encode.php) – Rahul Sep 18 '19 at 08:16
  • 1
    Hi and welcome to SO. Add code (edit your question) with what you have tried, otherwise your question may be closed and/or voted down. – Progrock Sep 18 '19 at 08:31
  • 1
    dupe : https://stackoverflow.com/questions/2122233/converting-php-result-array-to-json – treyBake Sep 18 '19 at 08:39
  • 1
    Possible duplicate of [Converting PHP result array to JSON](https://stackoverflow.com/questions/2122233/converting-php-result-array-to-json) – leonheess Sep 18 '19 at 09:02

3 Answers3

0

Not sure quite what your question is here (please clarify by editing your post).

Your output does not look like a Php array. More like JSON.

When you say remove indexes, could this be from having non sequential 0 counted indexes?

See below:

<?php

$people =
[
    [
        'name'=>'fred',
        'family'=>'flintstones'
    ],
    [
        'name'=>'barney',
        'family' => 'rubble'
    ],
    [
        'name'=> 'wilma',
        'family' => 'flintstones'
    ]
];

var_dump(json_encode($people));
unset($people[1]);
var_dump(json_encode($people));
var_dump(json_encode(array_values($people)));

Output:

string(116) "[{"name":"fred","family":"flintstones"},{"name":"barney","family":"rubble"},{"name":"wilma","family":"flintstones"}]"
string(88) "{"0":{"name":"fred","family":"flintstones"},"2":{"name":"wilma","family":"flintstones"}}"
string(80) "[{"name":"fred","family":"flintstones"},{"name":"wilma","family":"flintstones"}]"

You can use array_values to reindex arrays before encoding to JSON to 'remove' those indexes, and turn into a list of objects.

Progrock
  • 7,373
  • 1
  • 19
  • 25
  • thank you for answering my question. to be honest this array is a json file that has been decoded and then I do some array filters and then the index appears. I don't know how to restore it to the previous format – Septian Arisandi Sep 18 '19 at 09:03
0

json_encode() : Returns the JSON representation of a value

echo json_encode($a);

Working example :- https://3v4l.org/EgOtE

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
0

Question

Your array already has the correct dimensions, just run json_encode($array); over it and you will have the result you're looking for.

If you want to use an associative array, you'll have to be a bit more creative:

$result = [];
foreach($array as $index) {
   $result[$index['firstName']] = $index;
}

var_dump($result);

Result:

Array
(
    [Hadi] => Array
       (
          [firstName] => Hadi
          [lastName] => Pratama
          [age] => 22
       )

    [Jefri] => Array
       (
          [firstName] => Jefri
          [lastName] => Ronaldo
          [age] => 21
       )

    [Rizky] => Array
       (
          [firstName] => Rizky
          [lastName] => Aulia
          [age] => 20
       )
)
Community
  • 1
  • 1
Thrallix
  • 699
  • 5
  • 20