two array foreach echo - php

I am having trouble with showing my data. i have 2 arrays, i want to check it if array 2 is equal to array 1. in the sample array below the output should be like this. Any answers would be appreciated. i am a newbie
100000, 010000, 010000, 000001, 000001, 100000, 100000
array 1 data
array(6) {
[0]=>
object(stdClass)#370 (1) {
["id"]=>
string(1) "1"
}
[1]=>
object(stdClass)#369 (1) {
["id"]=>
string(1) "2"
}
[2]=>
object(stdClass)#368 (1) {
["id"]=>
string(1) "3"
}
[3]=>
object(stdClass)#367 (1) {
["id"]=>
string(1) "4"
}
[4]=>
object(stdClass)#366 (1) {
["id"]=>
string(1) "5"
}
[5]=>
object(stdClass)#365 (1) {
["id"]=>
string(1) "6"
}
}
array 2 data
array(7) {
[0]=>
object(stdClass)#354 (1) {
["stage_id"]=>
string(1) "1"
}
[1]=>
object(stdClass)#355 (1) {
["stage_id"]=>
string(1) "2"
}
[2]=>
object(stdClass)#353 (1) {
["stage_id"]=>
string(1) "2"
}
[3]=>
object(stdClass)#352 (1) {
["stage_id"]=>
string(1) "6"
}
[4]=>
object(stdClass)#378 (1) {
["stage_id"]=>
string(1) "6"
}
[5]=>
object(stdClass)#377 (1) {
["stage_id"]=>
string(1) "1"
}
[6]=>
object(stdClass)#376 (1) {
["stage_id"]=>
string(1) "1"
}
}
This is what i have tried and doesnt work. it only shows 100000
foreach ($this->stage as $index => $object)
{
if (isset($this->items[$index]) && $object->id == $this->items[$index]->stage_id)
{
echo 1;
}
else
{
echo 0;
}
}

Related

Combine Similar Strings Value into Unique One

I'm fetching a post meta from Wordpress CPT which's returning multiple strings form individually instead of array as shown below:
$data = $calendar->data();
foreach ($data as $event) {
$days = get_post_meta( $event->ID, 'sp_day', true );
var_dump($days);
}
string(1) "1" string(1) "1" string(1) "1" string(1) "1" string(1) "1"
string(1) "1" string(1) "2" string(1) "2" string(1) "2" string(1) "2"
string(1) "2" string(1) "2" string(1) "3" string(1) "3" string(1) "3"
string(1) "3" string(1) "3" string(1) "3" string(1) "4" string(1) "4"
string(1) "4" string(1) "4" string(1) "4" string(1) "4" string(0) ""
string(0) "" string(0) "" string(0) "" string(0) "" string(0) ""
I tried to convert it into array like $days = array(get_post_meta( $event->ID, 'sp_day', true )); the dump values were:
array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" }
array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" }
array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "2" }
array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" }
array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" }
array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "4" }
array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" }
array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" }
array(1) { [0]=> string(0) "" } array(1) { [0]=> string(0) "" }
Than I tried the array_merge() and array_unique()
None of the above steps have solved the issue. As you can see we have repeated values multiple times instead I want them unique to get each value only once such "1, 2, 3, 4, 5, etc."
Suggestions are highly appreciated
you can add an empty array before the for loop and inside the for loop you can check if the item exist in the array, if it doesn't exist you add the item to the array.
$data = $calendar->data();
$result = [];
foreach ($data as $event) {
$days = get_post_meta( $event->ID, 'sp_day', true );
if(! in_array($days, $result)) {
$result[] = $days;
}
}
var_dump($result);

making new arrays in 1 multidimensional array on looping statement codeigniter

guys lets play in multidimensional array little bit :p
lets said i have $filter which it contain multidimensional array :
array(2) {
["time"]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
}
["people"]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
[3]=>
string(1) "4"
[4]=>
string(1) "5"
}
}
from the array it should be return :
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "1"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "2"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "3"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "4"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "5"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "1"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "2"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "3"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "4"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "5"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "1"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "2"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "3"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "4"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "5"
["promo_id"]=>
string(1) "1"
}
actually i could get that result with this following code :
foreach ($filter['time'] as $row){
foreach ($filter['people'] as $item){
$new_array['time'] = $row;
$new_array['people'] = $item;
$new_array['promo_id'] = $promo_id;
vd($new_array,"new");
}
}
but i cant use that following code, because it using $filter['time'] and $filter['people'], how about if the time or people is a random string?
so can you show me guys, to looping it from $filter?
as like this :
foreach ($filter as $key => $row){
}
thank you (:
function combination($arr,$res,$completed_key){
$array_key = array_keys($arr);
$last_array_key = array_pop($array_key);
$start = false;
$temp = $res;
$res=array();
$i=0;
foreach($arr as $key=>$row){
if(!in_array($key,$completed_key)&& $start==false){
array_push($completed_key,$key);
$start=true;
}
if($start){
if(count($temp)==0){
foreach($row as $key2 => $row2){
$res[][$key]=$row2;
}
}else{
foreach($temp as $temp_key=>$temp_row){
foreach($row as $key2 => $row2){
$res[$i]=$temp_row;
$res[$i][$key]=$row2;
$i++;
}
}
}
}
if($start){
break;
}
}
if(!in_array($last_array_key,$completed_key)){
return $this->combination($arr,$res,$completed_key,$debugger);
}else{
return $res;
}
}
This function will return you want.
$filter["time"]=array(1,2,3);
$filter["people"]=array(1,2);
$filter["size"]=array("a","b");
$res=$this->combination($filter,array(),array());
I'll explain later.

unexpected changes - overriding object inside foreach php

I've got array of object form database like below:
array(3) {
[1]=>
array(2) {
[0]=>
object(stdClass)#99 (3) {
["id"]=>
string(2) "42"
["name"]=>
string(4) "NAME1"
["type"]=>
string(1) "6"
}
[1]=>
object(stdClass)#98 (3) {
["id"]=>
string(3) "146"
["name"]=>
string(3) "STH1"
["type"]=>
string(1) "2"
}
}
[2]=>
array(2) {
[0]=>
object(stdClass)#97 (3) {
["id"]=>
string(2) "422"
["name"]=>
string(4) "NAME2"
["type"]=>
string(1) "3"
}
[1]=>
object(stdClass)#96 (3) {
["id"]=>
string(3) "16"
["name"]=>
string(3) "STH2"
["type"]=>
string(1) "2"
}
}
[3]=>
array(2) {
[0]=>
object(stdClass)#95 (3) {
["id"]=>
string(2) "11"
["name"]=>
string(4) "NAME3"
["type"]=>
string(1) "5"
}
[1]=>
object(stdClass)#94 (3) {
["id"]=>
string(3) "69"
["name"]=>
string(3) "STH3"
["type"]=>
string(1) "3"
}
}
}
And if i want to add the same object to the next array and change value of its type, i override the current object. How can i fix it? My foreach loop below:
foreach($events as $key => $event){
foreach($event as $k => $v){
if($v->type == 6){
$v->type = "0";
$events[$key+1][] = $v;
$v->type = "6";
}
}
}
If my guess what you are trying to achieve is right i would go like this
foreach($events as $key => $event){
foreach($event as $k => $v){
if($v->type == 6){
$tmp = $v;
$tmp->type="0";
$events[$key+1][] = $tmp;
}
}
}

