0

How can I add values of the three arrays into one?

When I do like this

$response["data"] =  $data + $user_poi + $user_mileage;

it does add the arrays into one but not the complete array. Consider the following:

$data = {data 1, data 2, data 3, data 4, data 5, data 6, data, 7 data 8}
$user_poi = {poi 1}
$user_mileage {mileage 1, mileage 2, mileage 3, mileage 4}

If I write: $response["data"] = $data + $user_poi + $user_mileage;

It gives me: {data 1, data 2, data 3, data 4, data 5, data 6, data, 7 data 8}

If I write: $response["data"] = $user_poi + $user_mileage + $data;

It gives me: {poi 1, mileage 2, mileage 3, mileage 4, data 5, data 6, data, 7 data 8}

What I want is to write: $response["data"] = $user_poi + $user_mileage + $data;

And get the result: {poi 1, mileage 1, mileage 2, mileage 3, mileage 4, data 1, data 2, data 3, data 4, data 5, data 6, data, 7 data 8}

Jumana Alhaddad
  • 285
  • 2
  • 6
  • 17
  • Check this: http://stackoverflow.com/questions/2140090/operator-for-array-in-php – domsson Apr 13 '17 at 22:20
  • Also, you are basically asking two separate questions as one. I recommend you to split them into two actual questions (remove the second part from here, post it as a new question). – domsson Apr 13 '17 at 22:40
  • "Consider the following" is not valid PHP code. – miken32 Apr 13 '17 at 23:08

1 Answers1

1

I'm going to answer the first part of your question. For the second part, I strongly suggest post that as a new, separate question and remove it from here.

How can I add values of the three arrays into one?

Try array_merge():

$data = ["data 1", "data 2", "data 3"];
$poi = ["poi 1", "poi 1", "poi 3"];
$mileage = ["mileage 1", "mileage 2", "mileage 3"];

$response = array_merge($data, $poi, $mileage);

After this, $response will look like this:

array(
  "data 1",
  "data 2",
  "data 3",
  "poi 1",
  "poi 2",
  "poi 3",
  "mileage 1",
  "mileage 2",
  "mileage 3"
)

Why did + not work though?

Check the question + operator for array in PHP?.

To summarize the well written answer from there, using +, keys that exist in both arrays will be filled with the values from the array on the left-hand side, that's why you never see all of the expected elements in your output.

Update: What if one of the arrays is null?

Check the question merge_array returns null if one or more of arrays is empty?

Basically, if your input arrays could be null, you can explicitly cast them to arrays to make sure null values will become empty arrays:

$response = array_merge((array)$data, (array)$poi, (array)$mileage);
Community
  • 1
  • 1
domsson
  • 4,553
  • 2
  • 22
  • 40
  • This worked good. The issue now, if one of these arrays is null it does not return anything at all!!! – Jumana Alhaddad Apr 13 '17 at 22:59
  • Jumana, you should turn on all warnings. Anyway, check this answer for a solution to the `null` problem: http://stackoverflow.com/a/19712372/3316645 (basically, just cast all parameters for `array_merge` to array, this way `null` values will become empty arrays) – domsson Apr 13 '17 at 23:41