phpLearner
phpLearner

Reputation: 197

PhP multidimensional array to individual array

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.

Upvotes: 0

Views: 71

Answers (3)

AbraCadaver
AbraCadaver

Reputation: 79024

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

Upvotes: 1

Mark Baker
Mark Baker

Reputation: 212522

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

Upvotes: 2

Manolis Agkopian
Manolis Agkopian

Reputation: 1094

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

Upvotes: -1

Related Questions