Get specific value from PHP array using foreach key value

I have the following array:
array(15) {
[0]=> object(stdClass)#317 (2) { ["id"]=> string(1) "2" ["value"]=> string(1) "1" }
[1]=> object(stdClass)#316 (2) { ["id"]=> string(1) "3" ["value"]=> string(531) "awfaww" }
[2]=> object(stdClass)#315 (2) { ["id"]=> string(1) "4" ["value"]=> string(1) "1" }
[3]=> object(stdClass)#318 (2) { ["id"]=> string(1) "5" ["value"]=> string(1) "1" }
[4]=> object(stdClass)#319 (2) { ["id"]=> string(1) "6" ["value"]=> string(1) "1" }
[5]=> object(stdClass)#320 (2) { ["id"]=> string(1) "7" ["value"]=> string(1) "1" }
[6]=> object(stdClass)#321 (2) { ["id"]=> string(1) "8" ["value"]=> string(1) "1" }
[7]=> object(stdClass)#322 (2) { ["id"]=> string(2) "30" ["value"]=> string(8) "12:30:02" }
[8]=> object(stdClass)#323 (2) { ["id"]=> string(2) "31" ["value"]=> string(8) "18:12:00" }
[9]=> object(stdClass)#324 (2) { ["id"]=> string(2) "11" ["value"]=> string(10) "2014-06-17" }
[10]=> object(stdClass)#325 (2) { ["id"]=> string(2) "12" ["value"]=> string(10) "2014-06-26" }
[11]=> object(stdClass)#326 (2) { ["id"]=> string(2) "14" ["value"]=> string(1) "2" }
[12]=> object(stdClass)#327 (2) { ["id"]=> string(2) "15" ["value"]=> string(1) "2" }
[13]=> object(stdClass)#328 (2) { ["id"]=> string(2) "16" ["value"]=> string(1) "4" }
[14]=> object(stdClass)#329 (2) { ["id"]=> string(2) "17" ["value"]=> string(1) "5" }
}
I would like to get a specific value from this array using the ID. For example, if the ID: 11 is found in the array I want to retrieve its value. How can I do this?
Try something like this:
<?php
function findById($array, $id) {
foreach ($array as $value) {
if ($value->id == $id) {
return $value->value;
}
}
return null;
}
$result = findById($yourArray, 11);
?>
if the array is static for each run - i'd suggest changing the array into "key"=>"value" array, using this:
$new_arr = array();
foreach($original_array as $object) {
$new_arr[$object->id] = $object->value;
}
and then you can just use $new_arr[id], instead of searching the whole original array each time.
You can use array_filter:
array_filter($arr, function($i) { return $i->id == '11'; });
See Documentation

How do I sort this array

I grouped an array using the following script
$grouped_array = array();
foreach($ungrouped_array as $item) {
//group them by id
$grouped_array[$item['id']][] = $item;
}
Now this grouped array looks like this
array(3) {
[1]=>
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1"
}
[1]=>
array(1) {
["id"]=>
string(1) "1"
}
}
[6]=>
array(1) {
[0]=>
array(1) {
["id"]=>
string(1) "6"
}
}
[2]=>
array(4) {
[0]=>
array(1) {
["id"]=>
string(1) "2"
}
[1]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "1"
}
[2]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "2"
}
[3]=>
array(1) {
["id"]=>
string(1) "2"
}
}
}
I have deleted the most part of the array to make it shorter but there is no [0] field in this grouped array
All array fields are given the name of [id]'s value. I have no problem with that, I just have to short it again by [ID]
any suggestion will be great.
This should work to get 1, 2, 6:
<?php
$grouped_array = array();
foreach($ungrouped_array as $item) {
$grouped_array[$item['id']][] = $item;
}
// sort by key.
ksort( $grouped_array, SORT_NUMERIC );
print_r( $grouped_array );

Categories