0

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.

3
  • 1
    Problem1: Micro-optimalization has little to no effect here. Go with the flow. Commented May 1, 2014 at 23:32
  • What do you want the result to look like and why? Commented May 1, 2014 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. Commented May 1, 2014 at 23:36

3 Answers 3

2

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

4 Comments

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.
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
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 ?
foreach(array_keys(reset($hos_phsg)) as $key) { $$key = array_map( function ($value) use ($key) { return $value[$key]; }, $hos_phsg ); }
1

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

3 Comments

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 ?
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.
function ($value) use ($key) { return $value[$key]; },
-1

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

4 Comments

@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.
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
@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.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.