Ukuser32
Ukuser32

Reputation: 2189

Merging array values into main array

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 :)

Upvotes: 1

Views: 46

Answers (5)

guyaloni
guyaloni

Reputation: 5862

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;
  }
}

Upvotes: 1

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

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);

?>

Upvotes: 1

user5580901
user5580901

Reputation: 41

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

Upvotes: 1

Ukuser32
Ukuser32

Reputation: 2189

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.

Upvotes: 1

SheppardDigital
SheppardDigital

Reputation: 3255

<?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>';

Upvotes: 3

Related Questions