Alex
Alex

Reputation: 132

Displaying array chunks in php

im trying to display a specific chunk of an array where an element is located.

for example, lets say this is my array

Array
(
    [0] => Array
        (
            [1] => a
            [2] => b
            [3] => c
            [4] => d
            [5] => e            
        )

    [1] => Array
        (
            [6] => f
            [7] => g
            [8] => h
            [9] => i
            [10] => j   
        )
)

how do i search the array for the 10th key for example, and choose only the chunk its located in?

EDIT

thanks for the answers, however i didnt need a function so i came up with this, simple solution, i think:

$temp1 = array_chunk($cart_items, 5,true);
foreach ($temp1 as $key => $value) {
    foreach ($value as $key2 => $value2) {
        if($key2 == $itemid){
            $c_id = $key;
        }
        else{

        }
    }
}

Upvotes: 1

Views: 669

Answers (4)

Adam Cameron
Adam Cameron

Reputation: 29870

Caveat: I am very new to PHP (started a coupla weeks back).

How about this:

function findChunks($array, $key){
    return array_filter($array, function($subArray) use ($key){
        return array_key_exists($key, $subArray);
    });
}

$chunks = findChunks($a, 10);
var_dump($chunks);

Note: this will return multiple chunks if there are multiple chunks with the same key in them (which is entirely possible, depending on your data structure).

If you definitely only wanted the first chunk which matched, array_filter() is possibly doing too much work, as it will traverse the entire outer array whether you need it to or not. That that might matter is down to how big your arrays are.

If performance was more important than clarity of intent, then using a more traditional conditional loop might be more the ticket.

Upvotes: 2

Arjen
Arjen

Reputation: 1321

A recursive solution, in case there is a possiblity of there being subarrays.

It does have one flaw and that is that it's printing the array directly in the function, instead of returning you the key. But that should be fixable if desired.

$array = array(
    array(
        'a', 'b', 'c', 'd', 'e'
    ),
    array(
        'f', 'g', 'h', 'i', 'j', array(
            'k1', 'k2', 'k3',
        ),
    ),
    array(
        'x', 'y', 'z'
    )
);

function displayChunk($array, $nthElement, &$counter) {
    if(!is_array($array)) {
        return false;
    }

    $counter += count($array);
    if($counter >= $nthElement) {
        print_r($array);
        return true;
    }

    foreach($array as $subArray) {
        if(displayChunk($subArray, $nthElement, $counter) === true) {
            return true;
        }
    }
}

$counter = 0;
displayChunk($array, 12, $counter);

Demo

Upvotes: 0

jogesh_pi
jogesh_pi

Reputation: 9782

you want like this:

function find_chunk($arr, $find_value){
    if( !is_array($arr) && count($arr) == 0) return false;

    foreach($arr as $k=>$v) {
        if( is_array($v) && count($v) > 0 ) {
            foreach($v as $i=>$j) if ($i == $find_value) return $k;
        }
    }
    return false;
}

$arr = array_chunk(range(1, 10), 5, true);
print_r($arr);

echo 'Chunk is:' . find_chunk($arr, 9);

this is the DEMO

Upvotes: 0

bnx
bnx

Reputation: 427

You can use a little function to search your array to find the 10th key and return that "chunk" - we're actually finding the key for your chunk, but then you can load only this chunk using that key.

function findChunk($haystack, $needle){
    foreach($haystack as $key => $value){
        if(array_key_exists($needle, $value))
            return $key;
        }
    return false;
}

So then you'll have a key, and use this on your original array:

$chunk = $original_array[$key_from_function];

Upvotes: 0

Related Questions