How to get cat_id ?
Array
(
[0] => stdClass Object
(
[id] => 17
[res_id] => 10
[cat_id] => 3
)
[1] => stdClass Object
(
[id] => 18
[res_id] => 10
[cat_id] => 4
)
[2] => stdClass Object
(
[id] => 52
[res_id] => 19
[cat_id] => 1
)
[3] => stdClass Object
(
[id] => 53
[res_id] => 19
[cat_id] => 3
)
[4] => stdClass Object
(
[id] => 54
[res_id] => 19
[cat_id] => 4
)
I want cat_id from all stdClass Object array.
how can i get this ?
Can anyone help me how do I retrieve the value of cat_id?
Thanks.
Try this
$Your_array
foreach($Your_array AS $Cat){
echo $Cat->cat_id;
}
You can use array_map. For example, if your source array is $array:
$result = array_map(function($x){
return $x->cat_id;
}, $array);
print_r($result);
That will give you:
Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 3
[4] => 4
)
Demo
$array = array();
$object = new stdClass;
$object->id= "ID";
$object->res_id = "RES_ID";
$object->cat_id = "CAT_ID";
$array[] = $object;
To Access to object attribute:
echo $array[0]->cat_id;
You can replace 0 with index and iterate it in array:
for($index=0;$index<count($array);$index++){
if(isset($array[$index]->cat_id)){
// Do something...
}
}
Object properties in PHP are accessed using the -> syntax. To get the cat_id property of each item in the array, you could loop through the array to get each item one by one, and store the cat_id property of the objects in a new array, like so:
$cat_ids = array();
foreach($array as $item){
$cat_ids[] = $item->cat_id;
}
print_r($cat_ids);
Gives:
Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 3
[4] => 4
)
If you don't need all the cat_ids, but just the one for a specific object in your original array, you can use
$array[2]->cat_id;
Which would get the cat_id property of the 3rd item in your array.
Related
I have three arrays first array include ids and employees name and second array have monthly collection with employee ids and third array have daily collection with employee id and daily collection I want to merge these array with ids and name and dcollection and monthly collection but the desired output is not coming here my first array $ids is
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Rohit
)
[1] => stdClass Object
(
[id] => 2
[name] => Emop1
)
[2] => stdClass Object
(
[id] => 3
[name] => Pankaj
)
[3] => stdClass Object
(
[id] => 4
[name] => tejpal singh
)
)
second array $q1 is
Array
(
[0] => stdClass Object
(
[name] => Rohit
[id] => 1
[mcollecton] => 100
)
[1] => stdClass Object
(
[name] => Emop1
[id] => 2
[mcollecton] => 1222
)
)
third array $q2 is
Array
(
[0] => stdClass Object
(
[name] => Rohit
[id] => 1
[dcollecton] => 300
)
[1] => stdClass Object
(
[name] => Emop1
[id] => 2
[dcollecton] => 150
)
)
so far what I have tried
$new_array = array();
foreach($ids as $k) {
$q1n = array("id"=>$k->id,"name"=>$k->name);
foreach($q1 as $k1) {
if($k->id==$k1->id){
$mc = array("mc"=>$k1->mcollecton);
array_merge($q1n,$mc);
}
}
foreach($q2 as $k1){
if($k->id==$k1->id){
$dc = array("dc"=>$k1->dcollecton);
array_merge($q1n,$dc);
}
}
$a = array_merge($q1n,$mc);
$av = array_merge($q1n,$dc);
array_push($new_array,$q1n);
}
but the output is coming as
Array
(
[0] => Array
(
[id] => 1
[name] => Rohit
)
[1] => Array
(
[id] => 2
[name] => Emop1
)
[2] => Array
(
[id] => 3
[name] => Pankaj
)
[3] => Array
(
[id] => 4
[name] => tejpal singh
)
)
I want the output be like
Array
(
[0] => Array
(
[id] => 1
[name] => Rohit
[mcollection] => 100
[dcollection] => 300
)
[1] => Array
(
[id] => 2
[name] => Emop1
[mcollection] => 1222
[dcollection] => 150
)
[2] => Array
(
[id] => 3
[name] => Pankaj
[mcollection] => 0
[dcollection] => 0
)
[3] => Array
(
[id] => 4
[name] => tejpal singh
[mcollection] => 0
[dcollection] => 0
)
)
So I have tried many times but the desired output is not coming . please help me out how to get the desired output.
It seemed like that answer could be modified, or put in a function that you could call multiple times if needed to combine more than two arrays.
There's probably cleaner ways to handle this with array functions like array_merge or array_walk, but this is the general idea of how I might approach it. I haven't tested this, but maybe it's useful.
foreach($first as $key1 => $value){
foreach($second as $key2 => $value2){
// match the ids and check if array key exists on first array
if($value['id'] === $value2['id'] && empty($first[$key2])){
$first[$key][$key2] = $value2;
}
}
}
EDIT: Based on the answer you posted vs the question you asked, are you incrementing the collection numbers or just setting them? In other words why use +=? You should also be able to remove array_merge and array_push.
Below is geared more towards what you're trying to do. I haven't tested this either, but if you run into errors, post your code with the errors returned so that it's easier to debug:
foreach($ids as $k)
{
$thisArray = $newArray[] = array("id"=>$k->id,"name"=>$k->name);
foreach($q1 as $k1)
{
if($k->id == $k1->id && !empty($k1->mcollecton))
{
$thisArray['mc'] = $k1->mcollecton;
}
}
foreach($q2 as $k2)
{
if($k->id == $k2->id && !empty($k2->dcollecton))
{
$thisArray['dc'] = $k2->dcollecton;
}
}
}
// This should have both new collections fields on all array items
print_r($newArray)
I am looking to convert a multi dimensional array into another multidimensional array using a recursive function.
Source array :
Array
(
[1] => Array
(
[id] => 1
[source_name] => kk56ca1d0f2378f
[company_id] => 1
[lft] => 1
[rgt] => 18
[parent_id] => 0
[children] => Array
(
[2] => Array
(
[id] => 2
[source_name] => kk56ca1d17f3f63
[company_id] => 1
[lft] => 2
[rgt] => 3
[parent_id] => 1
[children] => Array
(
)
)
[3] => Array
(
[id] => 3
[source_name] => kk56ca1d1ebe975
[company_id] => 1
[lft] => 4
[rgt] => 13
[parent_id] => 1
[children] => Array
(
[6] => Array
(
[id] => 6
[source_name] => kk56ca1fc882ac0
[company_id] => 1
[lft] => 5
[rgt] => 10
[parent_id] => 3
[children] => Array
(
)
)
)
)
)
)
)
which I need to get into the format of
Array
(
[0] => Array
(
[id] => 1
[text] => kk56ca1d0f2378f
[parent_id] => 0
[nodes] => Array
(
[0] => Array
(
[id] => 2
[text] => kk56ca1d17f3f63
[parent_id] => 1
[nodes] => Array
(
)
)
[1] => Array
(
[id] => 3
[text] => kk56ca1d1ebe975
[parent_id] => 1
[nodes] => Array
(
[0] => Array
(
[id] => 6
[text] => kk56ca1fc882ac0
[parent_id] => 3
[nodes] => Array
(
)
)
[1] => Array
(
[id] => 15
[text] => kk
[parent_id] => 3
[nodes] => Array
(
)
)
)
)
)
)
)
I have been trying for hours and getting nowhere with this. Any help would be really appreciated.
The source array has associative indexes (though they are numbers) and the destination array has numerical indexes. Besides this, just need to remove a few indexes and change names of a few.
EDIT :
Specific changes :
change index name source_name to text
change index name children to nodes
unset indexes lft, rgt, company_id
I do not have much experience with recursion so I have trying fruitlessly.
This is what I could come up with :
// pass array of nodes
function convert_array($from){
// this is a node
if(isset($from['source_name']))
{
$temp = array();
$temp['id'] = $from['id'];
convert_array($from['children']);
}
// this is an array of nodes
else
{
foreach($from as $arr)
{
$ret = convert_array($arr);
print_r($ret);
}
}
}
But I am not able to understand what data to be returned and how the new array builds up from the return values.
Here the working function:
function convert_array( $array )
{
$retval = array();
foreach( $array as $row )
{
$child = array();
$child['id'] = $row['id'];
$child['text'] = $row['source_name'];
$child['parent_id'] = $row['parent_id'];
if( count( $row['children'] ) )
{ $child['nodes'] = convert_array( $row['children'] ); }
else
{ $child['nodes'] = array(); }
$retval[] = $child;
}
return $retval;
}
3v4l demo
I think it is self-explanatory, BTW: we init an empty array ($retval), then we perform a foreach loop through all array argument: for each element, we init a new array and we add it id, source_name as text and parent_id; if the children index has elements, we perform a recursive call to fill nodes array index, otherwise we set it to empty array.
I want to merge the 2 arrays of objects based on the 'id' field of Array1 and the 'itemVendorCode' of Array2. I also wanted to remove from the resulting arrays of object anything that didn't match.
Array1:
Array
(
[0] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
)
[1] => stdClass Object
(
[id] => 89-575-2354
[qty] => 24
[price] => 230.35
)
[2] => stdClass Object
(
[id] => 89-605-1250
[qty] => 2
[price] => 230.35
)
)
Array2:
Array
(
[0] => Item Object
(
[internalId] => 14062
[itemVendorCode] => 89-605-1250
)
[1] => Item Object
(
[internalId] => 33806
[itemVendorCode] => 89-575-2354
)
[2] => Item Object
(
[internalId] => 64126
[itemVendorCode] => 26-295-1006
)
)
I was able to solve this by this code:
$indexed = array();
foreach($itemsArray as $value) {
$indexed[$value->itemVendorCode] = $value;
}
$results = array();
foreach($vendorItems as $obj) {
$value = $indexed[$obj->id];
if (isset($value)) {
foreach($value as $name => $val) {
$obj->$name = $val;
array_push($results, $obj);
}
}
}
print_r($results);
credits to the original poster. I just modified it a bit,
I was able to get the result like this:
Array
(
[0] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
[internalId] => 2035
[itemVendorCode] => 10-423-1176
)
[1] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
[internalId] => 2035
[itemVendorCode] => 10-423-1176
)
[2] => stdClass Object
(
[id] => 14-102-1010
[qty] => 16
[price] => 3.2
[internalId] => 57033
[itemVendorCode] => 14-102-1010
)
)
I think you will have to use array_map function which provides you a callback function to execute on array(s).
In the callback function:
- declare your array1
- foreach the second array
- set an if statement to check that the current iteration with the id value matches the itemVendorCode of the array2 and return it
something like this:
// You have to specify to PHP to use a local copy of your $array2 to works with it into your callback
$cb = function ($obj1) use ($array2)
{
// you foreach this array
foreach ($array2 as $obj2) {
// if the value of id matches itemVendorCode
if ($obj1->id === $obj2->itemVendorCode) {
// you return the id
return $obj->id;
}
}
};
// this function will fill a new array with all returned data
$mergedArray = array_map($cb, $array1);
This code is a sample but doesn't provide you, your needled solution, try to update it to do what you exactly want ;)
I have an array which looks like this:
[0] => stdClass Object
(
[id] => 152
[type_id] => 5
[name] => test 1
)
[1] => stdClass Object
(
[id] => 153
[type_id] => 8
[name] => test 2
)
[2] => stdClass Object
(
[id] => 154
[type_id] => 13
[name] => test 3
)
Is there any way I can transform it into the array below?
[5] => stdClass Object
(
[id] => 152
[name] => test 1
)
[8] => stdClass Object
(
[id] => 153
[name] => test 2
)
[13] => stdClass Object
(
[id] => 154
[name] => test 3
)
Basically I changed the array key with the type_id value.
You should be able to unset the stdClass's properties as follows:
$newObject = array();
foreach ($oldObject as $object) {
$typeID = $object->type_id;
$newObject[$typeID] = $object;
unset($newObject[$typeID]->type_id);
}
This will iterate through your array of stdClass objects and save the entry into a new array. Afterwards, the property type_id will be deleted.
Pass it into a function that returns a new array:
function convert($arr)
{
$new_arr = array();
for ($i = 0; $i < count($arr); $i++)
{
$id = $arr[$i]['type_id'];
unset($arr[$i]['type_id']);
$new_arr[$id] = $arr[$i];
}
return $new_arr;
}
To replace a value, simply use: http://php.net/manual/en/function.unset.php and to add another value: http://php.net/manual/en/function.array-push.php
I have my array
Array
(
[0] => Dusche
[1] => Mobliert
)
And I have second array which is composed and looks like this:
[0] => Array
(
[id] => 1002
[attribute_id] => 65
[value_id] => 26815
[name] => Garten/-mitbenutzung
[order] => 0
)
[1] => Array
(
[id] => 1003
[attribute_id] => 65
[value_id] => 26811
[name] => Etagenheizung
[order] => 1
)
[2] => Array
(
[id] => 1004
[attribute_id] => 65
[value_id] => 26829
[name] => Balkon/Terrasse
[order] => 2
How can I search this second array with values from the first array and retrieve attribute_id from elements which have the same names?
PHP way:
filteredArray = array();
foreach ($secondArray as $type) {
if (in_array($type['name'], $firstArray)) {
$filteredArray[] = $type['attribute_id'];
}
}
Cake Set way, something along the lines of:
$filteredArray = array();
foreach ($firstArray as $keyword) {
$filteredArray = array_merge($filteredArray, Set::extract("/.[name=$keyword]/attribute_id", $secondArray));
}