473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Rearrange Array Elements

code green
1,726 Expert 1GB
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
Expand|Select|Wrap|Line Numbers
  1. Array{part=>G210, qty_req=>1, stock=>40, Customer=>173053, order_no=>10371482, date=>18-Apr-11}
I wish to become
Expand|Select|Wrap|Line Numbers
  1. 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
May 19 '11 #1
5 3618
dlite922
1,584 Expert 1GB
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
May 31 '11 #2
code green
1,726 Expert 1GB
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.
Jun 1 '11 #3
dlite922
1,584 Expert 1GB
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
Jun 2 '11 #4
dlite922
1,584 Expert 1GB
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.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function array_move($which, $where, $array)
  3. {
  4.     $tmp  = array_splice($array, $which, 1);
  5.     array_splice($array, $where, 0, $tmp);
  6.     return $array;
  7. }
  8. ?>
Did that do what you need?



Dan
Jun 2 '11 #5
code green
1,726 Expert 1GB
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
Jun 2 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
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...
3
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...
8
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...
24
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...
6
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
8
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...
14
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...
1
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...
2
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
1
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...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.