www.webdeveloper.com
Results 1 to 5 of 5

Thread: Breaking up 2D array into 2 separate 2D arrays..

  1. #1
    Join Date
    Mar 2010
    Posts
    281

    Breaking up 2D array into 2 separate 2D arrays..

    Hi Everyone,
    Here's something im' trying to do. I have a 2 dimensional array via some post values. Here's what it looks like. i've abbreviated here for readability.
    PHP Code:
    Array
    (
        [
    id] => Array
            (
                [
    0] => 78
                
    [1] => 79
                
    [2] => 80
                
    [3] => 81
                
    [4] => 82
            
    )

        [
    manualid] => Array
            (
                [
    0] => 1
                
    [1] => 2
                
    [2] => 3
                
    [3] => 4
                
    [4] => 5
            
    )

        [
    url] => Array
            (
                [
    0] => www.urlgoeshere.com
                
    [1] => www.urlgoesheretoo.com
                
    [2] => www.urlgoesherethree.com
                
    [3] => www.urlgoesherefour.com
                
    [4] => www.urlgoeshere.com        )
        )
        [
    info] => Array
            (
                [
    0] => Website info goes here for website 3. Lorem ipsum etc.
                [
    1] => Website info goes here for website 3. Lorem ipsum etc.
                [
    2] => Website info goes here for website 3. Lorem ipsum etc.
                [
    3] => Website info goes here for website 4. Lorem ipsum etc.
                [
    4] => Website info goes here for website 5. Lorem ipsum etc.
            )
        [
    add_id] => 
        [
    add_manualid] => 
        [
    add_url] => 
        [
    add_info] => 
        [
    submit] => submit

    I'm trying to break into 2 separate arrays so i can process and treat differently. I got the first half of this problem solved but not the second half. For the first half i did this....

    PHP Code:
    $postArray1 = array();
    $postArray2 = array();

    foreach(
    $_POST as $key => $value) {
        
    array_push($postArray1$key);
        if(
    $key == 'add_manualid') {
        break;
        }

    this works, but i'm trying to figure out how to start and continue the foreach loop at the 4th element ('add_id').
    what should i do.

    and i'd like to add...
    i really thought that if i echoed this. $_POST[0][0] i would get 78 but apparently not. what that echoes is NULL,
    just for the sake of understanding. how would i echo a single value within the second loop. like say...
    [id][1][79]
    (which i thought would be $_POST[0][1] ..?)
    ..?
    huge thanks for any help on this!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,311
    It's not clear to me what you actually want to do, but I can answer the second question: you would get the "78" value from $_POST['id'][0], since your first dimension's key is associative (string), not enumerated (integer) like the 2nd dimension is in that sub-array.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2010
    Posts
    227
    wow youre brave nodog.
    His question was really confusing.

    Break an array of MULTIPLE arrays into two arrays... based on what criteria?

    PHP Code:
    $add_manualid_array $_POST['add_manualid']; 

  4. #4
    Join Date
    Mar 2010
    Posts
    281
    thanks for the reply:

    This is a form that is part of a CMS. The first elements pull content from a table and are therefore used to either edit or delete. the last 4 (excluding the submit btn) are for adding new content (row count will be fixed) and may therefore may all be left empty upon form submission. They need to be put into 2 separate groups and the validated or handled differently. (ex. the last group may be left empty but the first may not.)

    I could put into 2 separate forms but i'd thought it'd be good programming from the user's end to leave all on one form.

    thanks for your all's help -

  5. #5
    Join Date
    Mar 2010
    Posts
    281
    i figured it out, the answers was alluded to NogDog's post. i needed to refer to the first Dim as a string and not a number, I had tried this,

    PHP Code:
    $newArray = array($_POST[0], $_POST[1], $_POST[2], etc.) 
    but then i tried this and it worked

    PHP Code:
    $updateVar1 $_POST['manualid']; $updateVar2 $_POST['short_name']; $updateVar2 $_POST['long_name'];

    $newArray = array($updateVar1$updateVar2$updateVar3); 
    as always, thank you for the invaluable help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles