0

I am getting form data like this from form post

Array
(
    [instagram] => dsaadsasd
    [facebook] => 12esda
)

I have to arrange data like these but i couldnt solve

 Array
([0]=>Array(
    [name] => instagram
    [data] => dsaadsasd)

 [1]=>Array(
    [name] => facebook
    [data] => 12esda)
2
  • 1
    what you are suggesting is not valid JSON. Commented Dec 29, 2019 at 23:22
  • @DirkJ.Faber Pls check again Commented Dec 30, 2019 at 0:10

1 Answer 1

1

In PHP your array looks like this:

$array = ['instagram' => 'dsaadsasd', 'facebook' => '12esda'];

Now to change this data into your desired array you can use a forEach loop, like so:

$newArray = [];
forEach($array as $key => $value){
$newArray[] = ["name" => $key, "data" => $value];
}

What this does is it creates an new array that for each of the items in your original array creates another array in your new array. Here's also a live example.

Sign up to request clarification or add additional context in comments.

2 Comments

before i can list data 'foreach($info as $key => $val){' but now how can list only 'name'?
@Can1, I'm sorry but I do not understand what you mean. If you want to access any of the data "below", you can use $newArray[0]["data"] (the 0 is for the first item of the array)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.