0
[
 ["STRG008c0",2,0,"orange","***STRG008*#*"], 
 ["STRG009c0",3,0,"orange","***STRG009*#*"], 
 ["STRG001c0",2,0,"green","***STRG001*#*"], 
 ["STRG003c0",3,0,"green","***STRG003*#*"], 
 ["STRG002c0",4,0,"green","***STRG002*#*"]
]

How do I get the below in a loop in PHP?

***STRG008*#*
***STRG009*#*
***STRG001*#*
***STRG003*#*
***STRG002*#*
nice_dev
  • 17,053
  • 2
  • 21
  • 35

3 Answers3

1

It looks like you want the last column values as a string.

Method 1:

Collect all last values of each subarray in a new variable, say $result and implode() them later.

<?php

$array = [
     ["STRG008c0",2,0,"orange","***STRG008*#*"], 
     ["STRG009c0",3,0,"orange","***STRG009*#*"], 
     ["STRG001c0",2,0,"green","***STRG001*#*"], 
     ["STRG003c0",3,0,"green","***STRG003*#*"], 
     ["STRG002c0",4,0,"green","***STRG002*#*"]
    ];

$result = [];
foreach ($array as $subarray) {
    $result[] = end($subarray);
} 

$result = implode(",",$result);

echo $result; 

Method 2:

You can use array_column() to filter only last column values and implode() them later. This would be a one-liner.

<?php

$array = [
     ["STRG008c0",2,0,"orange","***STRG008*#*"], 
     ["STRG009c0",3,0,"orange","***STRG009*#*"], 
     ["STRG001c0",2,0,"green","***STRG001*#*"], 
     ["STRG003c0",3,0,"green","***STRG003*#*"], 
     ["STRG002c0",4,0,"green","***STRG002*#*"]
    ];


$result = implode(",", array_column($array,4));
echo $result;
nice_dev
  • 17,053
  • 2
  • 21
  • 35
0

You have to iterate through an array of arrays

$a = [
["STRG008c0",2,0,"orange","***STRG008*#*"], 
["STRG009c0",3,0,"orange","***STRG009*#*"], 
["STRG001c0",2,0,"green","***STRG001*#*"], 
["STRG003c0",3,0,"green","***STRG003*#*"], 
["STRG002c0",4,0,"green","***STRG002*#*"]
];

$keys = array_keys($a);
for($i = 0; $i < count($a); $i++) {
    
    foreach($a[$keys[$i]] as $key => $value) {
        if($key == 4) 
          echo $value . "\n";
    };
}
portatlas
  • 657
  • 6
  • 12
  • thank you for help me.actually its genarate from loop,so may be that is the reson its not work .. something like this $fullAry="[".substr(trim($nn),0, -1)."]"; //echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx".$fullAry; $firstArray=array($fullAry); $keys = array_keys($firstArray); for(@$i = 0; $i < count(@$a); @$i++) { foreach($a[$keys[$i]] as $key => $value) { if($key == 4) echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx". $value . "\n"; } ; } ?> – Rasika Bandaranayaka Jun 27 '20 at 06:23
  • my generated text is like this ["STRG008c0",2,0,"orange","***STRG008*#*"],["STRG009c0",3,0,"orange","***STRG009*#*"],["STRG001c0",2,0,"green","***STRG001*#*"],["STRG003c0",3,0,"green","***STRG003*#*"],["STRG002c0",4,0,"green","***STRG002*#*"] – Rasika Bandaranayaka Jun 27 '20 at 06:34
0

You have to loop through the inner array:

$array = [
     ["STRG008c0",2,0,"orange","***STRG008*#*"], 
     ["STRG009c0",3,0,"orange","***STRG009*#*"], 
     ["STRG001c0",2,0,"green","***STRG001*#*"], 
     ["STRG003c0",3,0,"green","***STRG003*#*"], 
     ["STRG002c0",4,0,"green","***STRG002*#*"]
    ];


    foreach ($array as $array_element) {
        echo $array_element[sizeOf($array_element) - 1]."\n";
    } 
Logeshkumar
  • 131
  • 2
  • 9
  • ["STRG008c0",2,0,"orange","**STRG008*#"],["STRG009c0",3,0,"orange","**STRG009*#"],["STRG001c0",2,0,"green","**STRG001*#"],["STRG003c0",3,0,"green","**STRG003*#"],["STRG002c0",4,0,"green","**STRG002*#"] – Rasika Bandaranayaka Jun 27 '20 at 06:34
  • Can you be specific to the problem, which you facing? @RasikaBandaranayaka – Logeshkumar Jun 27 '20 at 06:47