I have this array
Array
(
[0] => Array
(
[id] => 15
[parent] => #
[text] => Shb2-1
)
[1] => Array
(
[id] => 17
[parent] => 16
[text] => Shb2-3
)
[2] => Array
(
[id] => 18
[parent] => 17
[text] => Shb2-4
)
)
I would like to search if [parent] value exist in the same array as [id]
If it do not exist exists, then the [parent] value will be replaced to 0.
Example:
check if the value 16 exists in the whole array as [id] (in my example available [id] are 15, 17 and 18).
if it do not exist, 16 will be replaced by 0.
Then check for the next array key, and so until I get un output with final replaced values.
Thank you for your help.
If you wanted to change parent to zero if it is not equal to any id, then I think this simple code will do it:
foreach ($array as $key => $val) {
// Search if 'id' exists in the array
$a_key = array_search($val['parent'], array_column($array, 'id'));
// Check if 'id' is in the array, if not then replace 'parent' with 0
if ($a_key == false) $array[$key]['parent'] = '0';
}
Related
I'm trying very simply to use in_array() to check a key is in an array and then echo its value.
$array = Array
(
[cart_item] => Array
(
[0] => Array
(
[product_name] => White Sakura Necktie
[id] => 11
[product_auto_id] => 556729685
[quantity] => 2
[product_regular_price] => 95
[product_sale_price] => 95
[product_image] => 556729680Black_Sakura_Necktie.jpg
)
[1] => Array
(
[product_name] => hhhad ba bhdbh
[id] => 10
[product_auto_id] => 951790801
[quantity] => 2
[product_regular_price] => 20
[product_sale_price] =>
[product_image] => 951790801hhhad_ba_bhdbh_.jpg
)
)
)
And I have value 556729685 which I want to check that this value exists or not? So I am using in_array() function for this.
in_array(556729685, array_keys($array));
in_array(556729685, array_values($array));
in_array(556729685, $array);
All above three i have used but result always showing NULL means blank.
I am really frustrated to find the solution. I don't understand what's happening.
You should use array_column() which will return the values from a single column in the input array as an array.
$product_auto_ids = array_column($array['cart_item'], 'product_auto_id');
In this case, it would return the following:
Array
(
[0] => 556729685
[1] => 951790801
)
Then you can use in_array() like you currently are.
in_array(556729685, $product_auto_ids);
Using the array below, I want to get just some value of an element containing a certain value for option_name. For example, I want to get the element with option_name => custom_radio_gender then the result will show the value of option_value for that element (i.e. Radio 1).
Array
(
[0] => Array
(
[ID] => 15
[user_id] => 3
[option_name] => custom_monStart
[option_value] => a:3:{i:0;s:8:"05:30 am";i:1;s:8:"07:30 am";i:2;s:8:"09:30 am";}
[autoload] => yes
)
[1] => Array
(
[ID] => 13
[user_id] => 3
[option_name] => custom_radio_gender
[option_value] => Radio 1
[autoload] => yes
)
[2] => Array
(
[ID] => 14
[user_id] => 3
[option_name] => custom_time2
[option_value] =>
[autoload] => yes
)
)
It's a basic PHP question,which has nothing relationship with javascript.What you need is just a function like:
function getValue($array, $key){
foreach($array as $content){
if(array_key_exists("option_name", $content) && $content["option_name"] == $key){
return $content["option_value"];
}
}
}
Given this array:
Array
(
[0] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[1] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[2] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
[3] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
)
The ssm_featured_post_id value corresponds to the value of the array items in the second array.
I want to order the first array items in the same order as the items in the second array
Array
(
[1] => 63
[0] => 70
[3] => 1
[2] => 49
)
so the result after sorting would be
Array
(
[0] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[1] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[2] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
[3] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
)
The simpler way would be to use usort and write a function that uses the second table to compare two values from first table.
You may want to check out array_multisort, particularly the third example given. The idea is that you create arrays based on the "columns" of the multidimensional array, then sort them simultaneously, and put the result back in the original array.
I have a list of items stored in a DB and after requesting them, they are in an array ($data). Now there are about 200 items in the array and each of them itself is a key value array. Each element in the data array has a key called [Acr] and a name assigned to it.
Now the problem is that in this array
Array
(
[0] => Array
(
[ID] => 2
[Name] => Name Here
[Acr] => ARR
[Valid] => 1
[Orig] => 1
)
[1] => Array
(
[ID] => 2
[Name] => Name Here
[Acr] => ABC
[Valid] => 1
[Orig] => 1
)
[2] => Array
(
[ID] => 2
[Name] => Name Here
[Acr] => XYZ
[Valid] => 1
[Orig] => 1
)
...
There are items that have the same Acr but are sub elements of the first item with that Acr. So for example there are 10 more items in $data that have the Acr as ARR and I want to add those sub elements into the original (aka the first) array item with that Acr value under the key called sub. So after iterating it makes this.
Array
(
[0] => Array
(
[ID] => 2
[Name] => Name Here
[Acr] => ABC
[Valid] => 1
[Orig] => 1
)
.....
[14] => Array
(
[ID] => 2
[Name] => Name Here
[Acr] => ARR
[Valid] => 1
[Orig] => 1
[Sub] =>
[0] => Array
(
[ID] => 23
[Name] => Sub Name Here
[Acr] => ARR
[Valid] => 1
[Orig] => 0
)
[1] => Array
(
[ID] => 24
[Name] => Sub Name Here
[Acr] => ARR
[Valid] => 0
[Orig] => 1
)
)
...
Now im not sure how to do this. Also, its all sorted so when you see the first ARR all the sub ARR are right under them and there are only about 5 original categories that have sub elements so if theres a way that can do this by knowing which ones to append, that would be great.
I'm not sure if I explained the problem correctly, so if you have any questions please just ask me and I will reply within minutes.
Thanks
For the case, that alpha-numeric keys are accepted in manipulated array:
$new = array();
foreach ($array as $entry) {
if (!array_key_exists($entry['Acr'], $new)) {
$entry['Sub'] = array();
$new[$entry['Acr']] = $entry;
} else $new[$entry['Acr']]['Sub'][] = $entry;
}
An attempt at a small function that can do this, test it and let me know if it works
$newRecord = array();
foreach($records as $record){
# if the Acr already exists in a primary record,
# insert this record as a Sub-record.
if(array_key_exists($record['Acr'], $newRecord)){
$newRecord[$record['Acr']]['Sub'][] = $record;
# else insert it as a primary record
} else {
$newRecord[$record['Acr']] = $record;
}
}
I am working on a function that submits multiple records on various relationship types. The main issue I am running into is the format of the array. In order for my saveAll() to work on my multiple relationships setup, the array needs to be in this format as you can see the models are Keys (first array below).
My main question is: 1) Is it possible to strip the numerical indexes off the second layer of the second array below?
I am returning my input fields like so. You can see the prefixed counter (which I believe is what is creating the numeric index on that second level).
<?php echo $this->Form->input("$i.monthly_cost", array('label' => 'Monthly Cost')); ?>
I am using a for loop counter for the fields. So my question number to is: can this for value be changed to something that will work with Cake's saveAll()?
<?php for ($i = 1; $i <= 2; $i++) { ?>
Example where models are the keys (this is the format I need):
Array
(
[User] => Array
(
[username] => billy
)
[Profile] => Array
(
[sex] => Male
[occupation] => Programmer
)
The only output I can get on my multiple input array (below is the debug() dump)
My actual output is numerically indexed:
Array
(
[Plan] => Array
(
[1] => Array
(
[plan_detail_id] => 36
[monthly_cost] => 0
[dental_cost] => 0
[age_id] => 14
[applicant_id] => 1
[state_id] => 1
)
[2] => Array
(
[plan_detail_id] => 36
[monthly_cost] => 0
[dental_cost] => 0
[age_id] => 2
[applicant_id] => 4
[state_id] => 1
)
)
[1] => Array
(
[1] => Array
(
[Zip] => Array
(
[0] => 487
[1] => 486
[2] => 485
[3] => 484
[4] => 483
)
)
)
[2] => Array
(
[2] => Array
(
[Zip] => Array
(
[0] => 485
[1] => 484
[2] => 483
)
)
)
)
Did you already check out the Set Core Utility Library? This can help you out a lot with array management.