How do I post this multi array?

0xym0r0n

Well-known member
Pardon me, I'm a noob!

I've been trying to convert an array to something that's readable for a while now.

The array looks like this and is from a JSON data scrap

Code:
Array(2) {
      [Item1]=>int(15)
      [List1]=>
        Array(2) {
          [0]=>
            Array(2) {
              [Name]=>SomeName
              [Age]=>24
                    }
               
          [1]=>
            Array(2) {
              [Name]=>SomeName1
              [Age]=>25
                    }
                  }

List1 arrays is always changes it can be 'infinite'

I'm trying to get this array to post to a database or just display as is. I've tried array_keys, recurvsivewrite, [obj][element][value]

I always turn up with nulled data or I'm just printing out new arrays but not specific element values.

I want to be able to create a List that's presentable on a site that will look like this example

Participants
1. Name: Andrew, Age: 24
2. Name: Beth, Age: 25


I am using PHP.net and stackoverflow as references but I'm not finding much help since I'm not really sure what to call the above other than a multi dimensional array. I'm not asking for the straight up answer. I'm fine reading! I'd just like to skip the the area that fits my situation (for example refer to me array_keys, json_encode, etc)
 
Please explain what you mean by "List1 arrays is always changes it can be 'infinite'"?

There is no such thing as an infinite array..

That said, you should be able to simply json_encode or serialize the array to post it to your database. But I may be miss-understanding your question here. If you can elaborate / rephrase that'd be very helpful.
 
Please explain what you mean by "List1 arrays is always changes it can be 'infinite'"?

Yeah for lack of a better word I used infinite. I apologize

What I mean is - My current code is scraping JSON data and posting it as an array on one page. Because the JSON data is from a user 'database', it will is continually updated with user stats. I may just misunderstand array in whole. I might have to re-read the entire arrays area.

SO

script1 uses and works (posts array successfully)
Code:
$url=file_get_contents('URL of JSON data');
$urld=decode('$url', true);
var_dump('$urld');


script2
is used to read the array(sript1) and either post it to mySQL database under the specific value I set or just post a specific value (for example names)< - where I'm having trouble.

All that I've been able to do thus far is post one value(name) from List1 (but there are at least 20 names).
 
I'm sorry you're still not being very clear. You say you are having trouble but you're not telling us what that trouble is?

You have an array that you want to save in the database. You can either map those array entries to a table in MySQL (each row being an entry in the array), or you serialize/encode the array and save it in the database where-ever it is relevant for your use-case.

It's hard to help you properly when you don't explain exactly what the problem is though.
 
My script2 is coming out as NULL or it's only displaying one value rather than the entire array. Sorry I wish I knew what I was talking about a little more.

What you said looks like what I'm looking for (mapping, serializing). I've already encoded from what im reading in tutorials. So I'll read about mapping and serializing and apply something.


But my issue is ->
List1 outputs as (on script2)

Name
Age

RATHER THAN (what i want it to do)

Name: SomeName
Age: 24

Name:SomeName2
Age:25


Thanks Naatan!
 
First of all I assume you are grabbing information that is not directly accessible to you from the database?!
I'm also confused as to how you wish to save the info. As an array or as individual names inserted into a table?
To display or break down the array you could use the foreach function.
I assume your array can have multiple lists? As in List1, List2...
Use two loops. One nested inside the other. The first to loop through your array and the second to loop through each of your lists (while skipping the items you do not want such as Item1).
 
YUP that did it thanks syndol!

I want to display the information. I want to store it into a DB so I could use it for other purposes a lot more easily. But i was just trying to go for a more basic approach on displaying it on one page because if I could grab an individual thing like Name and display all the names in the database, then i could figure out how to post it to a database from there.
 
If the information is already in the database then why do you want to re-save it? And why grab it for a url?
Why not just create a function to grab the info directly from the database as an array?
To display your list you can use foreach in your template to loop your array of names and ages.
 
If the information is already in the database then why do you want to re-save it? And why grab it for a url?
Why not just create a function to grab the info directly from the database as an array?
To display your list you can use foreach in your template to loop your array of names and ages.
because it's from a type of gaming. Kinda like PSN leaderboards but for some reason what I used for PSN leaderboards didn't work on this particular json data scrape.

I want to grab it directly but since this involves games and scores/categories... I want a user to be able to sort by name, score, and category. With my PSN Leaderboard script I can do that. But their data wasn't all clumped in one database either....
 
Why not have your PSN leaderboard script dump the relevant data directly into your target database. Doing this through http and encoding / decoding the data repeatedly is not a very efficient solution and will certainly affect your server's performance depending on the amount of data you have and how often the job is ran.

It sounds like you're very new to programming though, in which case you're probably glad to have something working at all. Maybe as a means to educate yourself you can take what you've learned and reimplement it properly from scratch.
 
Yeah I wasn't lying when I said noob. I can do really basic stuff like make a CMS, membership system, forms, shoutbox, simple scrapers, and a lot of other simple PHP/MySQL stuff. Arrays is a new area for me. It took me at least 4 months to get where I'm at with a PSN leaderboard. lol

But I'm confident that in a month I'll think of arrays as grade school knowledge XD
 
If I may give you one piece of advice; don't avoid subjects you don't understand. Rather, read up on them and subsequently try to apply them.

Four months of PHP without using arrays is akin to speaking without using the letter "e". I can only imagine your reason for not learning it sooner is simply because you avoided it.
 
If I may give you one piece of advice; don't avoid subjects you don't understand. Rather, read up on them and subsequently try to apply them.

Four months of PHP without using arrays is akin to speaking without using the letter "e". I can only imagine your reason for not learning it sooner is simply because you avoided it.
I hardly used it or if I used it... I didn't think of it as difficult? I've used it in forms before.

:\ I actually used to have a partner who did all the php and whatever other languages but he became ill and will no longer be working. I used php for basic things. VERY basic but never anything involving arrays. I learn as I go and need and now I need arrays, so that's what I'm learning now lol.
 
Top Bottom