Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been trying to create a Multidimensional array from an already existing array. The reason I am doing this is so I can separate the super array into a more categorized version so that later on I can run foreach on just those categories in another script.

This is a snippet of code // Please read comments :)

$and = array();

if($this-> input-> post('and')) // This is the super array and[] from a previous input field
{
    if(preg_grep("/reason_/", $this-> input-> post('and'))) // Search for the reason_
    {
        foreach($this-> input-> post('and') as $value) // if reason_ is present 
        {
            $and['reason_and'] .= end(explode('_', $value)) . ' , '; // Remove everything before and including _ so then i would get only values like 1, 2, 3, 4, and then concatenate them to the index
        }
    }
    if(preg_grep("/status_/", $this-> input-> post('and'))) // Search for status
    {
        foreach($this-> input-> post('and') as $value) // If it is in the super array
        {
            $and['status_and'] .= end(explode('_', $value)) . ' , '; // Remove everything before and including _ so then I would get values again like 1,2,3,4,5 and then concatenate them to the index
        }
    }
}

This approach is not giving me expected outcome however, I am getting a big string like so :

 array(2) { ["reason_and"]=> string(24) "2 , 3 , 4 , 3 , 4 , 5 , " 
            ["status_and"]=> string(24) "2 , 3 , 4 , 3 , 4 , 5 , " 

So to my knowledge (which is limited) when I try to do a foreach over the array

[reason_and]

I only get one loop since the array ["reason_and] only has one value (the 24 character string?). Is it possible to have the reason_and have a value for each of the numbers?

Is this even possible? I'm quite confused.

I've referred to this question for reference but I still do not get a outcome that I can work with. Thanks in advance.

share|improve this question
    
You first must explode into another array, then you can do another foreach. –  demonofnight May 24 '13 at 15:08
    
What does this even mean? - Sorry –  user2406611 May 24 '13 at 15:13

2 Answers 2

up vote 1 down vote accepted

first of all preg_grep returns an array with matched values, so

    $andArray = $this-> input-> post('and'); // This is the super array and[] from a previous input field

    if($andArray) {

    $reason = preg_grep("/reason_/", $andArray); // Search for the reason_

       if($reason) { // if reason_ is present 

foreach($reason as $value) {
                $and['reason_and'] .= end(explode('_', $value)) . ' , '; // Remove everything before and including _ so then i would get only values like 1, 2, 3, 4, and then concatenate them to the index
            }
        }

    $status = preg_grep("/status_/", $andArray); // Search for status

        if($status) {

            foreach($status as $value){ // If it is in the super array

                $and['status_and'] .= end(explode('_', $value)) . ' , '; // Remove everything before and including _ so then I would get values again like 1,2,3,4,5 and then concatenate them to the index
            }
        }
    }

Or if you need result as array, then remove ' , ' and replace dot with [];

share|improve this answer
    
You literally answered another question I was about to ask before I even asked. You are freaking awesome! I didn't read the docs well on preg_grep. Thank you so much. –  user2406611 May 24 '13 at 15:48

This

        $and['reason_and'] .= end(explode('_', $value)) . ' , ';
                          ^^^^----

should be

        $and['reason_and'][] = end(explode('_', $value)) . ' , ';
                          ^^--

which turns it into an "array push" operation, not a string concatenation. Then 'reason_and' will be an array, and you foreach over it.

share|improve this answer
    
You helped me very much, I wish I could accept two answers and give answers +1 but the other answer is much more in detail and helped me figure out preg_grep. Thank you Marc –  user2406611 May 24 '13 at 15:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.