1

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.

4
  • foreach loop, build a new array Commented Feb 5, 2016 at 22:14
  • 1
    Are the there always 5 items in each subarray? Commented Feb 5, 2016 at 22:17
  • Thank you. I thought maybe there's a way without foreach loop. The items is 5 or 6. Commented Feb 5, 2016 at 22:19
  • sigh, the problem with a foreach loop is what? Commented Feb 5, 2016 at 22:21

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

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.