array-search giving unexpected result

I have an array that looks like this:
    [281] => Array
        (
            [postID] => 4412
            [registration] => G-BLCT
            [type] => DR220
            [serial_number] =>
            [owner] =>
            [email] =>
            [web1] =>
            [web2] =>
            [web3] =>
            [based] =>
            [notes] =>
        )

and when I run this code
	reset($aircraft_data);
	$arraykey = array_search('G-BLCT', array_column($aircraft_data,"registration"));
	echo $arraykey , ' ', $aircraft_data[$arraykey]['registration'];

Open in new window

I would expect $arraykey to have a value of 281 but instead it has a value of 8 which corresponds to aircraft registration AKR-006
ClintonKAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
We need to see the full array data - there are several things that could be going on here.

For instance, array_search()
Searches the array for a given value and returns the first corresponding key if successful
Which means if there is another element with the same value before 281 that will be displayed.

1. Please post your full data using var_export() to dump your array before posting it.
2. Please use CODE TAGS for your code to do so
a) Highlight your code (your array)
b) Click the CODE button in the tool bar
CodeTags.jpg
0
ClintonKAuthor Commented:
To make this more manageable I have used an extract of the data to demonstrate the issue.
The code I run to test the value after the array-search is
	reset($aircraft_data);
	$arraykey = array_search('05FM', array_column($aircraft_data,"registration"));
	echo 'arraykey: ',$arraykey , ' Registration: ', $aircraft_data[$arraykey]['registration'],'<br><br>';
	var_export($aircraft_data);

Open in new window


If I populate the array with the first 16 posts I get
arraykey: 1 Registration: 05FM

