Foreach multiple arrays into one

Can’t seem to figure out how to do it.

I have 5 arrays with similar structure:

Array
(
    [13440] => 44 años
    [13477] => 39 años
)

All arrays have same key but different values!

Out of those 5 I need to create one:

$AjaxData[] = array(
"UserId"=>"",
"UserName"=>"",
"ProfileUrl"=> "",  
"AvatarUrl"=>"",
"Age"=>"",
"AboutMe"=>""
);

Please teach me how it is done.
Thanks

I assume it’s got to be foreach inside foreach.

i don’t see any relation between these two sets of data.

Please read my post. I have 5 similar arrays and I need to foreach them into one. All arrays have same key which corresponds to userId.

i don’t see what that would have to do with your example in $AjaxData and how any of the first data would covert to this.

$AjaxData = array();

so that’s your solution?

I think that I understand what you want.

I assume that you have 5 arrays with the same key, but different names.

So your array wouldn’t be

[13440]=>44 years;

I assume that your array would be something like

arrayName[13440]=>44 years;

It has been a long time since I’ve done this and I forgotten quite a bit of my javascript. But I believe that the answer you’re looking for would go something like this or close to this


$AjaxData[] = array(
"UserId"=>arrayName1[13440],
"UserName"=>arrayName2[13440],
"ProfileUrl"=>arrayName3[13440] ,  
"AvatarUrl"=>arrayName4[13440],
"Age"=>arrayName5[13440],
"AboutMe"=>arrayName6[13440]
);

If it is the very first property of each array what you want, I think this might work.

Take it with a pintch of salt though. It’s been a few years since I used JavaScript. If this doesn’t work, I will need to refresh my knowledge of associative arrays :smiley:

Could you possibly show an example of how you expect the finished array to look, with some sample data? Say, two of the original arrays, and how those two would look in the combined array.

I agree that a more complete question will help to find the right answer

The PHP manual has a couple of functions which may be useful, complete with examples:

https://www.php.net/manual/en/function.json-decode.php

https://www.php.net/manual/en/function.json-encode.php

There are also some functions dealing with Json errors.