Panagiotis Koursaris
Panagiotis Koursaris

Reputation: 4023

How to add keys to an existing multiple array

I have the bellow multiple array:

 array:4 [▼
      0 => array:5 [▼
        0 => "some content"
        1 => "some content"
        2 => "some content"
        3 => "some content"
        4 => "some content"
      ]
      1 => array:5 [▼
        0 => "some content"
        1 => "some content"
        2 => "some content"
        3 => "some content"
        4 => "some content"
      ]
      2 => array:6 [▼
        0 => "some content"
        1 => "some content"
        2 => "some content"
        3 => "some content"
        4 => "some content"
        5 => "some content"
      ]
      3 => array:5 [▼
        0 => "some content"
        1 => "some content"
        2 => "some content"
        3 => "some content"
        4 => "some content"
      ]
    ]

How can I add keys to this array so it looks like:

array:4 [▼
  0 => array:5 [▼
    title => "some content"
    subtitle => "some content"
    somekey => "some content"
    somekey2 => "some content"
    somekey3 => "some content"
  ]
  1 => array:5 [▼
    title => "some content"
    subtitle => "some content"
    somekey => "some content"
    somekey2 => "some content"
    somekey3 => "some content"
  ]
  2 => array:6 [▼
    title => "some content"
    subtitle => "some content"
    somekey => "some content"
    somekey2 => "some content"
    somekey3 => "some content"
    somekey4 => "some content"
  ]
  3 => array:5 [▼
    title => "some content"
    subtitle => "some content"
    somekey => "some content"
    somekey2 => "some content"
    somekey3 => "some content"
  ]
]

Is there any way to do it without for loop?

There isn't any way to add keys before because I don't know how many arrays will be create.

Thank you so much for your attention and participation.

Upvotes: 1

Views: 62

Answers (3)

showdev
showdev

Reputation: 29188

I suggest a combination of array_walk and array_combine to iterate through the array and combine a preset list of keys with the values of each nested array.

array_walk — Apply a user supplied function to every member of an array
array_combine — Creates an array by using one array for keys and another for its values

EXAMPLE CODE

$array=array(
  array('some content1','some content','some content','some content','some content'),
  array('some content2','some content','some content','some content','some content'),
  array('some content3','some content','some content','some content','some content'),
  array('some content4','some content','some content','some content','some content')
);    

$keys=array('title','subtitle','somekey','somekey2','somekey3');

function combine(&$v,$i,$keys) {
  $v=array_combine($keys,$v);
}

array_walk($array,'combine',$keys);

print_r($array);

OUTPUT

Array    (
    [0] => Array
        (
            [title] => some content1
            [subtitle] => some content
            [somekey] => some content
            [somekey2] => some content
            [somekey3] => some content
        )

    [1] => Array
        (
            [title] => some content2
            [subtitle] => some content
            [somekey] => some content
            [somekey2] => some content
            [somekey3] => some content
        )

    [2] => Array
        (
            [title] => some content3
            [subtitle] => some content
            [somekey] => some content
            [somekey2] => some content
            [somekey3] => some content
        )

    [3] => Array
        (
            [title] => some content4
            [subtitle] => some content
            [somekey] => some content
            [somekey2] => some content
            [somekey3] => some content
        )   
)

Here's a working example.

Note that this only works if your nested arrays all contain the same number of elements and that your array of key values contains the same number of elements (in this case five). Otherwise, you're likely to get a PHP error about "Both parameters should have an equal number of elements".


If you prefer, this can be achieved in one line by using an anonymous function:

array_walk($array,function(&$v,$i,$keys){$v=array_combine($keys,$v);},$keys);

Upvotes: 1

Ravinder Reddy
Ravinder Reddy

Reputation: 3879

Use array_combine function within foreach loop. see below example

$keys_array = array("title","subtitle","somekey","somekey2","somekey3");
$array=array(
  array('some content','some content','some content','some content','some content'),
  array('some content','some content','some content','some content','some content'),
  array('some content','some content','some content','some content','some content'),
  array('some content','some content','some content','some content','some content'),
  array('some content','some content','some content','some content','some content')
);
$new_array =array();
foreach($array as $key=>$value){
     $new_array[] = array_combine($keys_array, $value);
}
print_r($new_array);

Upvotes: 0

LuckyLue
LuckyLue

Reputation: 158

Not enough information.

function convertKeys($table) {
    foreach($table as $key => $value) {
        if (0 === $key) {
            $table['title'] = $value;
            unset($table[$key]);
        } else if (1 === $key) {
            $table['subtitle'] = $value;
            unset($table[$key]);
        } else //another mapping
    }
}

foreach($table as $val) {
    //$val is arrray
    convertKeys($val);
}

Upvotes: 0

Related Questions