How to Re-Index All Subarray Elements of a Multidimensional Array

How to re-index all subarray elements of a multidimensional array?

To reset the keys of all arrays in an array:

$arr = array_map('array_values', $arr);

In case you just want to reset first-level array keys, use array_values() without array_map.

Filter and reindex array

PHP arrays are not indexed, because they are not real arrays. They are in fact ordered hashmaps and as such you should not really care about the keys here. Iterating over these arrays is trivial and does not require using array_values at all.

foreach ($_SESSION['ShowingRequests']['ListingKey'] as $key => $value) {
echo "$key => $value\n";
}

Would give you...

        1 => 97826889139
2 => 97820967049
4 => 97825243774
5 => 97824864611

Where you get the name of the key and the value for each element in the array using the foreach construct.

In any case you have to remember that both array_values and array_filter are non-destructive functions. They return a new array. They do not modify the array by reference. As such you must assign the return value if you want to modify the existing array. They also do not work recursively.

$_SESSION['ShowingRequests']['ListingKey'] = array_values(array_filter($_SESSION['ShowingRequests']['ListingKey']));
$_SESSION['ShowingRequests']['Key'] = array_values(array_filter($_SESSION['ShowingRequests']['Key']));
$_SESSION['ShowingRequests'] = array_values(array_filter($_SESSION['ShowingRequests']));

Merge two multidimensional arrays and reindex all subarrays

FIXED (again)

function array_merge_to_indexed () {
$result = array();

foreach (func_get_args() as $arg) {
foreach ($arg as $innerArr) {
$result[] = array_values($innerArr);
}
}

return $result;
}

Accepts an unlimited number of input arrays, merges all sub arrays into one container as indexed arrays, and returns the result.

EDIT 03/2014: Improved readability and efficiency

Recursively call array_values() to re-index variable depth menu array

use this

<?php 

function reOrderArray($array) {
if(! is_array($array)) {
return $array;
}
$count = 0;
$result = array();
foreach($array as $k => $v) {
if(is_integer_value($k)) {
$result[$count] = reOrderArray($v);
++$count;
} else {
$result[$k] = reOrderArray($v);
}
}
return $result;
}

public function is_integer_value($value) {
if(!is_int($value)) {
if(is_string($value) && preg_match("/^-?\d+$/i",$value)) {
return true;
}
return false;
}
return true;
}

How can I reindex a multi-dimensional array?

Maybe this function will solve your issue

var_dump(array_map("array_values",arrayRecursiveDiff($a,$b)));

Edit:
This one keeps non-digit indexes:

var_dump(array_map(create_function('$x','$k = key($x); return (is_numeric($k)) ? array_values($x) : $x;'),$aDiff));

Note that this function only works for level 2 array-reindex.

Getting all array elements with same index in multidimensional array

something like

foreach ($stdClass as $sclass){
if(!is_array($sclass)){
echo $sclass.' ';
}else{
foreach ($sclass as $scl){


echo $scl.' ';
}
}

}

Rebase array keys after unsetting elements

Try this:

$array = array_values($array);

Using array_values()

How to add a vector element to every element of the subarray along the associated index of a multidimensional array

For your simple case, you can do:

B[:] = A[:, None]

This works because of broadcasting. By simulating the dimensions of B in A, you tell numpy where to place the elements unambiguously. For a more general case, where you want to place A along dimension k of B, you can do:

B[:] = np.expand_dims(A, tuple(range(A.ndim, A.ndim + B.ndim - k)))

np.expand_dims will add axes at the indices you tell it to. There are other ways too. For example, you can index A with B.ndim - k - 1 instances of None:

B[:] = A[(slice(None), *(None,) * (B.ndim - k - 1))]

You can also use np.reshape to get the correctly shaped view:

B[:] = A.reshape(-1, *np.ones(B.ndim - k - 1, dtype=int))

OR

B[:] = A.reshape(-1, *(1,) * (B.ndim - k - 1))

OR

B[:] = A.reshape((-1,) + (1,) * (B.ndim - k - 1))

In all these cases, you only need to expand the trailing dimensions of A, since that's how broadcasting works. Since broadcasting is such an integral part of numpy, you can simply relpace = with += to get the expected result.



Related Topics



Leave a reply



Submit