Skip to content

Instantly share code, notes, and snippets.

@isotrope
Last active September 22, 2015 21:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isotrope/0eeb8e7c69f6769c29bc to your computer and use it in GitHub Desktop.
Save isotrope/0eeb8e7c69f6769c29bc to your computer and use it in GitHub Desktop.
Traverse all the arrays
<?php
/***
* ____ _____ _____ _____ _____ _ _ _
* / __ \| __ \|_ _/ ____|_ _| \ | | /\ | |
* | | | | |__) | | || | __ | | | \| | / \ | |
* | | | | _ / | || | |_ | | | | . ` | / /\ \ | |
* | |__| | | \ \ _| || |__| |_| |_| |\ |/ ____ \| |____
* \____/|_|__\_\_____\_____|_____|_| \_/_/ \_\______|
* /\ | __ \| __ \ /\\ \ / /
* / \ | |__) | |__) | / \\ \_/ /
* / /\ \ | _ /| _ / / /\ \\ /
* / ____ \| | \ \| | \ \ / ____ \| |
* /_/ \_\_| \_\_| \_\/_/ \_\_|
*
*
*/
/*
* This is just to re-create the array you were showing us
*/
$original_array = array();
$bla = new stdClass();
$bla->id = 6;
$bla->name = 'PURL';
$bla->form_key = 'negl6u';
$original_array[] = $bla;
$bla->id = 9;
$bla->name = 'PURL List';
$bla->form_key = '73yyhk';
$original_array[] = $bla;
echo '<pre>';
var_dump( $original_array );
echo '</pre>';
/***
* ______ ____ _____ ______ _____ _ _
* | ____/ __ \| __ \| ____| /\ / ____| | | |
* | |__ | | | | |__) | |__ / \ | | | |__| |
* | __|| | | | _ /| __| / /\ \| | | __ |
* | | | |__| | | \ \| |____ / ____ \ |____| | | |
* |_| \____/|_| \_\______/_/ \_\_____|_| |_|
*
*
*/
/*
* Foreach iteration
* Loop through the array with a foreach
*/
// Prepping an array that we're going to stick stuff into
$foreach_array = array();
foreach ( $original_array as $array_element ) {
// At this point, you don't grab the values with $array_element['name']
// Because it's an object, it'll be with $array_element->name
$foreach_array[ $array_element->name ] = $array_element->name;
}
echo '<h1>Foreach traversal</h1>';
echo '<pre>';
var_dump( $foreach_array );
echo '</pre>';
/***
* ______ ____ _____ _____ _______ ______ _____ _______ _____ ____ _ _
* | ____/ __ \| __ \ |_ _|__ __| ____| __ \ /\|__ __|_ _/ __ \| \ | |
* | |__ | | | | |__) | | | | | | |__ | |__) | / \ | | | || | | | \| |
* | __|| | | | _ / | | | | | __| | _ / / /\ \ | | | || | | | . ` |
* | | | |__| | | \ \ _| |_ | | | |____| | \ \ / ____ \| | _| || |__| | |\ |
* |_| \____/|_| \_\ |_____| |_| |______|_| \_\/_/ \_\_| |_____\____/|_| \_|
*
*
*/
/*
* Iteration
* Loop through the array with an iteration
*/
$iteration_array = array();
for ( $i = 0; $i < count( $original_array ); $i ++ ) {
$iteration_array[ $original_array[ $i ]->name ] = $original_array[ $i ]->name;
}
echo '<h1>Iteration traversal</h1>';
echo '<pre>';
var_dump( $iteration_array );
echo '</pre>';
/*
* Iteration
* Same as above but a bit easier top read for the array positions
*/
$alternative_iteration_array = array();
for ( $i = 0; $i < count( $original_array ); $i ++ ) {
$curr_item = $original_array[ $i ];
$alternative_iteration_array[ $curr_item->name ] = $curr_item->name;
}
echo '<h1>Alternative iteration traversal</h1>';
echo '<pre>';
var_dump( $alternative_iteration_array );
echo '</pre>';
// ----------------------------------
$original_array2 = array(
array(
'id' => 6,
'name' => 'PURL',
'form_key' => 'negl6u',
),
array(
'id' => 9,
'name' => 'PURL',
'form_key' => 'negl6u',
),
);
echo '<h1>What if it\'s and array of arrays, you ask?</h1>';
echo '<pre>';
var_dump( $original_array2 );
echo '</pre>';
/***
* ______ ____ _____ ______ _____ _ _
* | ____/ __ \| __ \| ____| /\ / ____| | | |
* | |__ | | | | |__) | |__ / \ | | | |__| |
* | __|| | | | _ /| __| / /\ \| | | __ |
* | | | |__| | | \ \| |____ / ____ \ |____| | | |
* |_| \____/|_| \_\______/_/ \_\_____|_| |_|
*
*
*/
/*
* Foreach iteration
* Loop through the array with a foreach
*/
// Prepping an array that we're going to stick stuff into
$foreach_array2 = array();
foreach ( $original_array2 as $array_element2 ) {
$foreach_array2[ $array_element2['name'] ] = $array_element2['name'];
}
echo '<h1>Foreach traversal</h1>';
echo '<pre>';
var_dump( $foreach_array2 );
echo '</pre>';
/***
* ______ ____ _____ _____ _______ ______ _____ _______ _____ ____ _ _
* | ____/ __ \| __ \ |_ _|__ __| ____| __ \ /\|__ __|_ _/ __ \| \ | |
* | |__ | | | | |__) | | | | | | |__ | |__) | / \ | | | || | | | \| |
* | __|| | | | _ / | | | | | __| | _ / / /\ \ | | | || | | | . ` |
* | | | |__| | | \ \ _| |_ | | | |____| | \ \ / ____ \| | _| || |__| | |\ |
* |_| \____/|_| \_\ |_____| |_| |______|_| \_\/_/ \_\_| |_____\____/|_| \_|
*
*
*/
/*
* Iteration
* Loop through the array with an iteration
*/
$iteration_array2 = array();
for ( $i = 0; $i < count( $original_array2 ); $i ++ ) {
$iteration_array2[ $original_array2[ $i ]['name'] ] = $original_array2[ $i ]['name'];
}
echo '<h1>Iteration traversal</h1>';
echo '<pre>';
var_dump( $iteration_array2 );
echo '</pre>';
/*
* Iteration
* Same as above but a bit easier top read for the array positions
*/
$alternative_iteration_array2 = array();
for ( $i = 0; $i < count( $original_array2 ); $i ++ ) {
$curr_item = $original_array2[ $i ];
$alternative_iteration_array2[ $curr_item['name'] ] = $curr_item['name'];
}
echo '<h1>Alternative iteration traversal</h1>';
echo '<pre>';
var_dump( $alternative_iteration_array2 );
echo '</pre>';
//-+----------------------------------------------
$original_array3 = array(
'some_key' => array(
'id' => 6,
'name' => 'PURL',
'form_key' => 'negl6u',
),
'some_other_key' => array(
'id' => 9,
'name' => 'PURL',
'form_key' => 'negl6u',
),
);
echo '<h1>An array of key=>array() stuff</h1>';
echo '<pre>';
var_dump( $original_array3 );
echo '</pre>';
$keyval_array = array();
foreach ( $original_array3 as $key => $value ) {
// At this point, we're one level deep.
// We're looking at the value of the first level
echo '<pre>';
echo '<div>--------------------------</div>';
echo '<div>The current $key is ' . $key . '</div>';
echo '<div>The current $value is ' . $value . '</div>';
echo '<div>--------------------------</div>';
echo '</pre>';
echo '<h2>Awwww, crap! an error</h2>';
echo '<h3>Let\'s see what a var_dump of $value would do</h3>';
var_dump( $value );
echo '<h1>AHA!</h1>';
/*
* We need to actually loop through $value, which is an array
* Basically, a loop within a loop
*
* Here, you can choose foreach ($value as $curr_array_entry)
* or
* for ( $i = 0; $i < count( $value ); $i ++ ) {
* }
* Your call!
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment