简体   繁体   English

PHP奇怪的str_split数组输出

[英]PHP strange str_split array output

Ok, im having troubles figuring out why my str_split is giving strange array output, this is the code: 好的,我很难弄清楚为什么我的str_split提供奇怪的数组输出,这是代码:

$test = array(0 => array(53, 22, 12, "string"),
            1 => array(94, 84, 94, "string1"),
            2 => array(56, 45, 104, "string2"),
            3 => array(33, 21, 20, 23, "string3"),
            4 => array(44, 55, 66, 77)
            );

$newArray = array();            
$keys = array_keys($test);
for($i=0; $i < count($test); $i++){
foreach($test[$keys[$i]] as $key => $value){
}
$output = str_split($key);

echo "<pre>";
print_r($output);
echo "</pre>";  

Array output is: 数组输出为:

Array
(
[0] => 3
)

Array
(
[0] => 3
)

Array
(
[0] => 3
)

Array
(
[0] => 4
)

Array
(
[0] => 3
)

And im expecting output like this: 我期望这样的输出:

Array ([0] => 3
       [1] => 3
       [2] => 3
       [3] => 4
       [4] => 3
      )

Im wondering why is this happening? 我想知道为什么会这样吗? Thank you. 谢谢。

To achieve the output given, you would just do : 要获得给定的输出, 您只需执行以下操作

<?php

$test = array(0 => array(53, 22, 12, "string"),
        1 => array(94, 84, 94, "string1"),
        2 => array(56, 45, 104, "string2"),
        3 => array(33, 21, 20, 23, "string3"),
        4 => array(44, 55, 66, 77)
        );

foreach ($test as $row) {
    $output[] = count($row)-1;          // non-associative, so the last key is
}                                       // just the length of the array minus 1

print_r($output);

?>

as the sub-arrays are not associative. 因为子数组没有关联。

If they are, replace the line inside the loop with: 如果是这样,请将循环内的行替换为:

    $keys = array_keys($row);           // get the keys of the row
    $output[] = $keys[count($keys)-1];  // and access the last of them

with your code the solution is, but it is not an efficient way of doing it. 解决方案可以解决您的代码,但这并不是一种有效的方法。

<?php
$test = array(0 => array(53, 22, 12, "string"),
            1 => array(94, 84, 94, "string1"),
            2 => array(56, 45, 104, "string2"),
            3 => array(33, 21, 20, 23, "string3"),
            4 => array(44, 55, 66, 77)
            );

$newArray = array();            
$keys = array_keys($test);
for($i=0; $i < count($test); $i++){
foreach($test[$i] as $key => $value){
    $output[$i] = str_split($key)[0];
}
echo "<pre>";
//print_r($output);
echo "</pre>";  
}
var_dump($output);

output 输出

array (size=5)
  0 => string '3' (length=1)
  1 => string '3' (length=1)
  2 => string '3' (length=1)
  3 => string '4' (length=1)
  4 => string '3' (length=1)

But by changing it to this will work, for arrays when the string position is not constant . 但是通过将其更改为该选项将适用于字符串位置不恒定的数组。

<?php
$test = array(0 => array(53, 22, 12, "string"),
            1 => array(94, 84, 94, "string1"),
            2 => array(56, 45, 104, "string2"),
            3 => array(33, 21, "string3", 20, 23),
            4 => array(44, 55, 66, 77)
            );



$newArray = array();            
$keys = array_keys($test);
for($i=0; $i < count($test); $i++){
foreach($test[$i] as $key => $value){
    if(is_string($value)){
       unset($test[$i][$key]);
    }
    $output[$i] = count($test[$i]);
}
echo "<pre>";
//print_r($output);
echo "</pre>";  
}
var_dump($output);

output 输出

array (size=5)
  0 => int 3
  1 => int 3
  2 => int 3
  3 => int 4
  4 => int 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM