array(2) {
[0]=>
object(stdClass)#144 (7) {
["id"]=>
string(1) "2"
["name"]=>
string(8) "name1"
["value"]=>
string(22) "Lorem Ipsum Dolar Amet"
["type"]=>
string(8) "textarea"
["group"]=>
string(1) "1"
["published"]=>
string(1) "1"
["ordering"]=>
string(1) "1"
}
[1]=>
object(stdClass)#145 (7) {
["id"]=>
string(1) "4"
["name"]=>
string(6) "Link1"
["value"]=>
string(36) "abcabcab"
["type"]=>
string(4) "link"
["group"]=>
string(1) "1"
["published"]=>
string(1) "1"
["ordering"]=>
string(1) "2"
}
}
I want to print only "value" (abcabcab) of id=4. How can I achieve this?
foreach($array as $row){
if($row['id']==4){
print($row['value']);
}
}
foreach ($array as $entry) {
if ($entry['id'] == 4)
echo $entry['value'];
}
array_walk($a, function($el){if($el->id === 4){print $el->value;}});
this works:
foreach ($array as $entry) {
if ($entry->id == 4)
echo $entry->value;
}
Thanks!
Related
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.
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;
}
}
}
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
What would be my best option to get the data out of this array?
array(4) {
[0]=> array(10) {
["id"]=> string(3) "158"
["name"]=> string(8) "Tractors"
["parent_id"]=> string(1) "0"
["image_id"]=> string(2) "37"
["blurb"]=> string(17) "Agrilife Tractors"
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "1"
["sorder"]=> string(1) "0"
["state"]=> string(1) "1"
}
[1]=> array(10) {
["id"]=> string(3) "159"
["name"]=> string(8) "Ride Ons"
["parent_id"]=> string(1) "0"
["image_id"]=> string(2) "74"
["blurb"]=> string(0) ""
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "2"
["sorder"]=> string(1) "1"
["state"]=> string(1) "1"
}
[2]=> array(10) {
["id"]=> string(3) "160"
["name"]=> string(9) "Machinery"
["parent_id"]=> string(1) "0"
["image_id"]=> string(2) "14"
["blurb"]=> string(0) ""
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "3"
["sorder"]=> string(1) "2"
["state"]=> string(1) "1"
}
[3]=> array(10) {
["id"]=> string(3) "161"
["name"]=> string(17) "Outdoor Equipment"
["parent_id"]=> string(1) "0"
["image_id"]=> string(3) "114"
["blurb"]=> NULL
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "5"
["sorder"]=> string(1) "3"
["state"]=> string(1) "1"
}
}
Tractors
My HTML looks like this I am trying to foreach to get all of the relevant data out so I can echo when or where needed.
HTML:
foreach($assoc_categories as $assoc_cat)
{
// Page load - does assoc exist?
$checked_state = "";
$does_assoc_exist = $this->Ps_products_model->brand_specific_cat_assoc_exist($brand_id, $assoc_cat['id']);
if($does_assoc_exist == "1")
{
$checked_state = " checked='checked'";
}
?>
<div>
<input type="checkbox" name="product_category" class="product_category_selector" id="product_category_<?php echo $assoc_cat['id']; ?>" data-id="<?php echo $assoc_cat['id']; ?>" <?php echo $checked_state; ?> /> <?php echo $assoc_cat['name']; ?>
</div>
<input class="order" type="input" />
<?php
}
?>
To dump all the values, you'd need nested foreach like this:
foreach ($original_array as $sub_array) {
foreach ($sub_array as $key=>$value) {
echo $key.' '.$value.'<br>';
}
}
To get just one value, you need to access it with its address. It may not be set, so check first:
foreach ($original_array as $sub_array) {
// Say you want all the `name`s
if (isset($sub_array['name'])) {
echo $sub_array['name'].'<br>';
}
}
Given this array:
array(1) {
[0]=>
array(2) {
["Project"]=>
array(5) {
["id"]=>
string(1) "2"
["user_id"]=>
string(2) "21"
["customer_id"]=>
string(1) "4"
["name"]=>
string(15) "WordPress Theme"
["created"]=>
string(19) "2011-09-26 21:30:38"
}
["Track"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "7"
["user_id"]=>
string(2) "21"
["project"]=>
string(1) "2"
["customer"]=>
string(1) "4"
["title"]=>
string(7) "Backend"
["notes"]=>
string(0) ""
["created"]=>
string(19) "2011-09-28 22:21:22"
["Lapse"]=>
array(2) {
[0]=>
array(5) {
["id"]=>
string(1) "4"
["track_id"]=>
string(1) "7"
["start"]=>
string(19) "2011-09-28 22:22:21"
["stop"]=>
string(19) "2011-09-28 22:22:30"
["created"]=>
string(19) "2011-09-28 22:22:21"
}
[1]=>
array(5) {
["id"]=>
string(1) "3"
["track_id"]=>
string(1) "7"
["start"]=>
string(19) "2011-09-28 22:22:07"
["stop"]=>
string(19) "2011-09-28 22:22:12"
["created"]=>
string(19) "2011-09-28 22:22:07"
}
}
}
}
}
}
How would i group by Day in the Lapse Array with PHP? This may have been easier to do directly with MySQL but i'm using CakePHP's recursive function and i cant figure out how to use Group By with that!
$list = array();
function extractByDates($arr) {
foreach ($arr as $key => $v)
if (is_array($v))
function extractByDates($v);
else if ($key == 'created')
$list[$v] = $v;
}
extractByDates($yourGivenArray);
I not tested!