473,407 Members | 2,320 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,407 software developers and data experts.

How can I make this code simpler and sexier?

Hello,
I wrote a little chunk of code that explodes a string and uses the string components to access some array data stored in "$instruction_json". The below code is quite repetetive/redundant and I suspect it could be done with just a couple of lines of code instead, but I can`t figure out how. Any takers?

Expand|Select|Wrap|Line Numbers
  1. // compile data actually comes from a database, but an example value would be "title.jp", or "title.0.en"
  2. $compile_data = "title.jp";
  3.  
  4. /* $instruction_json is an array of data that was pulled previously from a json file.  it contains things like:
  5.  
  6. "title": {
  7. "en" : "Comforts and Conveniences *1*",
  8. "jp" : "Title Jp 快適さと便利さ *1*",
  9. "kr" : "Title Kr 안락함과 편의 *1*",
  10. "ch" : "Title Ch 舒适和方便 *1*"
  11. }
  12.  
  13. So you can see what the above $compile_data var value "title.jp" is trying to access.  The code below works, but it`s long and messy, and doesn`t work past 10 levels deep (or however many times the code is repeated below).
  14.  
  15. */
  16.  
  17. // explode instructions
  18. $explode_compile_data = explode( ".", $compile_data );
  19.  
  20. // access via explosion bits
  21. if( count( $explode_compile_data ) == 1 ) { $result_data = $instruction_json[ $explode_compile_data[0] ]; }
  22. elseif( count( $explode_compile_data ) == 2 ) { $result_data = $instruction_json[ $explode_compile_data[0] ][ $explode_compile_data[1] ]; }
  23. elseif( count( $explode_compile_data ) == 3 ) { $result_data = $instruction_json[ $explode_compile_data[0] ][ $explode_compile_data[1] ][ $explode_compile_data[2] ]; }
  24. elseif( count( $explode_compile_data ) == 4 ) { $result_data = $instruction_json[ $explode_compile_data[0] ][ $explode_compile_data[1] ][ $explode_compile_data[2] ][ $explode_compile_data[3] ]; }
  25. elseif( count( $explode_compile_data ) == 5 ) { $result_data = $instruction_json[ $explode_compile_data[0] ][ $explode_compile_data[1] ][ $explode_compile_data[2] ][ $explode_compile_data[3] ][ $explode_compile_data[4] ]; }
  26. elseif( count( $explode_compile_data ) == 6 ) { $result_data = $instruction_json[ $explode_compile_data[0] ][ $explode_compile_data[1] ][ $explode_compile_data[2] ][ $explode_compile_data[3] ][ $explode_compile_data[4] ][ $explode_compile_data[5] ]; }
  27. elseif( count( $explode_compile_data ) == 7 ) { $result_data = $instruction_json[ $explode_compile_data[0] ][ $explode_compile_data[1] ][ $explode_compile_data[2] ][ $explode_compile_data[3] ][ $explode_compile_data[4] ][ $explode_compile_data[5] ][ $explode_compile_data[6] ]; }
  28. elseif( count( $explode_compile_data ) == 8 ) { $result_data = $instruction_json[ $explode_compile_data[0] ][ $explode_compile_data[1] ][ $explode_compile_data[2] ][ $explode_compile_data[3] ][ $explode_compile_data[4] ][ $explode_compile_data[5] ][ $explode_compile_data[6] ][ $explode_compile_data[7] ]; }
  29. elseif( count( $explode_compile_data ) == 9 ) { $result_data = $instruction_json[ $explode_compile_data[0] ][ $explode_compile_data[1] ][ $explode_compile_data[2] ][ $explode_compile_data[3] ][ $explode_compile_data[4] ][ $explode_compile_data[5] ][ $explode_compile_data[6] ][ $explode_compile_data[7] ][ $explode_compile_data[8] ][ $explode_compile_data[9] ]; }
  30. elseif( count( $explode_compile_data ) == 10 ) { $result_data = $instruction_json[ $explode_compile_data[0] ][ $explode_compile_data[1] ][ $explode_compile_data[2] ][ $explode_compile_data[3] ][ $explode_compile_data[4] ][ $explode_compile_data[5] ][ $explode_compile_data[6] ][ $explode_compile_data[7] ][ $explode_compile_data[8] ][ $explode_compile_data[9] ][ $explode_compile_data[10] ]; }
  31.  
Feb 18 '11 #1
8 1430
Rabbit
12,516 Expert Mod 8TB
Use a for loop with the count as the termination condition.
Feb 18 '11 #2
Hi Rabbit,
Thanks, I understand a for loop with a counter will take care of iterations, but I don`t know how to request a different length of results from the "$instruction_json" variable each time.

