Of all the array functions available in PHP, I cannot find a combination that will fulfill a simple task.
That is move the position of an array element from say column 4 to the first column for an associative array.
Array format - Array{part=>G210, qty_req=>1, stock=>40, Customer=>173053, order_no=>10371482, date=>18-Apr-11}
I wish to become - Array{order_no=>10371482, part=>G210, qty_req=>1, stock=>40, Customer=>173053, date=>18-Apr-11}
What is the most efficient way to do this.
This may be one of a few thousand records within a 2d array extrated from a DB and being written to a CSV
5 3618
Try a combination of array_reverse(), array_pop/push() and array_keys().
1. pop the last value
2. pop the last key
3. reverse the array
4. push the value back on to the send
5. reverse the array
All this can be done pretty much on one line of code if you wanted to :)
Dan
I can see that will work Dan, in this case.
I was hoping to produce a generic function that could move any column to the first, leaving the other columns in the same place.
It is purely for presentation in a CSV.
The array functions are more efficient that manipulating arrays by hand, but this requires at least 5 function calls, so I doubt it is much better performance.
I think looping and rearranging each row is simpler.
Well you could design a function that will do exactly that. If I have some free time I'll write it.
Following the rules of programming:
first make it work (function)
then make it elegant (robust)
lastly make it fast. (speed)
I'll do the first, I'm sure you can play around with it and do the other two.
Cheers,
Dan
see "bdjumakov at gmail dot com" comment on http://www.php.net/manual/en/function.array-splice.php
Someone might find this function usefull. It just takes a given element from the array and moves it before given element into the same array. -
<?php
-
function array_move($which, $where, $array)
-
{
-
$tmp = array_splice($array, $which, 1);
-
array_splice($array, $where, 0, $tmp);
-
return $array;
-
}
-
?>
Did that do what you need?
Dan
Thanks again Dan.
I had considered array_splice, but as with a lot of the powerful array functions, they don't work with associative arrays.
paule on the same page has written an associative version, that obviously loops through.
I may build from that
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Herrcho |
last post by:
in K&R Chapter 6.3
it mentions two methods to calculate NKEYS.
and points out the first one which is to terminate the list of
initializers with a null pointer, then loop along keytab until the...
|
by: trevorelbourne |
last post by:
Hi,
I am having trouble accessing the elements of an array using
reflection. This is the code I am having trouble with:
FieldInfo Fields = Obj.GetType().GetFields();
foreach (FieldInfo fi in...
|
by: ljlevend2 |
last post by:
If a property's type is an array and the property is exposed in a
PropertyGrid, then the property can be expanded so that the user can view and
edit the array elements. But, I've noticed that if...
|
by: RyanTaylor |
last post by:
I have a final coming up later this week in my beginning Java class and my
prof has decided to give us possible Javascript code we may have to write.
Problem is, we didn't really cover JS and what...
|
by: Kumar |
last post by:
How should I declare the array elements being half word?
and the array elements of the type byte in C??
Please help
Kumar
|
by: SP |
last post by:
The following code crashes after I add the two nested FOR loops at the
end, I am starting to learn about pointers and would like to understand
what I'm doing wrong. I think the problem is the way...
|
by: dan |
last post by:
I would like to have the preprocessor automatically generate the
number of array elements requested. Each element is zero. The elements
get pasted into a larger array. The other elements may be...
|
by: simply123 |
last post by:
I doing doing C btw...
i have been trying to convert array elements into their respective addresses but Im faced with many problems.
eg. int x (array with 7 elements)
im trying to set the...
|
by: awaisworld13 |
last post by:
I want to write a program which take input and save the poistions of the array elements in another array.
let if i enter an array
A= 4
A=1
A=2
A=3
now the output array will hold
1
2
3
|
by: eran yurman |
last post by:
H,
I need to call an API with the follwoing prototype:
unsigned long CountDevices (BYTE *p_deviceSize,BYTE *p_device );
I have no knowledge of the content of the API function itself but...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |