Solved

Chunking and Assi=ociative array into smaller arrays.

Posted on 2011-09-22
7
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 look for a specific file type in a local or remote server directory using PHP.

751 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