Parsing array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joeyd
Forum Newbie
Posts: 10
Joined: Wed Mar 10, 2010 2:08 pm

Parsing array

Post by joeyd »

Hello,

Having a bit of trouble trying to figure out how I can iterate through an array in PHP to update the database. I am getting a string with all the elements serialized from jquery ui using the .sortable. This is a nested list made of UL/LI tags. Here is an example: http://www.b-hind.eu/jquery/index.php

The array comes out as a serialized string... (formatted below to make it easier to read)

listItem[]=472
&listItem_472[]=478
&listItem[]=479
&listItem[]=480
&listItem[]=481
&listItem[]=469
&listItem[]=445
&listItem[]=468
&listItem[]=477

This is what I need to to translate as.

0=472
472=478
0=479
0=480
0=481
0=469
0=445
0=468
0=477

The left number indicates parentid. The right number indicates the childid. "0" is the root level.

Any help much appreciated.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Parsing array

Post by requinix »

joeyd wrote:(formatted below to make it easier to read)
What's the unformatted version? What do you actually have? Is it an array? A string? Something else?
joeyd
Forum Newbie
Posts: 10
Joined: Wed Mar 10, 2010 2:08 pm

Re: Parsing array

Post by joeyd »

Thanks for the reply...

This is the jquery code that sends the string to my save page.

Code: Select all

$('#navlist').sortable({
            'items':'li',
            'placeholder':'placeholder2',
            'nested':'ul',
            'maxLevels':5,
            update : function () {
          var order = $('#navlist').sortable('serialize');
            
        $("#result").load("save.php?"+order);
       
Here is the unordered list... The string comes from the ID of each element. Root level items are just listItem[].

Code: Select all

<ul class="ui-sortable" id="navlist">
  <li id="listItem_472">Services
    <ul>
      <li class="" style="" id="listItem_478">IT/Networking</li>
    </ul>
  </li>
  <li id="listItem_479">Products</li>
  <li id="listItem_480">Case Studies</li>
  <li id="listItem_481">Partners</li>
  <li id="listItem_469">About Us</li>
  <li id="listItem_445">Bios</li>
  <li id="listItem_468">Careers</li>
  <li id="listItem_477">Contact Us</li>
</ul>
This is the actual string that is given via jquery...

listItem[]=472&listItem_472[]=478&listItem[]=479&listItem[]=480&listItem[]=481&listItem[]=469&listItem[]=445&listItem[]=468&listItem[]=477
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Parsing array

Post by requinix »

First I'd check that the query string is valid for your purpose.

Code: Select all

if (preg_match('/^(listItem(_\d+)?\[\]=\d+&?)+$/', $_SERVER["QUERY_STRING"])) {
Then you can iterate over $_GET. If the key is just "listItem" then substitute it for "listItem_0".
Given that, the parent node ID is (int)substr(key, 10) and the child node ID is the value.
Post Reply