Remove level of an array

Colin Brazier
Colin Brazier used Ask the Experts™
on
Hi experts,

I need to change an array from this
array to alter
to this:

Array
(
     [KCL P] => Kent county League Premier division
     [IRC 1] => Bill Manklow Inter Regional Challenge Cup 1st round
     [IRC 2] => Bill Manklow Inter Regional Challenge Cup 2nd round
)

Any help appreciated.

Col
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
Please try this -

$1DimensionalArray = array_map('current', $2Dimensionalarray);

Open in new window


or

 $1DimensionalArray  = array_reduce($2Dimensionalarray, 'array_merge', array())

Open in new window

Author

Commented:
After I removed the initial numbers from the array names,
$1DimensionalArray = array_map('current', $2Dimensionalarray);
gives
Array
(
    [0] => KCL P
    [1] => IRC 1
    [2] => IRC 2
)

$1DimensionalArray  = array_reduce($2Dimensionalarray, 'array_merge', array())
gives
Array
(
    [comp_code] => Other
    [comp_desc] => Other
)

Other is the last option in each case.
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
This should do what you want
<?php
$x = array(
	array('comp_code' => 'KCL P', 'comp_desc' => 'Kent county league premier division'),
	array('comp_code' => 'IRC 1', 'comp_desc' => 'Bill Manklow Inter Regional Challenge Cup 1st round'),
	array('comp_code' => 'IRC 2', 'comp_desc' => 'Bill Manklow Inter Regional Challenge Cup 2nd round')
);

$n = array();

foreach($x as $i) {
	$n[$i['comp_code']] = $i['comp_desc'];
}
echo "<pre>" . print_r($n, true) . "</pre>";

Open in new window

Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Author

Commented:
It does - thanks Julian.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Author

Commented:
There might be a few more array questions coming up, I'm really struggling
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Post away and then PM me

Author

Commented:
Will do, I think I've made some progress though.

Cheers

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial