2017-01-03 10 views
-1

배열이 여러 개 있습니다. 각 배열에서 위치 '0'을 에코해야합니다. 어떻게해야합니까?PHP는 여러 배열에서 특정 위치를 반향합니다.

[0] => Array 
    (
     [accountname] => test 
     [0] => test 
    ) 

[1] => Array 
    (
     [accountname] => test2 
     [0] => test2 
    ) 

나는이 시도했지만,이 0 인 배열의 모든 위치, 배열 내부가 아닌 위치 0 메아리.

이제 test, test이 표시되지만, test,test2이 필요합니다.

+2

당신은 당신의 질문에 현재 코드를 추가하는 것을 잊었다. – arkascha

답변

1

array_walk 또는 foreach를 사용하여 배열을 탐색하려면 인덱스 0을 사용하여 내부 배열에 액세스하십시오.

array_walk($array, function($v){echo $v[0];}); 
1

가능한 많은 접근법. 그들 중 일부에 대한 간단한 예제를 살펴 보자 :

<?php 
$input = [ 
    [ 
     'accountname' => 'test', 
     0 => 'test' 
    ], 
    [ 
     'accountname' => 'test2', 
     0 => 'test2' 
    ] 
]; 

// #1: treating the input as a table and selecting a "column": 
var_dump(array_column($input, 0)); 

// #2: using an anonymous "lambda" function: 
$output = []; 
array_walk($input, function($val) use (&$output) { $output[]=$val[0]; }); 
var_dump($output); 

// #3: destructive approach flattening the input: 
$output = $input; 
array_walk($output, function(&$val) { $val = $val[0]; }); 
var_dump($output); 

// #4: simple "foreach" loop, traditional approach: 
$output = []; 
foreach($input as $entry) { 
    $output[] = $entry[0]; 
} 
var_dump($output); 

// #5: classical "for" loop, scales better for big data: 
$output = []; 
for($i=0; $i<count($input); $i++) { 
    $output[] = $input[$i][0]; 
} 
var_dump($output); 

각 분명의 출력은 다음과 같습니다

array(2) { 
    [0] => 
    string(4) "test" 
    [1] => 
    string(5) "test2" 
} 
관련 문제