PHP Multi-dimensional array rearranging [closed] - php

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)

Related

Parse JSON with PHP - irregular format [closed]

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 7 years ago.
Improve this question
New to php and trying to figure out how to parse API data which is returned in what looks like a weird format. Here is a sample of the data:
[{"campaign_id":"9000","date":"2016-01-11","totalcount":"1838","page":"1","totalpages":1,"index":1,"count":1838},{"video2.stack.com":["84254","105","0","83.71"],...,"zierfischforum.at":["1","0","0","0.00"]}]
Here is an example of how you can parse your JSON as an array:
$json_string = '[{"campaign_id":"9000","date":"2016-01-11","totalcount":"1838","page":"1","totalpages":1,"index":1,"count":1838},{"video2.stack.com":["84254","105","0","83.71"],"zierfischforum.at":["1","0","0","0.00"]}]';
$json_array = json_decode($json_string, true); // true gets us an array
echo '<pre>';
print_r($json_array);
echo $json_array[1]['video2.stack.com'][0];
Provides the following results:
Array
(
[0] => Array
(
[campaign_id] => 9000
[date] => 2016-01-11
[totalcount] => 1838
[page] => 1
[totalpages] => 1
[index] => 1
[count] => 1838
)
[1] => Array
(
[video2.stack.com] => Array
(
[0] => 84254
[1] => 105
[2] => 0
[3] => 83.71
)
[zierfischforum.at] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 0.00
)
)
)
84254
First we output the entire array. Based on data there we are able to single out a value for one of the array parts for video2.stack.com. It is relatively easy to traverse and you should be able extract any information you need. You could even build a recursive search function for your JSON.
NOTE: I removed some of your data(the part ,...) as it made your JSON non-valid.

Can't echo out first item from array [closed]

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
Using PHP I am try to echo out the first item from an array...
Array
(
[docs] => Array
(
[0] => Array
(
[imgurl] => http://www.example.com/image1.jpg
)
[1] => Array
(
[imgurl] => http://www.example.com/image2.jpg
)
[2] => Array
(
[imgurl] => http://www.example.com/image3.jpg
)
[3] => Array
(
[imgurl] => http://www.example.com/image4.jpg
)
)
)
I am using the following PHP to attempt to display the first item...
echo $array['docs'][0]['imgurl'];
But it is giving me the error...
Warning: Illegal string offset 'docs'
Can anyone show me what I am doing wrong?
#fightstarr20, Your array format is not correct. I just correct it and then tried and it works fine.
<?php
$array = Array('docs' => Array
(
0 => Array
(
'imgurl' => 'http://www.example.com/image1.jpg'
),
1 => Array
(
'imgurl' => 'http://www.example.com/image2.jpg'
),
2 => Array
(
'imgurl' => 'http://www.example.com/image3.jpg'
),
3 => Array
(
'imgurl' => 'http://www.example.com/image4.jpg'
),
)
);
echo $array['docs'][0]['imgurl'];
?>
Output is:- http://www.example.com/image1.jpg
Since you have not given the full code I assume you have the following array:
$array = array('docs'=>array(
'0'=>array('imgurl'=>'http://www.example.com/image0.jpg'),
'1'=>array('imgurl'=>'http://www.example.com/image1.jpg'),
'2'=>array('imgurl'=>'http://www.example.com/image2.jpg'),
'3'=>array('imgurl'=>'http://www.example.com/image3.jpg'),
));
Then you can access the imgurl as:
echo $array['docs'][0]['imgurl'];
You can test the above code here

converting array of objects to simple array [closed]

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);

foreach loop begins at last element [closed]

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.

How to compare arrays and objects to find a match PHP [closed]

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);

Categories