Viewed   239 times

The question is how to reset key e.g. for an array:

Array ( 
    [1_Name] => Array ( 
        [1] => leo 
        [4] => NULL 
    ) 
    [1_Phone] => Array ( 
        [1] => 12345 
        [4] => 434324
    )  
)

reset to :

Array ( 
    [1_Name] => Array ( 
        [0] => leo 
        [1] => NULL 
    ) 
    [1_Phone] => Array ( 
        [0] => 12345 
        [1] => 434324
    ) 
)

 Answers

5

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.

Friday, August 5, 2022
2

I updated your function and it is giving expected output. Please give a try as following.

function array_key_exists_r($needle, $haystack){
    $result = array_key_exists($needle, $haystack);
    if ($result) 
    {
        foreach ($haystack as $a=>$v) 
        {
            if($needle == $a)
                return $haystack[$a];
            if (is_array($v)) {
                $result = array_key_exists_r($needle, $v);
            }
            if ($result) return $result;
        }
    }
    foreach ($haystack as $v) {
        if (is_array($v)) {
            $result = array_key_exists_r($needle, $v);
        }
        if ($result) return $result;
    }
    return $result;
};
Thursday, December 1, 2022
 
asaph
 
2

The actual problem is you're not assigning the result of array_reverse to anything

// change this
array_reverse($respArr);

// to this
$respArr = array_reverse($respArr);
Friday, August 19, 2022
 
benvd
 
4
function a_iconv(array &$arr) {
  foreach ($arr as $key => $val) {
    if (is_array($val)) {
      a_iconv($arr[$key]);
    } else {
      unset($arr[$key]);
      $arr[iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $key)] = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $val);
    }
  }
}

That function should do it.

function a_iconv(array $src) {
  $dst = array();
  foreach ($src as $key => $val) {
    if (is_array($val)) {
      $dst[iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $key)] = a_iconv($val);
    } else {
      $dst[iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $key)] = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $val);
    }
  }
  return $dst;
}

That one should do it without modifying the original array, returning the new version instead. Both functions recursively apply themselves on any array in your original array, applying iconv on any non array entry. I didn't checked for objects since I assume you don't have some in your array. Use get_object_vars() if that happens to be the case.

Friday, December 16, 2022
5
function printAll($a) {
    if (!is_array($a)) {
        echo $a, ' ';
        return;
    }

    foreach($a as $k => $value) {
         if($k<10){
             printAll($k);
             printAll($value);
        }
    }
}
Tuesday, October 18, 2022
 
uwe_98
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :