0

I have an called $array as follows. I need to be able to pull out specific arrays within it based off the [day] value.

So for instance, I could have $day2 equal to $array[1] because its day value is 2 and so on. How can I go about doing that? They are not ordered when they come in and there cannot be multiples as in the max amount of arrays in $array can be 7 as there are 7 days in the week.

Array
(
    [0] => Array
        (
            [time_id] => 1
            [user_id] => 4
            [day] => 1
            [12a] => 0
            [1a] => 0
            [2a] => 0
            [3a] => 0
            [4a] => 0
            [5a] => 0
            [6a] => 0
            [7a] => 0
            [8a] => 0
            [9a] => 0
            [10a] => 0
            [11a] => 0
            [12p] => 1
            [1p] => 1
            [2p] => 1
            [3p] => 1
            [4p] => 1
            [5p] => 1
            [6p] => 1
            [7p] => 1
            [8p] => 0
            [9p] => 0
            [10p] => 0
            [11p] => 0
        )

    [1] => Array
        (
            [time_id] => 2
            [user_id] => 4
            [day] => 2
            [12a] => 1
            [1a] => 1
            [2a] => 1
            [3a] => 1
            [4a] => 1
            [5a] => 1
            [6a] => 1
            [7a] => 1
            [8a] => 1
            [9a] => 1
            [10a] => 1
            [11a] => 1
            [12p] => 1
            [1p] => 1
            [2p] => 1
            [3p] => 1
            [4p] => 1
            [5p] => 1
            [6p] => 1
            [7p] => 1
            [8p] => 1
            [9p] => 0
            [10p] => 1
            [11p] => 1
        )

)

EDIT for anyone interested :

Here is what I ended up with... $settings being the original multi array...

//set counter
$counter = 0;

//loop through and set different day arrays
foreach($settings as $value) {
    // inc counter
    $counter++;

    //set array using variable variables ($hourstemp is actually $hours1, $hours2, $hours3, etc.)
    if($value['day'] == $counter) {
        $hourstemp = 'hours'.$counter;
        $$hourstemp = $value;
    }
}
1
  • 2
    I still dont quite understand what you are looking to do. You want to assign $array[1] to a new variable called $day2 because $array[1]['day'] == 2? So you want to have 7 new variables, $day1, $day2, $day3, $day4, $day5, $day6, $day7 All matching on the $array[X]['day']? Commented Mar 20, 2014 at 1:13

1 Answer 1

1

If you want to get a specific day:

$day = 2;

foreach($array as $value) {
    if($value['day'] == $day) {
         $result = $value;
         break;
    }
}

Or if they are consistent in that 'day' is always 1 greater than the index, then just:

$result = $array[$day-1];

Or if you want to reorder the array so the index is the 'day':

foreach($array as $value) {
    $result[$value['day']] = $value;
}
//then you can just use $result[$day]
Sign up to request clarification or add additional context in comments.

1 Comment

I was hoping there would be a way without foreach, but I could not find anything... this is working as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.