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? -
// compile data actually comes from a database, but an example value would be "title.jp", or "title.0.en"
-
$compile_data = "title.jp";
-
-
/* $instruction_json is an array of data that was pulled previously from a json file. it contains things like:
-
-
"title": {
-
"en" : "Comforts and Conveniences *1*",
-
"jp" : "Title Jp 快適さと便利さ *1*",
-
"kr" : "Title Kr 안락함과 편의 *1*",
-
"ch" : "Title Ch 舒适和方便 *1*"
-
}
-
-
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).
-
-
*/
-
-
// explode instructions
-
$explode_compile_data = explode( ".", $compile_data );
-
-
// access via explosion bits
-
if( count( $explode_compile_data ) == 1 ) { $result_data = $instruction_json[ $explode_compile_data[0] ]; }
-
elseif( count( $explode_compile_data ) == 2 ) { $result_data = $instruction_json[ $explode_compile_data[0] ][ $explode_compile_data[1] ]; }
-
elseif( count( $explode_compile_data ) == 3 ) { $result_data = $instruction_json[ $explode_compile_data[0] ][ $explode_compile_data[1] ][ $explode_compile_data[2] ]; }
-
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] ]; }
-
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] ]; }
-
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] ]; }
-
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] ]; }
-
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] ]; }
-
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] ]; }
-
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] ]; }
-
8 1430
Use a for loop with the count as the termination condition.
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
I have no idea where you get this kind of array, but try this... -
$result_data=$instruction_json;
-
for($i=0;$i<count($explode_comopile_data);$i++)
-
{
-
$result_data=$result_date[$explode_comopile_data[$i]];
-
}
-
echo $result_data;
-
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
just follow the last statement - 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 -
$result_data=$instruction_json;
-
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.
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
Have you tried giving it long legs and blonde hair?
Ba dum cha!
what is your p[oint jking?
anyway got it :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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"...
|
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,...
|
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.
...
|
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...
|
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...
|
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
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: 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...
|
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: 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,...
|
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: 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...
| |