array ( 0 => array ( 'postID' => 4700, 'registration' => 'G-TEST', 'type' => 'D9', 'serial_number' => '12345', 'owner' => 'Joe Bloggs', 'email' => 'joe@bloggs.com', 'web1' => 'http://www.joebloggsaircraft.com', 'web2' => '', 'web3' => '', 'based' => 'Bradford', 'notes' => '', ), 1 => array ( 'postID' => 4692, 'registration' => '05FM', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Pierre Bourgeois', 'email' => 'Peibo@aol.com', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'South of France', 'notes' => 'Powered by a VW 1600cc', ), 2 => array ( 'postID' => 4691, 'registration' => '19-3924', 'type' => 'D9', 'serial_number' => '19-3924', 'owner' => 'Lyle Passfield', 'email' => 'lyle.passfield@gmail.com', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Australia', 'notes' => 'Jabiru 2.2 engine. Fuselage widened 100mm. 

Wing area increased to 9.5sq m. Profile changed to D18, BRS, wing tanks.

Lives in shipping container on carrier. 

Assembles in about 20 minute', ), 3 => array ( 'postID' => 4690, 'registration' => '28-480', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Australia', 'notes' => '1835cc VW powered', ), 4 => array ( 'postID' => 4689, 'registration' => '28-1823', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Keith Jordan', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Queensland, Australia', 'notes' => 'Powered by a Jabiru, 80 hp, 2.2 litre engine. 

120 knots straight and level!', ), 5 => array ( 'postID' => 4688, 'registration' => '62-AAD', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => ' St. Omer, France', 'notes' => 'Photograph location: St. Omer, France', ), 6 => array ( 'postID' => 4687, 'registration' => '62-ACH', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'St. Omer, France', 'notes' => 'Photograph location: St. Omer, France', ), 7 => array ( 'postID' => 4686, 'registration' => '801-Y', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'St. Omer, France', 'notes' => 'Photograph location: St. Omer, France





', ), 8 => array ( 'postID' => 4685, 'registration' => 'AKR-006', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Chas Kenny', 'email' => 'chassam@slingshot.co.nz', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Clydevale, Southern New Zealand.', 'notes' => 'Has now been flying untouched for nearly 20 years. 

Engine is 1800cc type 4 VW.', ), 9 => array ( 'postID' => 4684, 'registration' => 'CF-RFK', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => 'pearceterry@shaw.ca', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => '1963 built D9 Model PMS1. It is now in a museum in Canada.

It has total time of about 240 hours. Powered by a C65 engine. Quite a performer. Has no tail wheel, just a skid. Other notables: larger rudder for the larger horsepower, wing dihedral is reduced to 7.5 degrees from 15 degrees and fuel tank is behind the pilot for balance of the larger engine.

This picture was taken in 1996 as CF-RFK was being made ready for the museum.

As the kit was from Falconar Aircraft in YEG it is best classified as a Falconar.

', ), 10 => array ( 'postID' => 4683, 'registration' => 'F-PHJS', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'Photograph location: Beaune 1987. 

Photograph supplied by Bob Horrell.', ), 11 => array ( 'postID' => 4682, 'registration' => 'F-PIYP', 'type' => 'D9', 'serial_number' => '398', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'VW powered.

Photograph supplied by Bob Horrell.', ), 12 => array ( 'postID' => 4681, 'registration' => 'F-FMXU', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'Pons, France. 15th August. 1970. 

Photograph by Alain Nouge.', ), 13 => array ( 'postID' => 4680, 'registration' => 'F-POIY', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'RSA Laval, France. 1969. 

Photograph by Alain Nouge', ), 14 => array ( 'postID' => 4679, 'registration' => 'F-PEVD', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Christian Rose', 'email' => 'jodelrose@orange.fr', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => '', ), 15 => array ( 'postID' => 4678, 'registration' => 'G-BDEI', 'type' => 'D9', 'serial_number' => '936', 'owner' => 'Richard Newns', 'email' => '', 'web1' => '', 'web2' => '', 'web3' => '', 'based' => '', 'notes' => 'Photograph location: Popham, UK. 

S/N PFA 936, 1977. VW 1600 powered. 

Photograph by Stuart MacConnacher, 2004.. 

', ), )

Open in new window


If I populate the array with the first 17 posts I get
arraykey: 9 Registration: CF-RFK

array ( 0 => array ( 'postID' => 4700, 'registration' => 'G-TEST', 'type' => 'D9', 'serial_number' => '12345', 'owner' => 'Joe Bloggs', 'email' => 'joe@bloggs.com', 'web1' => 'http://www.joebloggsaircraft.com', 'web2' => '', 'web3' => '', 'based' => 'Bradford', 'notes' => '', ), 9 => array ( 'postID' => 4684, 'registration' => 'CF-RFK', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => 'pearceterry@shaw.ca', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => '1963 built D9 Model PMS1. It is now in a museum in Canada.

It has total time of about 240 hours. Powered by a C65 engine. Quite a performer. Has no tail wheel, just a skid. Other notables: larger rudder for the larger horsepower, wing dihedral is reduced to 7.5 degrees from 15 degrees and fuel tank is behind the pilot for balance of the larger engine.

This picture was taken in 1996 as CF-RFK was being made ready for the museum.

As the kit was from Falconar Aircraft in YEG it is best classified as a Falconar.

', ), 15 => array ( 'postID' => 4678, 'registration' => 'G-BDEI', 'type' => 'D9', 'serial_number' => '936', 'owner' => 'Richard Newns', 'email' => '', 'web1' => '', 'web2' => '', 'web3' => '', 'based' => '', 'notes' => 'Photograph location: Popham, UK. 

S/N PFA 936, 1977. VW 1600 powered. 

Photograph by Stuart MacConnacher, 2004.. 

', ), 14 => array ( 'postID' => 4679, 'registration' => 'F-PEVD', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Christian Rose', 'email' => 'jodelrose@orange.fr', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => '', ), 13 => array ( 'postID' => 4680, 'registration' => 'F-POIY', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'RSA Laval, France. 1969. 

Photograph by Alain Nouge', ), 12 => array ( 'postID' => 4681, 'registration' => 'F-FMXU', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'Pons, France. 15th August. 1970. 

Photograph by Alain Nouge.', ), 11 => array ( 'postID' => 4682, 'registration' => 'F-PIYP', 'type' => 'D9', 'serial_number' => '398', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'VW powered.

Photograph supplied by Bob Horrell.', ), 10 => array ( 'postID' => 4683, 'registration' => 'F-PHJS', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'Photograph location: Beaune 1987. 

Photograph supplied by Bob Horrell.', ), 8 => array ( 'postID' => 4685, 'registration' => 'AKR-006', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Chas Kenny', 'email' => 'chassam@slingshot.co.nz', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Clydevale, Southern New Zealand.', 'notes' => 'Has now been flying untouched for nearly 20 years. 

Engine is 1800cc type 4 VW.', ), 1 => array ( 'postID' => 4692, 'registration' => '05FM', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Pierre Bourgeois', 'email' => 'Peibo@aol.com', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'South of France', 'notes' => 'Powered by a VW 1600cc', ), 7 => array ( 'postID' => 4686, 'registration' => '801-Y', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'St. Omer, France', 'notes' => 'Photograph location: St. Omer, France





', ), 6 => array ( 'postID' => 4687, 'registration' => '62-ACH', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'St. Omer, France', 'notes' => 'Photograph location: St. Omer, France', ), 5 => array ( 'postID' => 4688, 'registration' => '62-AAD', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => ' St. Omer, France', 'notes' => 'Photograph location: St. Omer, France', ), 4 => array ( 'postID' => 4689, 'registration' => '28-1823', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Keith Jordan', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Queensland, Australia', 'notes' => 'Powered by a Jabiru, 80 hp, 2.2 litre engine. 

120 knots straight and level!', ), 3 => array ( 'postID' => 4690, 'registration' => '28-480', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Australia', 'notes' => '1835cc VW powered', ), 2 => array ( 'postID' => 4691, 'registration' => '19-3924', 'type' => 'D9', 'serial_number' => '19-3924', 'owner' => 'Lyle Passfield', 'email' => 'lyle.passfield@gmail.com', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Australia', 'notes' => 'Jabiru 2.2 engine. Fuselage widened 100mm. 

Wing area increased to 9.5sq m. Profile changed to D18, BRS, wing tanks.

Lives in shipping container on carrier. 

Assembles in about 20 minute', ), 16 => array ( 'postID' => 4677, 'registration' => 'G-KDIX', 'type' => 'D9', 'serial_number' => '', 'owner' => 'D. J. Wells', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'Photograph location: Fenland, UK. 

Construction no: PFA / 54 - 10293', ), )

Open in new window

Note (line 1 of the output) uses the same array-search criteria but different results are returned when the array has 16 and 17 posts
0
Julian HansenCommented:
Look at what you get when you sort your arrays
http://www.marcorpsa.com/ee/t2970.php

Look at the data you posted - the arrays are not sorted - so you are getting different results.

Full listing of the above here
t2970.php
t2970-15.php
t2970-16.php
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Jim RiddlesPrepress/OMS SpecialistCommented:
@ClintonK, as I mentioned in a previous question, you will need to take a different approach to find the key with an array built like that.
Now, you will need to loop through each entry in your array to find the value you are looking for and access the key.  Try the following abbreviated code:
$aircraft_data= array(
    "4750" => array(
        "Sequence_number" => "1",
        "Item_Desc" => "D9"
        ),
    "4751" => array(
        "Sequence_number" => "2",
        "Item_Desc" => "D11"
        ),
    "4752" => array(
        "Sequence_number" => "3",
        "Item_Desc" => "D18"
        ),
    "4753" => array(
        "Sequence_number" => "4",
        "Item_Desc" => "D19"
        )
);

foreach ($aircraft_data as $key => $aircraft) {
    if (array_search("D18", $aircraft)) {
        echo $key;
    }
}

Open in new window

I have built a simple representation of your data.  We need to loop through each element in the array, and search for the value within that array element.  Then we output the key.  Let me know if this works for you.
0
ClintonKAuthor Commented:
Thanks both.

So just to clarify please.
This is how my array is built
$aircraft_data[] = array(
	'postID' => $postID,
	'registration' => get_the_title($postID),
	'type' => get_field('type', $postID),
	'serial_number' => get_field('serial_number',$postID),
	'owner' => get_field('owner', $postID),
	'email' => get_field('email', $postID),
	'web1' => get_field('web1', $postID),
	'web2' => get_field('web2', $postID),
	'web3' => get_field('web3', $postID),
	'based' => get_field('based', $postID),
	'notes' => get_field('notes', $postID)
);	

Open in new window

and using Jim R's approach I have successfully searched the array using this code ($var holds the aircraft registration)
foreach ($aircraft_data as $key => $info):
	if (array_search($var, $info)): break; endif;
endforeach;

Open in new window


I have also re-introduced the sort rountine
uasort($aircraft_data, function($a, $b) {
if ($a['registration'] == $b['registration']) return 0;
	return ($a['registration'] < $b['registration'])?-1:1;
});

Open in new window

Would this mean that
$arraykey = array_search('05FM', array_column($aircraft_data,"registration"));

Open in new window

would now produce the correct results or is it that I can't use this approach given the build of my array?
0
Julian HansenCommented:
Just sort your array.
0
ClintonKAuthor Commented:
I load the array like this
function getAircraftDataFromDB()
{
	/*	Get aircraft data */	
	static $aircraft_data = false;
	
	if (!$aircraft_data):
		$aircraft_posts = get_posts(
		array(
			'post_type'      => 'aircraft',
			'post_status'    => 'publish',
			'posts_per_page' => - 1,
			'fields'         => 'ids',
		) );
		foreach( $aircraft_posts as $postID ):
			$aircraft_data[] = array(
					'postID' => $postID,
					'registration' => get_the_title($postID),
					'type' => get_field('type', $postID),
					'serial_number' => get_field('serial_number',$postID),
					'owner' => get_field('owner', $postID),
					'email' => get_field('email', $postID),
					'web1' => get_field('web1', $postID),
					'web2' => get_field('web2', $postID),
					'web3' => get_field('web3', $postID),
					'based' => get_field('based', $postID),
					'notes' => get_field('notes', $postID)
				);		
		endforeach;
	endif;

	uasort($aircraft_data, function($a, $b) {
	if ($a['registration'] == $b['registration']) return 0;
		return ($a['registration'] < $b['registration'])?-1:1;
	});

	return $aircraft_data;
}

Open in new window

and read it with this
	$aircraft_data = getAircraftDataFromDB();
	$arraykey = array_search('05FM', array_column($aircraft_data,"registration"));
	echo 'arraykey: ',$arraykey , ' Registration: ', $aircraft_data[$arraykey]['registration'],'<br>';

Open in new window


and get arraykey: 0 Registration: G-TEST as output
Is my sort wrong?
0
Julian HansenCommented:
You are sorting on registration - which is scrambling the array keys - when you get the columns they come out in the array key order which is why our code is not working.

Why don't you do this (assuming reg number is unique)
foreach( $aircraft_posts as $postID ):
			$aircraft_data[get_the_title($postID)] = array(
					'postID' => $postID,
					'registration' => get_the_title($postID),
					'type' => get_field('type', $postID),
					'serial_number' => get_field('serial_number',$postID),
					'owner' => get_field('owner', $postID),
					'email' => get_field('email', $postID),
					'web1' => get_field('web1', $postID),
					'web2' => get_field('web2', $postID),
					'web3' => get_field('web3', $postID),
					'based' => get_field('based', $postID),
					'notes' => get_field('notes', $postID)

Open in new window

In other words use the title (registration) as the array key - that way you don't have to do any of that other code trying to find the record you want - just use it as an array index and it takes you right there - something like this
$acdata = isset($aircraft_data['05FM']) ? $aircraft_data['05FM'] : false;

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ClintonKAuthor Commented:
Many thanks both. For the array where I can assign the unique registration to the key (aircraft_data), Julian's solution is ideal. Where my other array comprises of media attachments I can't use the registration as key because there are duplicates. In this case I will use Jim's solution.
Both great answers to my problem and it's moved me on a great deal in the development of my website and my understanding of array manipulation.
Cheers
0
Julian HansenCommented:
At the risk of putting this question on life support - why are you doing this in your code (function getAircraftDataFromDB())
uasort($aircraft_data, function($a, $b) {
	if ($a['registration'] == $b['registration']) return 0;
		return ($a['registration'] < $b['registration'])?-1:1;
	});

Open in new window

Why not just get the posts and order them when you get them
$aircraft_posts = get_posts(array(
  'post_type'      => 'aircraft',
  'post_status'    => 'publish',
  'posts_per_page' => - 1,
  'fields'         => 'ids',
  'orderby'        => 'title',
  'order'          => 'ASC'
));

Open in new window

This way the posts come out already sorted using a DB sort which is far better and array keys will be in the right order which mean your original code for searching for the registration should work as expected.
0
ClintonKAuthor Commented:
Thanks. I hadn't looked into the options of get_posts so orderby will come in handy. There are limited options for orderby aren't there? As far as I can see I can't orderby an ACF field can I?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.