Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've got a problem.
I have got this array:
Array (
[0] => Array (
[id] => 1
[opt] => reg_limit
[value] => 0
)
[1] => Array (
[id] => 3
[opt] => pages_offline
[value] => []
)
[2] => Array (
[id] => 4
[opt] => devolp
[value] => TRUE
)
)
I want to check if the [opt] devolp has the [value] TRUE in the third array. How can I do?
$aArray = Array (
[0] => Array (
[id] => 1
[opt] => reg_limit
[value] => 0
)
[1] => Array (
[id] => 3
[opt] => pages_offline
[value] => []
)
[2] => Array (
[id] => 4
[opt] => devolp
[value] => TRUE
)
)
foreach($aArray AS $aInnerArray){
if($aInnerArray['opt'] == 'devolp' && $aInnerArray['value'] == TRUE){
//YOUR USE CASE
}
}
if ($array[2]['value']) echo 'true';
Since the OP's question is quite vague on the details wether or not he knows which array key he needs to check,
Here is a simple example that you can use if you know the array key that you need to check in.
$bool = $yourMultiDeminsionalArray[2]['value'];
if ($bool) {
//Do some awesome PHP shizzle here
}
Provided you will work with a big array in the future and need some flexibility, this foreach will work for you:
foreach($array as $a) {
if(array_key_exists("opt", $a) && $a['opt'] == "devolp") {
if(array_key_exists("value", $a) && $a['value'] == TRUE) {
echo "Found it!";
//Do whatever you need to do here....
}
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have the following array that i want to re-arrange
Array
(
[0] => stdClass Object
(
[feeds_id] => 1338
[flag] => 0
)
[1] => stdClass Object
(
[feeds_id] => 1339
[flag] => 0
)
[2] => stdClass Object
(
[feeds_id] => 1339
[flag] => 1
)
)
I want to arrange it to look like this
[1338] => Array (
[0] => 0
)
[1339] => Array (
[0] => 0
[1] => 1
)
This code should work:
$newArray=array();
foreach($items as $item){
if(!is_array($newArray[$item->feeds_id])){
$newArray[$item->feeds_id]=array();
}
array_push($newArray[$item->feeds_id],$item->flag);
}
You should first create an empty array where the new data will be stored. Then, inside the foreach, you should use array_push, BUT if the sub-array in where you want to put the data is not an array, you should declare it first (that's why the "if" before the array_push)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have an array of objects, each object consists of an id and an organisation. It looks like this
Array (
[0] => stdClass Object (
[id] => 2 [organisation] => org1
)
[1] => stdClass Object (
[id] => 4 [organisation] => org2
)
[2] => stdClass Object (
[id] => 1 [organisation] => org3
)
)
I need to convert it into a simple associative array ([id]=>organisation,...) so the above example would look like this
Array (
[2] => org1
[4] => org2
[1] => org3
)
Greatful for any thoughts
Loop through it using a foreach statement and append it to another array.
$finished = [];
foreach($array as $arr) {
$finished[$arr->id] = $arr->organisation;
}
$result = array();
foreach($array as $arr) {
$result[$arr->id] = $arr->organisation;
}
echo "<pre>";print_r($result);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is there any simple solution to convert php mysql result change array structure with foreach or any method ?
Current result
Array
(
[1033] => stdClass Object
(
[id] => 1033
[plugin] => kooyke
[name] => kooyduration
[value] => 13
)
[1029] => stdClass Object
(
[id] => 1029
[plugin] => kooyke
[name] => kooyendpoint
[value] => http://localhost/public/data/
)
[1030] => stdClass Object
(
[id] => 1030
[plugin] => kooyke
[name] => kooylogin
[value] => ryrtrtr68fds876fdsf876fsd87fd
)
)
Expecting result
Array
(
[kooyduration] => 13
[kooyendpoint] => http://localhost/public/data/
[kooylogin] => ryrtrtr68fds876fdsf876fsd87fd
)
the code trying to convert
foreach($result as $value){
$expresult[] = $value['value'];
}
print_r($expresult);
Bellow code blocks may help you.
$i = 0;
$last_arr = array();
$new_arr = array();
foreach($my_array as $key=>$val)
{
$new_arr[$val->name] = $val->value;
}
array_push($last_arr,$new_arr);
You can not use like $value['value'], should be $value->value because of object. And push new array your name index. Try following:
$expresult = array();
foreach($result as $value)
{
$expresult[$value->name] = $value->value;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My first question is, how do I search values of my array inside the objects array [words] and return object if match is found?
How do I search for a better match? In the example below, second object is a better match with 2 words in common, rather than first with only 1 match.
Array
(
[0] => blue
[1] => green
[2] => love
[3] => sandro
)
stdClass Object
(
[1] => stdClass Object
(
[words] => Array
(
[0] => green
[1] => blue
)
[html] => html+img+link+code
)
[2] => stdClass Object
(
[words] => Array
(
[0] => love
[1] => sex
[2] => blue
)
[html] => html+img+link+code
)
)
Code I tried:
foreach ($ads_arr as $ad) {
print_r(array_intersect($ad->words,$words_arr));
}
You can use a forloop for your case, but you should consider defining real php class (not stdClass ) and implement some methods to help you.
foreach($main_std as $id => $sub_std){
$count_match[$id] = 0;
// now, check for each objects
// you can use an other loop with in_array, array_intersect
// or any other way
foreach($the_array as $word_search)
{
// for each word you're looking for, add +1
if (in_array($word_search, $sub_std->words))
$count_match[$id] ++;
}
}
// here, $count_match is an array you can sort by best match or whatever you want
Try out array_intersect() :
$output = array_intersect($array1, $array2);
print_r($output);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I've looked on this site and have found a few examples of merging arrays but to be honest, I'm not sure which one is right for me.
I am having a difficult time trying to merge these arrays. I've tried array_combine and array_merge but I am not having any luck.
What I need is a single array with the output like this:
Array
(
[0] => Array
(
[UnitNo] => 91
[Name] => Receiving
[ActivityNo] =>
[Active] => 2
[CallNo] =>
[CallStatusNo] =>
[Assigned] =>
[UnitId] => 2
)
[1] => Array
(
[UnitNo] => 83
[Name] => Shipping
[ActivityNo] =>
[Active] =>
[CallNo] =>
[CallStatusNo] =>
[Assigned] =>
[UnitId] => 1
)
)
These are the two arrays that I need to merge together based on the UnitId that should form a complete array like the one above. I just don't know how to do this. If someone could direct me a bit, I would be grateful.
// Array #1
Array
(
[0] => Array
(
[UnitId] => 2
[UnitNo] => 91
[Name] => Receiving
[Active] => 4
)
[1] => Array
(
[UnitId] => 1
[UnitNo] => 83
[Name] => Shipping
[Active] => 4
)
)
// Array #2
Array
(
[0] => Array
(
[UnitId] => 2
[ActivityNo] => 1
[CallNo] => 1
[CallStatusNo] => 1
[Assigned] => 1
)
[1] => Array
(
[UnitId] => 1
[ActivityNo] => 11
[CallNo] => 2
[CallStatusNo] => 1
[Assigned] => 1
)
)
This isn't a case where you can just use a php function and call it good - you will have to do some custom logic of your own.
This would work, but only if both $array1 and $array2 have the same items at each index.
$array1;
$array2;
$mergedArray;
for ($i=0; $i++; $i < count($array1) - 1) {
//first add all the values from array1
$mergedArray[$i] = $array1[$i];
foreach($array2[$i] as $array2Key=>$array2Value){
//now check for missing key/values that are in array2 but not in array1
if (array_key_exists($array2Key, $array1[$i]) == false) {
// the key does not exist in array1, so add it to mergedArray
$mergedArray[$i][$array2Key] = $array2Value;
}
}
}
It would be safer if your arrays were associative arrays, indexed by a unique value.