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 5 years ago.
Improve this question
I have a complicated JSON and I need create array from this JSON.
I already parsed JSON and create a variablies like this:
$name = $json[response][docs][$i][name][0];
$osm_id = $json[response][docs][$i][osm_id][0];
$place = $json[response][docs][$i][place][0];
$population= $json[response][docs][$i][population][0];
now I need a array, with this variablies, where the $i is changing, like this:
$array = [array_1(name,osm_id,place,population),array_2(name_2,osm_id_2)]
Can you help me with the cycle to fill this array?
If my understanding is correct,
$expected_arr = array();
foreach($json[response][docs] as $inc => $values){
$data = array();
foreach($values as $key => $val){
$data[$key] = $val[0];
}
$expected_arr[$inc] = $data;
}
So you would get something like
array(0 => array( 'name'=>'xxx', 'osm_id'=>'yy',..), 1=> array('name'=>'',.. ,),...)
Related
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 3 years ago.
Improve this question
I need an array like this in PHP?
array = [mani , mani, nithi, nithi, basto]
return in
array = [basto]
not for other elements
Does anyone know how to solve this problem?
<?php
$items = ['mani' , 'mani', 'nithi', 'nithi', 'basto'];
$counts = array_count_values($items);
// Remove elements that occur more than once.
$filtered = array_filter($items, function ($item) use ($counts) {
return $counts[$item] === 1;
});
var_export($filtered);
Output:
array (
4 => 'basto',
)
You may want to specify your question so users here can provide better assistance.
To your issue:
array_unique($array);
https://www.php.net/manual/en/function.array-unique.php
EDIT: you want to search all items by name, to do that you need this function: https://www.php.net/manual/en/function.array-search.php
with your example:
$array = ['mani' , 'mani', 'nithi', 'nithi', 'basto'];
$basto = array_search('basto', $array);
Best,
Sebo
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 4 years ago.
Improve this question
I wanted to output a 3 dimensional array that will come from the Database. This is my database looks like:
Basically, I wanted my first array will be the header_name and under the header_name the sub_header_name will come then underneath is the name
eg:
User Role Management => array(
'' => array (
'Create User Role'
)
),
Config Management => array(
'Organisation' => array('Create Country','Create
Organisation'),
'Site' => array('Create Site','Edit Site')
)
and here are my codes:
$getAllPermission = Permission::get();
$arrHeader = array();
$arrSubHeader = array();
$arrPermissions = array();
// $x = 0;
foreach($getAllPermission as $value){
$title = $value->header_name;
$sub_header_name = $value->sub_header_name;
$permission_name = $value->name;
if ($sub_header_name == ""){
$sub_header_name = 0;
}
array_push($arrPermissions,$permission_name);
$arrHeader[$title] = array($sub_header_name => array($arrPermissions));
//$x++;
}
and my output was like this:
You're pushing onto the same $arrPermissions array every time through the loop, that's why each of the roles gets a longer and longer copy of the permissions array.
You're overwriting $arrHeader[$title] each time through the loop instead of adding a new key to it.
Your desired output has a key of '' for the empty sub_header_name, so I don't see why you have the if that sets $sub_header_name = 0;.
It should simply be:
foreach ($getAllPermissions as $value) {
$arrHeader[$value->header_name][$value->sub_header_name][] = $value->name;
}
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 5 years ago.
Improve this question
How i can combine ad array of values into an array with double combination string without duplications ?
For example, if i have something like:
array('one','two','tre','four','five');
I want to obtain an array of combinations like this ('one / two') just one time and not also ('two / one').
In this way i wanto to get something similar to:
array('one/two', 'one/tre', 'one/four', 'one/five', 'two/tree','two/four' .......
Suggestions ?
You can do it with this code. It won't show two/one, three/two, etc (that's how I understood it):
<?php
$array = array('one','two','tre','four','five');
$newArray = [];
foreach ($array as $el) {
foreach ($array as $el2) {
if ($el === $el2) continue;
$newArray[] = $el."/".$el2;
}
array_shift($array); // remove the element we just went through
}
print_r($newArray);
Demo
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 6 years ago.
Improve this question
The problem is to get the following result set:
'org1', 'unit1', 'proj1'
'org1', 'unit1', 'proj2'
'org1', 'unit2', 'proj1'
'org1', 'unit2', 'proj2'
'org2', 'unit1', 'proj1'
'org2', 'unit1', 'proj2'
to the following in php:
[{"units": [{"name": "unit1", "projects": ["project1", "project2"]}, {"name": "unit2", "projects": ["project1", "project2"]}], "name": "org1"}, {"units": [{"name": "unit1", "projects": ["project1", "project2"]}], "name": "org2"}]
Any suggestions?
You can use something like
$final_array = array();
foreach($array as $row){
$final_array[$row[0]][$row[1]][] = $row[2];
}
Now this will "convert the SQL result array into a multidimentional array/tree" but not like what you want yet.
So we shall have to process the array again..
$final_array_2 = array(); // Lets go deep
foreach ($final_array as $name => $units) {
$quarterfinal_array = array(); // Not deep enough
$semi_final_array = array();
foreach ($units as $proj_name => $projects) {
$nano_final_array = array(); // Lets dig deeper ;)
$nano_final_array['name'] = $proj_name;
$nano_final_array['projects'] = $projects;
$semi_final_array[] = $nano_final_array;
}
$quarterfinal_array['units'] = $semi_final_array;
$quarterfinal_array['name'] = $name;
$final_array_2[] = $quarterfinal_array;
}
echo json_encode($final_array_2);
PS: Sorry my choice of variable names are not the most ideal, but they get the work done ;) This is a P.O.C, You can always improve on it.
DEMO
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 8 years ago.
Improve this question
I want do add values to array in my function to show movies:
$movies = array();
function find_movie($tag) {
// SELECT MOVIE BY %TAG%
$movies[] = $MOVIE;
return 'WATCH MOVIE';
}
Induction:
echo find_movie('cars'); // WATCH MOVIE
My problem is here -> $movies array is empty...
I want to have array with all movies what is showed.
How can I do that ?
You need to pass the $movies array as a reference to make this work out.
Like this..
function find_movie($tag,&$movies){
and call it like..
echo find_movie('cars',$movies);
The code..
<?php
$movies = array();
function find_movie($tag,&$movies) {
$movies[] = "<a href=$tag>WATCH MOVIE</a>";
}
find_movie('cars',$movies);
var_dump($movies);
OUTPUT:
array (size=1)
0 => string '<a href=cars>WATCH MOVIE</a>' (length=28)