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);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a multi dimensional output array with following structure :
Array
(
[0] => Array
(
[id] => 5925
[fb_id] => 123
[description] =>
[video] =>
[thum] =>
)
[1] => Array
(
[id] => 7060
[fb_id] => 2344
[description] =>
[video] =>
[thum] =>
)
[2] => Array
(
[id] => 6579
[fb_id] => 123
[description] =>
[video] =>
)
)
Here, I want to limit the sub array count to 5 for all users with same 'fb_id'. Only 5 sub arrays for each fb_id should be in final output array. Any help would be appreciable.
The original array being used here is called $original and this code will reduce it down and produce a new array called $new. Feel free to change the variable names as appropriate.
$new = [];
$count = [];
foreach ($original as $element) {
$fbid = $element['fb_id'];
if (!isset($count[$fbid])) {
$count[$fbid] = 0;
}
if ($count[$fbid] < 5) {
$new[] = $element;
$count[$fbid]++;
}
}
// New array with limited sub-arrays
print_r($new);
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 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 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
My foreach loop begins at last element. I have an array $gridRows within object $grid that is currently set up like so:
$grid
gridRows
2
gridCells
1
2
3
3
gridCells
1
2
gridRow 2 has 3 gridCells in it and gridRow 3 has 2 gridCells.
However, when I iterate through it, the first foreach goes to gridRow 3 first. It still iterates over gridRow 2 but it does it second.
$gridRows = $grid->getGridRows();
foreach ($gridRows as $rowKey => $rowValue) {
$gridCells = $rowValue->getGridCells();
foreach ($gridCells as $cellKey => $cellValue) {
// do something
}
}
What gives? I tried using sort($grid, SORT_NUMERIC) but I get the same effect.
Result of print_r($gridRows):
Array (
[3] => GridRow Object (
[gridCells:GridRow:private] => Array (
[2] => GridCell Object ( ... )
[1] => GridCell Object ( ... )
)
)
[2] => GridRow Object (
[gridCells:GridRow:private] => Array (
[1] => GridCell Object ( ... )
[2] => GridCell Object ( ... )
[3] => GridCell Object ( ... )
)
)
)
It seems to being an object not an array. Use a numerically indexed array to maintain the order you want.
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);