Suppose you have the following array values assigned to a variable,
$erz = Array (
[0] => stdClass Object ( [id] => 43 [gt] => 112.5 )
[1] => stdClass Object ( [id] => 47 [gt] => 46 )
[2] => stdClass Object ( [id] => 48 [gt] => 23.75 )
[3] => stdClass Object ( [id] => 49 [gt] => 12.5 )
)
I need to be able to get the array index number given the id. So for instance I want to get 2 given id 48, or get 3 given id 49, etc. Is there a php command able to do this?
I dont think there is but its easy to set up your own function..
function findArrayIndex($arr, $searchId) {
$arrLen = count($arr);
for($i=0; $i < $arrLen; $i++) {
if($arr[$i][id] == $searchId) return $i;
}
return -1;
}
No, there is no such funcion. There is an array_search() actually, but you can't use it with objects. For example, here has been asked a simmilar question: PHP - find entry by object property from a array of objects
So you have to make your own loop:
$result = null;
$givenID = 43;
foreach ($erz as $key => $element)
{
if ($element->id == $givenID)
$result = $key;
}
Related
I'm just a beginner and would like to select a value of an array inside an object. I'm quite lost and don't know how to do.
ie : how to get de value of "thailande" inside this object ?
Forminator_Form_Entry_Model Object
(
[entry_id] => 42
[entry_type] => custom-forms
[form_id] => 24342
[is_spam] => 0
[date_created_sql] => 2020-07-02 11:42:21
[date_created] => 2 Juil 2020
[time_created] => 2 Juil 2020 # 11:42
[meta_data] => Array
(
[select-1] => Array
(
[id] => 87
[value] => thailande
)
[radio-1] => Array
(
[id] => 88
[value] => 1
)
[number-1] => Array
(
[id] => 89
[value] => 10
)
[_forminator_user_ip] => Array
(
[id] => 90
[value] => 84.101.156.169
)
)
[table_name:protected] => politis_5_frmt_form_entry
[table_meta_name:protected] => politis_5_frmt_form_entry_meta
)
thx a lot for your help.
It's fairly straightforward - you just go down the hierarchy one step at a time referencing the index you need.
So, assuming $obj in this example is an instance of Forminator_Form_Entry_Model then you would write
$obj->meta_data["select-1"]["value"]
which will point to the data you're looking for.
N.B. The ->index syntax is used to get properties of an object. the ["index"] syntax is used to get properties of an array.
You can try Callback Functions
function array_search_id($val_for_search, $array_data, $search_in_path='root') {
if(is_array($array_data) && count($array_data) > 0) { // if value has child
foreach($array_data as $key => $value) {
$paths_list = $search_in_path;
// Adding current key to search path
array_push($paths_list, $key);
if(is_array($value) && count($value) > 0) { // if value has child
$res = array_search_id($val_for_search, $value, $paths_list);//callback function
if ($res != null)
return $res;
}
else if($value == $val_for_search){
//if you wants path + result
return end($paths_list);
/*
//if you wants path
return join(" --> ", $paths_list);
*/
} //if value find in array return val
}
}
return null;
}
array_search_id('thailande', $your_array);
I have a multidimensional array. I want to get array element which value is greater than 2 and less than 17. My Array is given below:
Array
(
[4] => Array
(
[0] => 17
[1] => 15
[2] => 12
)
[5] => Array
(
[0] => 16
)
[2] => Array
(
[0] => 3
[1] => 1
)
[1] => Array
(
[0] => 2
)
)
I want output like below:
Array
(
[4] => Array
(
[0] => 17
[1] => 15
[2] => 12
)
[5] => Array
(
[0] => 16
)
[2] => Array
(
[0] => 3
[1] => 1
)
)
Please help me how can I do it easy & fast method.
You can use a nested array filter.
$result = array_filter($outer_array, function($inner_array) {
return array_filter($inner_array, function($number) {
return $number > 2 && $number < 17;
});
});
Then inner array filter will result in an empty array being passed to the outer array filter if no values are found in the specified range. The empty array will evaluate to false in the outer filter callback, eliminating that array from the result.
Demo at 3v4l.org.
Generally, we want you to show what you have tried before we'll write code for you, but this one's on the house. For future reference, include some code snippets along with your question to avoid getting downvoted.
$output = array();
for($i = 0; $i < count($arr); $i++)
{
$use = false;
for($j = 0; $j < count($arr[$i]); $j++)
{
if($arr[$i][$j] > 2 and $arr[$i][$j] < 17)
{
$use = true;
break;
}
}
if($use)
$output[] = $arr[$i];
}
return $output;
Good morning!
I'm using a SQL query to get category IDs from posts. Several post can have the same ID if they are from the same category. This is my array:
Array (
[0] => stdClass Object ( [catid] => 48 )
[1] => stdClass Object ( [catid] => 77 )
[2] => stdClass Object ( [catid] => 57 )
[3] => stdClass Object ( [catid] => 57 )
[4] => stdClass Object ( [catid] => 38 ))
Now I need to get the sum of all different catid's (in this case 4). I could achieve this by using "distinct" or a "group by" in my SQL, anyway I didnt because they mess up my results.
The problem is, that some catids should be replaced. For example is catid 48 'HDDs' and 77 is 'Storage' so 48 should be a part of 77 (So the sum is now 3). I already got an array of the catids that should be merged:
$catMergeArray = array(
38=>74,36=>46,34=>72,37=>73,40=>76,39=>75,103=>85,102=>81,53=>82,51=>80,50=>79,
55=>90,56=>91,57=>92,58=>93,55=>90,104=>68,106=>69,65=>70,66=>86,108=>95,48=>77,
172=>171,105=>170,111=>169);
by using
foreach ($result as $cats) {
$newarray[] = $cats->catid;
}
I changed my Array to
$newarray = Array ([0] => 48 [1] => 77 [2] => 57 [3] => 57 [4] => 38)
Then (like mentioned in $catMergeArray where it says 48=>77) remove the [0] => 48 value so the result should be $catids = Array ([0] => 77 [1] => 57 [2] => 38) so I can count the elements and have 3 as result to work with.
ummm have you tried using array_unique? http://php.net/manual/en/function.array-unique.php
or you can also group them into a unique array index like this:
$summary = array();
foreach( $catids as $catid => $cat){
$summary[$catid][] = $cat;
}
It'd probably be better to do it in php. Here's a rough idea and some pseudocode.
probably not very efficent nor elegent, but it should work.
$list_of_id = mysqli_fetch_array(query("distinct catId"));
for($i=0;$i<count($list_of_id);$i++){
$parent_cat_id = $catMergeArray[$list_od_id[$i]];
if ($parent_cat_id > 0){
for($j=0;$j<count($list_of_id);$j++){
if($list_of_id[$j]==$parent_cat_id){
unset($list_of_id[$i]);
break;
}
}
}
}
echo count($list_of_id);
EDIT: since OP doesn't seems to understand pseudocode...I'll make a php version... Surely it's clear enough?
Thanks for all the help. I now solved my problem with this:
foreach ($result as $value) {
$intArray[] = $value->catid;
}
$uniqueArray = array_unique($intArray);
$cUniqueArray = count($uniqueArray);
$cma = array(48=>77);
$lenght = $cUniqueArray-1;
for($i = 0; $i <= $lenght; $i++){
$key = array_search($uniqueArray[$i],$cma);
if(is_int($key)){
$uniqueArray[$i] = $key;
}
}
I am using Codeigniter and I have a function like this .
function total_income(){
$data['total_income'] = $this->mod_products->total_income();
echo"<pre>";
print_r($data['total_income']);
}
The above code return an array like this.
Array
(
[0] => stdClass Object
(
[sub_product_price] => 1000
[quantity] => 1
)
[1] => stdClass Object
(
[sub_product_price] => 1000
[quantity] => 1
)
[2] => stdClass Object
(
[sub_product_price] => 50
[quantity] => 15
)
[3] => stdClass Object
(
[sub_product_price] => 500
[quantity] => 5
)
)
Now I want to get the [sub_product_price] and multiply that value with [quantity] .Then I want to get the array_sum. I don't have an idea how to do that. Could some one help me It would be grate ,
Cheers!! Rob
$sum = 0;
foreach ($array as $obj) {
$sum += ($obj->quantity * $obj->sub_product_price);
}
Add the all values after multiplication in an empty array and use array_sum() to get the final value.
$temp = array();
foreach($data['total_income'] as $in)
{
$temp[] = $in->sub_product_price * $in->quantity;
}
$total = array_sum($temp);
Explanation:
array_sum() returns the sum of all the values of an array.
Example,
$array = array(4,5,6);
echo array_sum($array); // 15
I have some array containing other arrays:
Array
(
[0] => Slip Object
(
[userId:protected] => 1
[parentSlipId:protected] => 0
[id:protected] => 25
[madeDatetime:protected] => 2011-04-19 17:13:09
[stake:protected] => 34.00
[status:protected] => 6
)
[1] => Slip Object
(
[userId:protected] => 1
[parentSlipId:protected] => 0
[id:protected] => 25
[madeDatetime:protected] => 2011-04-19 17:13:09
[stake:protected] => 34.00
[status:protected] => 6
)
[2] => Slip Object
(
[userId:protected] => 1
[parentSlipId:protected] => 0
[id:protected] => 24
[madeDatetime:protected] => 2011-04-18 11:31:26
[stake:protected] => 13.00
[status:protected] => 6
)
)
What's the best way of counting unique arrays?
Off the top of my head you could try:
$hashes = array();
$uniques = 0;
foreach($array as $slip) {
$hash = sha1(serialize($slip));
if(!in_array($hash, $hashes)) {
++$uniques;
$hashes[] = $hash;
}
}
var_dump($uniques); // prints total number of unique objects.
Edit:
#biakaveron's idea looks better though and could be adapted to:
$uniques = count(array_unique($array, SORT_REGULAR));
var_dump($uniques); // prints total number of unique objects.
This previous question has various solutions for removing duplicate arrays from within an array. If you implement any of them and then use sizeof() on the returned array you will have your solution.
eg:
<?php
$yourarray = array();
$tmp = array ();
foreach ($yourarray as $row)
if (!in_array($row,$tmp)) array_push($tmp,$row);
echo sizeof($tmp);
?>