If you know, could you give me a short example / code snippet example of this?

Tyler
Feb 18 '11 #3
johny10151981
1,059 1GB
I have no idea where you get this kind of array, but try this...

Expand|Select|Wrap|Line Numbers
  1. $result_data=$instruction_json;
  2. for($i=0;$i<count($explode_comopile_data);$i++)
  3. {
  4.  $result_data=$result_date[$explode_comopile_data[$i]];
  5. }
  6. echo $result_data;
  7.  
Feb 18 '11 #4
Hi Johny,
I think the 'for' statement is moving in the right direction, but I can`t see how the syntax for your middle bit is supposed to work (tested it).

Ie. how can the $result_data notation be setup to accept a random number of array keys.

I tried playing with your code snippet, but I can`t get it to work. It seems to return no result.

Tyler
Feb 18 '11 #5
johny10151981
1,059 1GB
just follow the last statement
Expand|Select|Wrap|Line Numbers
  1. elseif( count( $explode_compile_data ) == 10 ) { $result_data = $instruction_json[ $explode_compile_data[0] ][ $explode_compile_data[1] ][ $explode_compile_data[2] ][ $explode_compile_data[3] ][ $explode_compile_data[4] ][ $explode_compile_data[5] ][ $explode_compile_data[6] ][ $explode_compile_data[7] ][ $explode_compile_data[8] ][ $explode_compile_data[9] ][ $explode_compile_data[10] ]; }
and compare my code
Expand|Select|Wrap|Line Numbers
  1. $result_data=$instruction_json;
  2.  
nothing to say about it

now for loop. it will run through 0 to lenght...

in the first loop in $result_data will get the data of $instruction_json[ $explode_compile_data[0] ]

that is what ever it is holding

thus run through the data only if data structure is correct.
Feb 18 '11 #6
Hi Johny,
That makes a lot of sense, thanks for the explanation.

I tried it again based on that understanding and it works.

Thanks!!

Tyler
Feb 18 '11 #7
JKing
1,206 Expert 1GB
Have you tried giving it long legs and blonde hair?

Ba dum cha!
Feb 18 '11 #8
johny10151981
1,059 1GB
what is your p[oint jking?

anyway got it :)
Feb 18 '11 #9

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

Similar topics

5
by: michaaal | last post by:
I am trying to run this code on a Windows 2003 Server. I have ASP enabled. Any idea why it doesn't work? <% newDB = "new.mdb" newDB = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & newDB...
1
by: Kewlb | last post by:
Hey Guys, My server decided to all of a sudden stop giving me compile/scrip errors and instead now just gives me a blank page with no informatio as to what the problem might be. I have looked...
8
by: sachin bond | last post by:
The following code(in c++) is supposed to divide a file into 'n' different files. suppose i'm having a file called "input.zip". The execution of the following code should divide "input.zip"...
2
by: blongmire | last post by:
.... I know just enough to be dangerous, but the real danger is that I might fall asleep and hit my head on my keyboard waiting for this code to finish executing. Some preliminaries: WinXP Pro,...
7
by: Robert Bachmann | last post by:
Two years I wrote a simple cesar encryption program, it worked but it relied on ASCII. So today I tried to make an portable cesar encryption. Please tell me if the code below is really protable. ...
12
by: Michael Culley | last post by:
I found this code in a project: StringBuilder sb = new StringBuilder(); sb.Append("SELECT * FROM SomeTable") .Append("WHERE SomeField = SomeValue") .Append("ORDER BY etc etc etc") at first I...
9
by: Gregory Petrosyan | last post by:
Hello, it's me again. I am trying to optimise small module for working with polynomials, and I have encountered problem: new, optimised, code, doesn't work in some specific case. Here's the old...
21
by: onkar | last post by:
#include<stdio.h> int i; int i; int main(){ printf("i=%d\n",i); return 0; } Note : I am using gcc-3.4.3 on i386-redhat-linux
14
by: Just_a_fan | last post by:
In VB6, I could easily take the value from a combo box and make a command with it, this: baudrate = cboBaud(listindex). With the new dotted stuff in VB9, I can't seem to do that. Here's an...
1
by: Jeff | last post by:
hi ..Net 2.0 I've created the code below and think I've executed it. After I thought I've executed it I checked in EventLog on the computer (win2k3) and no entry was added. This could mean 2...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.