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);
Related
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;
}
I have a codeigniter shopping cart going and its "cart" array is the following:
Array (
[a87ff679a2f3e71d9181a67b7542122c] => Array
(
[rowid] => a87ff679a2f3e71d9181a67b7542122c
[id] => 4
[qty] => 1
[price] => 12.95
[name] => Maroon Choir Stole
[image] => 2353463627maroon_3.jpg
[custprod] => 0
[subtotal] => 12.95
)
[8f14e45fceea167a5a36dedd4bea2543] => Array
(
[rowid] => 8f14e45fceea167a5a36dedd4bea2543
[id] => 7
[qty] => 1
[price] => 12.95
[name] => Shiny Red Choir Stole
[image] => 2899638984red_vstole_1.jpg
[custprod] => 0
[subtotal] => 12.95
)
[eccbc87e4b5ce2fe28308fd9f2a7baf3] => Array
(
[rowid] => eccbc87e4b5ce2fe28308fd9f2a7baf3
[id] => 3
[qty] => 1
[price] => 14.95
[name] => Royal Blue Choir Stole
[image] => 1270984005royal_vstole.jpg
[custprod] => 1
[subtotal] => 14.95
)
)
My goal is to loop through this multidimensional array some how and if ANY product with the key value pair "custprod == 1" exists, then my checkout page will display one thing, and if no custom products are in the cart it displays another thing. Any help is appreciated. Thanks.
Rather than looping over it, you can check for the custprod key using array_key_exists. Or simply check to see if arr['custprod'] isset (both functions handle null differently).
$key = "custprod";
$arr = Array(
"custprod" => 1,
"someprop" => 23
);
if (array_key_exists($key, $arr) && 1 == $arr[$key]) {
// 'custprod' exists and is 1
}
function item_exists($cart, $custprod) {
foreach($cart as $item) {
if(array_key_exists("custprod", $item) && $item["custprod"] == $custprod) {
return true;
}
}
return false;
}
Now, you can use this function to check if product exist in stack:
if(item_exists($cart, 1)) {
// true
} else {
// false
}
You still need to loop the array to check it:
$cust_prod_found = false;
foreach($this->cart->contents() as $item){
if (array_key_exists("custprod", $item) && 1 == $item["custprod"]) {
$cust_prod_found = true; break;
}
}
if ($cust_prod_found) {
// display one thing
} else {
// display another thing
}
Have array
Array (
[3] =>
stdClass Object (
[term_id] => 3
[name] => Lietuviu
[slug] => lietuviu
[term_group] => 0
[term_taxonomy_id] => 3
[taxonomy] => kalba
[description] =>
[parent] => 0
[count] => 7
[object_id] => 135
)
)
want display: [name] => Lietuviu , try $var[3][name], but this not work
That is because that Array is actually an object. Put $var into this function:
function object_to_array($data)
{
if(is_array($data) || is_object($data))
{
$result = array();
foreach($data as $key => $value)
{
$result[$key] = object_to_array($value);
}
return $result;
}
return $data;
}
Like so:
$realArray = object_to_array($var);
The value under the index 3 is a stdClass object, you will have to use the arrow operator -> to get its values:
print $var[3]->name;
As your $var[3] element is an object, you have to access its properties like this:
echo $var[3]->name;
You could use object_to_array as defined or access the value in following way:
$var[3]->name
I have the below array coming though, ideally I am looking for a way of matching one value and printing out another value.
e.g.
if($randomvalue == $cards[Card][unit_id]) { echo $cards[SaleDetail][date_pid_signed]; }
I'm not sure exactly how to go about getting the above to work with the current array structure as below.
Any ideas how I can get around this?
Thanks
$cards = Array
(
[0] => Array
(
[Card] => Array
(
[id] => 210
[property_id] => 4
[unit_id] => 90
)
[SaleDetail] => Array
(
[property_agent] =>
[date_pid_signed] => 2012-06-15
[property_date_listed] =>
)
)
[1] => Array
(
[Card] => Array
(
[id] => 209
[property_id] => 4
[unit_id] => 103
)
[SaleDetail] => Array
(
[property_agent] =>
[date_pid_signed] => 2011-10-21
[property_date_listed] =>
)
)
)
foreach($cards as $card){
if($randomvalue == $card[Card][unit_id]) {
echo $card[SaleDetail][date_pid_signed];
}
}
Use $cards[0]['Card']['unit_id'] and $cards[0]['SaleDetail']['date_pid_signed']. Notice the indexes [0]. You can then use [1].
You might also want to check foreach or for loops!
if($randomvalue == $cards[0][Card][unit_id]) { echo $cards[0][SaleDetail][date_pid_signed]; }
also you can do
foreach($cards as $card)
{
if($randomvalue == $card[Card][unit_id])
{
echo $card[SaleDetail][date_pid_signed];
}
}
I have the following multidimensional array:
Array ( [0] => Array
( [id] => 1
[name] => Jonah
[points] => 27 )
[1] => Array
( [id] => 2
[name] => Mark
[points] => 34 )
)
I'm currently using a foreach loop to extract the values from the array:
foreach ($result as $key => $sub)
{
...
}
But I was wondering how do I see whether a value within the array already exists.
So for example if I wanted to add another set to the array, but the id is 1 (so the person is Jonah) and their score is 5, can I add the 5 to the already created array value in id 0 instead of creating a new array value?
So after the loop has finished the array will look like this:
Array ( [0] => Array
( [id] => 1
[name] => Jonah
[points] => 32 )
[1] => Array
( [id] => 2
[name] => Mark
[points] => 34 )
)
What about looping over your array, checking for each item if it's id is the one you're looking for ?
$found = false;
foreach ($your_array as $key => $data) {
if ($data['id'] == $the_id_youre_lloking_for) {
// The item has been found => add the new points to the existing ones
$data['points'] += $the_number_of_points;
$found = true;
break; // no need to loop anymore, as we have found the item => exit the loop
}
}
if ($found === false) {
// The id you were looking for has not been found,
// which means the corresponding item is not already present in your array
// => Add a new item to the array
}
you can first store the array with index equal to the id.
for example :
$arr =Array ( [0] => Array
( [id] => 1
[name] => Jonah
[points] => 27 )
[1] => Array
( [id] => 2
[name] => Mark
[points] => 34 )
);
$new = array();
foreach($arr as $value){
$new[$value['id']] = $value;
}
//So now you can check the array $new for if the key exists already
if(array_key_exists(1, $new)){
$new[1]['points'] = 32;
}
Even though the question is answered, I wanted to post my answer. Might come handy to future viewers. You can create new array from this array with filter then from there you can check if value exist on that array or not. You can follow below code. Sample
$arr = array(
0 =>array(
"id"=> 1,
"name"=> "Bangladesh",
"action"=> "27"
),
1 =>array(
"id"=> 2,
"name"=> "Entertainment",
"action"=> "34"
)
);
$new = array();
foreach($arr as $value){
$new[$value['id']] = $value;
}
if(array_key_exists(1, $new)){
echo $new[1]['id'];
}
else {
echo "aaa";
}
//print_r($new);