1

In PHP how do I add the values of [2] into the main body of the array as opposed to having it as a sub array? I've tried array_push, the + concatenator and array_merge but none work.

Array
(
    [0] => 42299.37181713
    [1] => Yes
    [name] => Bob Smith
    [country] => United Kingdom
    [2] => Array
    (
        [0] => Name 1
        [1] => Name 2
        [2] => Name 3
        [3] => 
    )

What I am looking for / need is a way to get to the following (and I'm sure there's a simply/correct PHP way of doing it!):

Array
(
    [0] => 42299.37181713
    [1] => Yes
    [name] => Bob Smith
    [country] => United Kingdom
    [2] => Name 1
    [3] => Name 2
    [4] => Name 3
    [5] => 

Thanks for your answers - I have now posted my solution as one of the answers :)

6
  • how did you get that array in the first place ? Commented Nov 19, 2015 at 10:59
  • They are two separate arrays so the first part is what I'm currently getting if I use say array_merge. Commented Nov 19, 2015 at 11:01
  • array_merge() should do exactly what you want, my guess is you used it wrong somehow to end up with #1 instead of #2. Commented Nov 19, 2015 at 11:05
  • The reason was because I was in a foreach and I was merging an array with values from another array. The solution was to use array_merge but on the parent[$k] rather than on $vs. Does that warrant an answer below? Commented Nov 19, 2015 at 11:06
  • @Ukuser32 can you please upload your code ? Commented Nov 19, 2015 at 11:09

5 Answers 5

3
<?php
$myArray = array(
    0 => 42299.371,
    1 => "Yes",
    "name" => "Bob Smith",
    "country" => "United Kingdom",
    2 => array(
        0 => "Name 1",
        1 => "Name 2",
        2 => "Name 3"
        )
    );

$tmp = $myArray[2];
unset($myArray[2]);

$myArray = array_merge($myArray, $tmp);

echo '<pre>';
print_r($myArray);
echo '</pre>';
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry $myArray is the output not what I have to work from.
Any chance of the source data then?
1

The issue here was that I was doing

foreach ($data as $row){

I was then trying to do array_merge on $row or use + on $row but these are values not an array in themselves even though print_r says its an Array.

So the solution is to do the following:

foreach ($data as $k=>$row){
     $new_row = array_merge($data[$k],$sub_array);

That way you are actually merging an array not a value set from an array.

Comments

1

if you get arrays separately you can use array_merge :

$a = ['whashington', 'NewYork', 98];
$b = ['fruits', 'clothes', 4];
$a = array_merge($a, $b);

array (size=6)
  0 => string 'whashington' (length=11)
  1 => string 'NewYork' (length=7)
  2 => int 98
  3 => string 'fruits' (length=6)
  4 => string 'clothes' (length=7)
  5 => int 4

Comments

1

You can do something like this to get the desired result,

<?php

    $multi_array = array
    (
        0 => 42299.37181713,
        1 => "Yes",
        "name" => "Bob Smith",
        "country" => "United Kingdom",
        2 => array
        (
            0 => "Name 1",
            1 => "Name 2",
            2 => "Name 3",
        )
    );

    $tmp = $multi_array[2];
    unset($multi_array[2]);
    $i = 2;
    foreach($tmp as $value){
        $multi_array[$i] = $value;
        ++$i;
    }

    print_r($multi_array);

?>

Comments

1

I understand that you are interested in merging a sub-array as elements of the parent array.

You might achieve it doing:

$tmp_arr = array();
foreach ($data as $k=>$v) {
  if (is_array($v)) {
    $tmp_arr = array_merge($tmp_arr, $v);
  } else {
    $tmp_arr[] = $v;
  }
}

Comments

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.