Solved

Chunking and Assi=ociative array into smaller arrays.

Posted on 2011-09-22
7
389 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 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

15 Experts available now in Live!

Get 1:1 Help Now