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/>';
}
}
Related
How to make this array from foreach in my code?
Below is my code
Array
(
[0] => Array
(
[day] => 2016-11-21
)
[1] => Array
(
[day] => 2016-11-22
)
[2] => Array
(
[day] => 2016-11-23
)
[3] => Array
(
[day] => 2016-11-24
)
[4] => Array
(
[day] => 2016-11-25
)
)
Below is my code but the result is different.
The result is from foreach and I want it to be an array, just like the array above.
foreach ($data['my_undertime'] as $undertime_row) {
$datetimein = new DateTime($undertime_row->timein);
$datetimein->format('h:m:s');
$datetimeout = new DateTime($undertime_row->timeout);
$datetimeout->format('h:m:s');
$items = array();
if ($datetimein->format('h:m:s') > $undertime_row->beg_time && $datetimeout->format('h:m:s') < $undertime_row->end_time) {
// echo $undertime_row->day;
$items[]['day'] = $undertime_row->day;
}
echo "<pre>";
print_r($items);
}
Please help.
Thanks.
The problem is in the line $items = array();.
As it is now, $item is recreated on each loop. After the foreach it ends up remembering only the last iteration of the loop.
If you want to keep all the data in $item then move this line of code before the foreach loop.
$items = array();
$i = 0;
foreach ($data['my_undertime'] as $undertime_row) {
$datetimein = new DateTime($undertime_row->timein);
$datetimein->format('h:m:s');
$datetimeout = new DateTime($undertime_row->timeout);
$datetimeout->format('h:m:s');
if ($datetimein->format('h:m:s') > $undertime_row->beg_time && $datetimeout->format('h:m:s') < $undertime_row->end_time) {
// echo $undertime_row->day;
$items[$i]['day'] = $undertime_row->day;
}
$i++;
}
echo "<pre>";
print_r($items);
try this
i have two arrays
$value_array = array('50','40','30','20','10');
$customer = array('300','200','100');
i want to distribute the value array to customers based on the value of customers that is taken as limit.adding values by checking it wont cross the limit that is 300 , 200 and 100.
but customer array not working one direction it should work first forward and then backward like that
i want to produce an array in form of
Array
(
[0] => Array
(
[0] => 50
)
[1] => Array
(
[0] => 40
[1] => 10
)
[2] => Array
(
[0] => 30
[1] => 20
)
)
After completing customer loop first time it should start from last to first. both array count will change , i mean count.
value array should check 50 -> 300 , 40->200, 30->100 then from last ie, 20 ->100, 10->200 etc.
I tried like
$i = 0;
while($i < count($customer)){
foreach($value_array as $k=>$value){
$v = 0;
if($value <= $customer[$i]){
$customer2[$i][] = $value;
unset($value_array[$k]);
$v = 1;
}
if($v ==1){
break;
}
}
//echo $i."<br/>";
if($i == (count($customer)-1) && (!empty($value_array))){
$i = 0;
$customer = array_reverse($customer, true);
}
$i++;
}
echo "<pre>";
print_r($customer2);
$valueArray = array('50','40','30','20','10','0','-11');
$customer = array('300','200','100');
function parse(array $valueArr, array $customerArr)
{
$customerCount = count($customerArr);
$chunkedValueArr = array_chunk($valueArr, $customerCount);
$temp = array_fill(0, $customerCount, array());
$i = 0;
foreach ($chunkedValueArr as $item) {
foreach ($item as $key => $value) {
$temp[$key][] = $value;
}
$temp = rotateArray($temp);
$i++;
}
// if $i is odd
if ($i & 1) {
$temp = rotateArray($temp);
}
return $temp;
}
function rotateArray(array $arr)
{
$rotatedArr = array();
//set the pointer to the last element and add it to the second array
array_push($rotatedArr, end($arr));
//while we have items, get the previous item and add it to the second array
for($i=0; $i<sizeof($arr)-1; $i++){
array_push($rotatedArr, prev($arr));
}
return $rotatedArr;
}
print_r(parse($valueArray, $customer));
returns:
Array
(
[0] => Array
(
[0] => 50
[1] => 0
[2] => -11
)
[1] => Array
(
[0] => 40
[1] => 10
)
[2] => Array
(
[0] => 30
[1] => 20
)
)
I need to fetch data from the array that I got through
print_r($result);
The array I got is
Array
(
[0] => Array
(
[id] =>
[Location] => Array
(
[img] => 177223
[name] =>
)
[Max] =>
[Total] =>
[Description] => Array
(
[Pre] =>
[Updated] =>
[Program] => Array
(
[Schedule] =>
)
)
[Staff] => Array
(
[FirstName] =>
)
)
)
I used this code
if (!empty($result))
{
foreach ($result as $res)
{
$Max = $res['Max'];
echo $Max;
echo "<br>";
if(isset($res['Location']))
{
foreach($res['Location'] as $loc)
{
$img= $loc['img'];
echo $img;
echo "<br>";
}
}
}
}
I am getting correct value for the first array (I.e Max etc) but not for Location, Description and Staff, can anyone correct my code
Location is not an array of arrays.
It is just an associative array.
if (!empty($result)) {
foreach ($result as $res) {
$Max = $res['Max'];
echo $Max;
echo "<br>";
if(isset($res['Location'])) {
$img= $res['Location']['img'];
echo $img;
echo "<br>";
}
}
}
You dont need to foreach through location, just access it's elements directly:
if(isset($res['Location']))
{
$img= $res['Location']['img'];
echo $img;
echo "<br>";
}
Or somethig like that.
I have a following array
Array
(
[0] => Array
(
[0] =>
)
[1] => Array
(
[0] => flatrate
[1] => flatrate3
[2] => freeshipping
)
[2] => Array
(
[0] => flatrate
[1] => flatrate2
[2] => flatrate3
[3] => flatrate4
[4] => freeshipping
)
)
Now, i need to match some element in this like
if freeshipping in above array . it should echo yes else no.
Please suggest, how can i do this.
for($i = 0; count($array); $i++){
if(in_array($string, $array[$i]) echo true;
}
you can do simple code like that :
foreach ($array as $key) {
foreach ($key as $value) {
if($value == "your condition")
{
echo 'yes';
}else{
echo 'no';
}
}
}
try it.
Create A function
function checkfreeShipping($arrData)
{
foreach($arrData as $data){
if(in_array('freeshipping',$data))
return true;
}
}
Call defined function to check Shipping -
if(checkfreeShipping($cartArr)){
echo "free Shipping";
}
else{
echo "Paid Shipping";
}
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...