Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a multidimensional array which I want to convert to individual arrays.

Original array is

$hos_pabsl = array(
    0 =>
    array(
        'tile_id' => '1',
        'tile_type' => '4',
        'title' => 'Introduction',
        'topicNum' => '1',
        'topicTitle' => 'Introduction',
        'subNum' => NULL,
    ),
    1 =>
    array(
        'tile_id' => '2',
        'tile_type' => '9',
        'title' => 'Beer',
        'topicNum' => '2',
        'topicTitle' => 'Beer',
        'subNum' => NULL,
    ),
    2 =>
    array(
        'tile_id' => '3',
        'tile_type' => '4',
        'title' => 'Methods of Brewing',
        'topicNum' => '2',
        'topicTitle' => 'Beer',
        'subNum' => NULL,
    ),
    3 =>
    array(
        'tile_id' => '4',
        'tile_type' => '11',
        'title' => 'Beer Styles',
        'topicNum' => '2',
        'topicTitle' => 'Beer',
        'subNum' => '',
    ),
);

I want to convert this array into individual arrays named 'tile_id' , 'tile_type' , ....

Currently I am doing it the following way !

$tile_id = [];
$tile_type = [];
$title = [];
$topicNum = [];
$topicTitle= [];
$subNum = [];

foreach($hos_pabsl as $val){
    array_push($tile_id, $val['tile_id']);
    array_push($tile_type, $val['tile_type']);
    array_push($title, $val['title']);
    array_push($topicNum, $val['topicNum']);
    array_push($topicTitle, $val['topicTitle']);
    array_push($subNum, $val['subNum']);
}

Problem 1: IS this the most efficient way (in terms of speed) to do this operation?

Problem 2:
The $hos_pabsl array's index (or keys) are always going to be sequential. However, my problem is that for second array (at level 2 OR $hos_pabsl[0]) the index (or keys) might increase or decrease.
E.g. all arrays in might have only 2 items 'tile_id' & 'title'. OR might have one extra item 'description'. So how can I make the above operation dynamic ?

To Solve problem 2, I have thought of using array_keys to extract names first $names = array_keys($hos_pabsl[0]) then using those names as array names like ${$names[0]} =[]. Again I don't think this is the right/efficient way to do this.



Any guidance on this would be really appreciated.

share|improve this question
1  
Problem1: Micro-optimalization has little to no effect here. Go with the flow. –  OptimusCrime May 1 '14 at 23:32
    
What do you want the result to look like and why? –  AbraCadaver May 1 '14 at 23:33
    
I want to result to have individual arrays. E.g. $tile_id=Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4); and $title = Array ( [0] => Introduction [1] => Beer [2] => Methods of Brewing [3] => Beer Styles); and $topicTitle = [0] => Introduction [1] => Beer [2] => Beer [3] => Beer); and so on and so forth.. I am getting the original array from DB and I need individual array for separate unrelated processing in different functions. –  phpLearner May 1 '14 at 23:36

3 Answers 3

up vote 2 down vote accepted

If you're running PHP 5.5, then you can use array_column()

$tile_id = array_column($hos_pabsl, 'tile_id');
$tile_type = array_column($hos_pabsl, 'tile_type');
... etc

for versions of PHP earlier than 5.5, you can use array_map()

$tile_id = array_map(
    function ($value) { return $value['tile_id']; }, $hos_pabsl
);
$tile_type = array_map(
    function ($value) { return $value['tile_type']; }, $hos_pabsl
);
... etc
share|improve this answer
    
Don't know if this changed in PHP 5.5 but: Anonymous functions are created using create_function(String args, String body), not by simply embedding them. –  ccKep May 1 '14 at 23:47
    
Anonymous functions (closures) can be embedded as I've shown in my example since PHP version 5.3: you don't have to use create_function –  Mark Baker May 1 '14 at 23:53
    
Hi MarkBaker and @AbraCadaver I have php version 5.4.4. So I tried to mix both of your methods. foreach(array_keys(reset($hos_phsg)) as $key) { $$key = array_map( function ($value) { return $value[$key]; }, $hos_phsg ); } The error I am getting is that $key is undefined in $value[$key]. How can I pass this $key value for it to be dynamic ? –  phpLearner May 2 '14 at 0:12
    
foreach(array_keys(reset($hos_phsg)) as $key) { $$key = array_map( function ($value) use ($key) { return $value[$key]; }, $hos_phsg ); } –  Mark Baker May 2 '14 at 9:49

To go with Mark Baker's answer since I was already typing it:

foreach(array_keys(reset($hos_pabsl)) as $key) {
    $$key = array_column($hos_pabsl, $key);
}
share|improve this answer
    
Sorry I can accept only one answer. Your answer also helped. I am trying to use combination your answer and @MarkBaker 's answer. I tried to mix both of your methods. foreach(array_keys(reset($hos_phsg)) as $key) { $$key = array_map( function ($value) { return $value[$key]; }, $hos_phsg ); } The error I am getting is that $key is undefined in $value[$key]. How can I pass this $key value for it to be dynamic ? –  phpLearner May 2 '14 at 0:14
    
Thanks for reply. I had tried that assuming $key would be available in closure function because of variable scope. But it throws Missing argument 2 for {closure}() in error. –  phpLearner May 2 '14 at 0:22
1  
function ($value) use ($key) { return $value[$key]; }, –  Mark Baker May 2 '14 at 7:04

In terms of performance if the array is huge using a for instead of a foreach it will be faster.

$tile_id = array();
$tile_type = array();
$title = array();
$topicNum = array();
$topicTitle = array();
$subNum = array();

$hos_pabsl_sz = count($hos_pabsl);
for ($i = 0; $i < $hos_pabsl_sz; ++$i ) {
    $tile_id[$i] = $hos_pabsl[$i]['tile_id'];
    $tile_type[$i] = $hos_pabsl[$i]['tile_type'];
    $title[$i] = $hos_pabsl[$i]['title'];
    $topicNum[$i] = $hos_pabsl[$i]['topicNum'];
    $topicTitle[$i] = $hos_pabsl[$i]['topicTitle'];
    $subNum[$i] = $hos_pabsl[$i]['subNum'];
}
share|improve this answer
    
Isn't this exactly the same as what I am doing already? –  phpLearner May 1 '14 at 23:38
    
@user2834439 Yes, but you said you wanted something faster. foreach is slower than for phpbench.com. In most cases it doesn't matter but it still slower. –  Manolis Agkopian May 1 '14 at 23:40
    
Note that the phpbench.com benchmark is using foreach($array as $key => $val) rather than simply foreach($array as $val)... including the key can make a significant performance overhead when using foreach; but normally foreach will be faster to iterate an array than for –  Mark Baker May 1 '14 at 23:49
    
@MarkBaker I agree but it is allready about 3500% slower, I don't thing that including the key or not will have a huge difference. Anyway, the only way to be sure it to benchmark it by yourself. –  Manolis Agkopian May 1 '14 at 23:55
    
Check the Read Loop: foreach() vs. for() vs. while(list() = each()) figures, I think you've just found the first reference on the phpbench.com site (Modify Loop: foreach() vs. for() vs. while(list() = each())), not the appropriate one for this case –  Mark Baker May 1 '14 at 23:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.