Solved

Chunking and Assi=ociative array into smaller arrays.

Posted on 2011-09-22
7
379 Views
Last Modified: 2012-05-12
OK I have an multidimensional array $arrLists:-

AStatus
            For Sale
                        ID          125
                        Other       99
            Hold Sale
                        ID          56
                        Other       12
            Not for Sale
                        ID          22
                        Other       32
Base
            Canvas
                        ID          326
                        Other       99
            Cloth
                        ID          17
                        Other       9

Open in new window


I want to split it into two smaller arrays $arrAStatus:-
For Sale
            ID          125
            Other       99
Hold Sale
            ID          56
            Other       12
Not for Sale
            ID          22
            Other       32

Open in new window


and $arrBase:-

Canvas
            ID          326
            Other       99
Cloth
            ID          17
            Other       9

Open in new window


How do I achieve this?
0
Comment
Question by:cescentman
  • 3
  • 3
7 Comments
 
LVL 9

Expert Comment

by:ghodder
ID: 36585313
$arrAStatus = $arrLists['AStatus'];
$arrBase = $arrLists['Base'];

Open in new window

0
 
LVL 1

Author Comment

by:cescentman
ID: 36585431
Sorry I can see now that I have been less that specific. Indeed what you suggest would work but I want to loop through $arrLists and create each array automatically based on the key.
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 36586089
Try this

<?php
    $a = Array('AStatus' => Array('For Sale' => Array('ID' => 125, 'Other' => 99),
                                    'Hold Sale' => Array('ID' => 56, 'Other' => 12),
                                    'Not for Sale' => Array('ID' => 22, 'Other' => 32)
                                   ),
                'Base' => Array('Canvas' => Array('ID' => 326, 'Other' => 99),
                                 'Cloth' => Array('ID' => 17, 'Other' => 9)
                                )
               );
?>

    <pre>
    <?php print_r($a); ?>
    </pre>
    
<?php
    foreach($a as $key => $val){
        
        $vn = "arr".$key;
        $nvn = $$vn = $val;
        
        echo"<pre>";
        print_r($nvn);
        echo"</pre>";
    }
?>

Open in new window

   
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Closing Comment

by:cescentman
ID: 36586113
Thanks that's brilliant.

I'm not familiar with the syntax:-

$nvn = $$vn = $val;

Can you enlightne me?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36586134
Sure,

// this below makes the array name based on the key,
// so there are two arrays arrAStatus, arrBase
// vn - variable name
$vn = "arr".$key;

// nvn - new variable name
// adding $ to $ makes a variables variable, because the $vn is the string
// for example "arrBase", so adding an extra $ will produce a variable named $arrBase
// and assign the $val to it, which in this case is the array below
$nvn = $$vn = $val;

Hope it's clear
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36586138
in other words:

$newVariable = $("arrAStatus"/"arrBase") = the array for the proper key
0
 
LVL 1

Author Comment

by:cescentman
ID: 36592076
Brilliant thanks. Learnt something new
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Router for PHP reqeusts 12 33
get radio button vale in array 7 37
Re-imbursement Claim System 3 25
sql sentence 2 13
Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now