I have following associative array:
Array
(
[0] => Array
(
[0] => 268
)
[1] => Array
(
[0] => 125
[1] => 258
[2] => 658
[3] => 128
[4] => 987
)
[2] => Array
(
[0] => 123
[1] => 258
)
[3] => Array
(
[0] => 168
)
)
I need the following result as string.
268
125258658128987
123258
168
What I have tried so far;
<?php
//consider my array is in $array variable
for ($i = 0; $i < count($array); $i++)
{
foreach ($array[$i] as $res)
{
echo $res . '<br/>';
}
}
?>
But unfortunately I am getting each numbers in a new line.
Any suggestion will be appreciated.
You have to echo the <br /> outside the foreach loop:
for ($i = 0; $i < count($array); $i++)
{
foreach ($array[$i] as $res) {
echo $res;
}
echo '<br />'; //<-- Put this outside the foreach loop
}
Or another option, you can use foreach and implode for a simpler approach
foreach ($array as $value)
{
echo implode('',$value);
echo '<br />';
}
Doc: implode()
Related
I have an Array Array
( [0] => ["1","2","7","8","9"] )
and i want to like that
Array ( [0] => 1 [1] => 2 [2] => 7 [3] => 8 [4] => 9 )
i try foreach loop
foreach($no_a as $key=>$val){
foreach($val as $k=>$v){
$newp_arr[] $v ;
}
}
$result = $no_a[0];
$new_result = array();
for ($i=0; $i < 21 ; $i++) {
# code...
$num = $result[$i];
if(is_numeric($num))
{
$new_result[] = $result[$i];
}
}
print_r($new_result);
I would like to get the values of id and name inside this array.
Array
(
[data] => Array
(
[0] => Array
(
[id] => 238345159549706
[members] => Array
(
[data] => Array
(
[0] => Array
(
[id] => 100001130889528
[name] => Sy Cheeze
)
[1] => Array
(
[id] => 100002616426665
[name] => Chun Jenny
)
.......
I've tried using this foreach.
foreach ($acquaintances as $acquaintance)
{
foreach ($acquaintance as $acquaint)
{
$acqID = $acquaint['id'];
$acqName = $acquaint['name'];
echo $acqName;
}
}
but nothing will be displayed. What would I do with my code? Any idea and suggestions please. Thank you!
$array = array
(
array("bla",22,18),
array("blaa",15,13),
array("blaaa",5,2),
array("blaaaa",17,15)
);
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$array[$row][$col]."</li>";
}
echo "</ul>";
}
You can also access the indices directly in your foreach loop. Like this:
foreach($acquaintances['data'] as $acquaintance) {
foreach($acquaintance['members']['data'] as $acquaint) {
$acqID = $acquaint['id'];
$acqName = $acquaint['name'];
echo $acqName . '<br/>';
}
}
I want to convert a stdClass object to string and reduce an array with the max value from the stdClass object.
This is my array:
Array
(
[135] => Array
(
[0] => stdClass Object
(
[ID] => 145
)
[1] => stdClass Object
(
[ID] => 138
)
[2] => stdClass Object
(
[ID] => 139
)
)
[140] => Array
(
[0] => stdClass Object
(
[ID] => 163
)
[1] => stdClass Object
(
[ID] => 155
)
)
Basically it should look like this:
Array
(
[135] => 139
[140] => 164
)
Is this possible? I've tried various foreach loops but i don't get it with the stdClass object...
My try so far:
foreach($ids as $k => $v) {
for($i = 0; $i < count($v); $i++) {
$idss[$i] = array()$v;
}
}
That doesn't work.
This will solve your purpose. let me know if anything goes wrong.
$ids[135][0]->ID = 145;
$ids[135][1]->ID = 135;
$ids[135][2]->ID = 155;
$ids[140][0]->ID = 125;
$ids[140][1]->ID = 135;
$idss = array();
foreach($ids as $k => $v) {
for($i = 0; $i < count($v); $i++) {
if(!#$idss[$k] || $v[$i]->ID > $idss[$k])
{
$idss[$k] = $v[$i]->ID;
}
}
}
echo "<Pre>";
print_r($idss);
die;
Already answered but here is a shorter version of this
$final_array =array();
foreach($arr as $key=>$val){
$max = max(array_keys($val));
$final_array[$key] = $val[$max]->ID ;
}
print_r($final_array);
Here $arr is your input array.
You need to do some compariosn in your inner for loop to know which one holds the max value. Here is an example :
$new_arr = array();
foreach($elements as $index => $value){
$max = -1;
$foreach($value as $obj){
if($obj->id > $max)
$max = $obj->id;
}
$new_arr [$index] = $max;
}
Problem
I have an array which is returned from PHPExcel via the following
<?php
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$excelFile = "excel/1240.xlsx";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($excelFile);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$arrayData[$worksheet->getTitle()] = $worksheet->toArray();
}
print_r($arrayData);
?>
This returns:
Array
(
[Films] => Array
(
[0] => Array
(
[0] => Name
[1] => Rating
)
[1] => Array
(
[0] => Shawshank Redemption
[1] => 39
)
[2] => Array
(
[0] => A Clockwork Orange
[1] => 39
)
)
[Games] => Array
(
[0] => Array
(
[0] => Name
[1] => Rating
)
[1] => Array
(
[0] => F.E.A.R
[1] => 4
)
[2] => Array
(
[0] => World of Warcraft
[1] => 6
)
)
)
What I would like to have is
Array
(
[Films] => Array
(
[0] => Array
(
[Name] => Shawshank Redemption
[Rating] => 39
)
[1] => Array
(
[Name] => A Clockwork Orange
[Rating] => 39
)
)
[Games] => Array
(
[0] => Array
(
[Name] => F.E.A.R
[Rating] => 4
)
[1] => Array
(
[Name] => World of Warcraft
[Rating] => 6
)
)
)
The arrays names (Films, Games) are taken from the sheet name so the amount can be variable. The first sub-array will always contain the key names e.g. Films[0] and Games[0] and the amount of these can be varible. I (think I) know I will need to do something like below but I'm at a loss.
foreach ($arrayData as $value) {
foreach ($value as $rowKey => $rowValue) {
for ($i=0; $i <count($value) ; $i++) {
# code to add NAME[n] as keys
}
}
}
I have searched extensively here and else where if it is a duplicate I will remove it.
Thanks for any input
Try
$result= array();
foreach($arr as $key=>$value){
$keys = array_slice($value,0,1);
$values = array_slice($value,1);
foreach($values as $val){
$result[$key][] = array_combine($keys[0],$val);
}
}
See demo here
You may use nested array_map calls. Somehow like this:
$result = array_map(
function ($subarr) {
$names = array_shift($subarr);
return array_map(
function ($el) use ($names) {
return array_combine($names, $el);
},
$subarr
);
},
$array
);
Demo
Something like this should work:
$newArray = array();
foreach ($arrayData as $section => $list) {
$newArray[$section] = array();
$count = count($list);
for ($x = 1; $x < $count; $x++) {
$newArray[$section][] = array_combine($list[0], $list[$x]);
}
}
unset($arrayData, $section, $x);
Demo: http://ideone.com/ZmnFMM
Probably a little late answer, but it looks more like your tried solution
//Films,Games // Row Data
foreach ($arrayData as $type => $value)
{
$key1 = $value[0][0]; // Get the Name Key
$key2 = $value[0][1]; // Get the Rating Key
$count = count($value) - 1;
for ($i = 0; $i < $count; $i++)
{
/* Get the values from the i+1 row and put it in the ith row, with a set key */
$arrayData[$type][$i] = array(
$key1 => $value[$i + 1][0],
$key2 => $value[$i + 1][1],
);
}
unset($arrayData[$type][$count]); // Unset the last row since this will be repeated data
}
I think this will do:
foreach($arrayData as $key => $array){
for($i=0; $i<count($array[0]); $i++){
$indexes[$i]=$array[0][$i];
}
for($i=1; $i<count($array); $i++){
for($j=0; $j<count($array[$i]); $j++){
$temp_array[$indexes[$j]]=$array[$i][$j];
}
$new_array[$key][]=$temp_array;
}
}
print_r($new_array);
EDIT: tested and updated the code, works...
Anyone know what I'm doing wrong?
From the following array I want to count the the number of times "Friday" occurs.
Array (
[404979509517702] => Array (
[0] => 235
[1] => 04:10,Friday
)
[404045862944400] => Array (
[0] => 192
[1] => 23:52,Wednesday
)
[20403274909688162] => Array (
[0] => 186
[1] => 22:21,Tuesday
)
[202735273075459] => Array (
[0] => 336
[1] => 04:29,Tuesday
)
[652948031457462] => Array (
[0] => 410
[1] => 06:22,Monday
)
[2606749954978] => Array (
[0] => 312
[1] => 05:01,Saturday
)
[755318061725] => Array (
[0] => 384
[1] => 04:51,Friday
)
)
This is what I'm doing:
$friday = array();
foreach ($the_array as $friday){
$the_array = explode(',', $friday[1]);
$the_array[$the_array [1]] += ($friday[1]);
}
print_r($friday);
This way, I get the wrong number of "Friday" occurrences.
Any idea what I'm doing wrong or if there is a more elegant solution to that?
I would try something like the following if all you want to do is count occurrences:
$friday_count = 0;
foreach($the_array as $record) {
// Search for "Friday"
if(stristr($record[1],'Friday') !== false) {
$friday_count++; // increment count
}
}
$fridayCount = 0;
foreach ($the_array as $friday){
$friArr = explode(',', $friday[1]);
if($friArr[1] == "Friday") {
$fridayCount++;
}
}
echo ($fridayCount);
The code should be:
$fridayCount = 0;
foreach ($the_array as $friday){
if(strrpos($friday[1], 'Friday') !== false) {
$fridayCount++;
}
}
echo $fridayCount;
I think you might be complicating this unnecessarily. You should be able to test for 'Friday' and increment a counter ($friday, here) as follows:
$friday = 0;
foreach ($the_array as $element){
if (array_pop(explode(',', $element[1])) == 'Friday') {
$friday++;
}
}
echo $friday;
$cnt = 0;
foreach ($the_array as $friday){
if (explode(',', $friday[1]) == 'Friday') {
$cnt++;
}
}
echo $cnt;