This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to find the value from this array in php?
hello coders.I have an array like this
Array
(
[0] => Array
(
[0] => admin/login
[1] => LoginHandler
[2] => index
)
[1] => Array
(
[0] => post
[1] => PostHandler
[2] => index
)
[2] => Array
(
[0] => post/create
[1] => postHandler
[2] => create
)
[3] => Array
(
[0] => post/update
[1] => postHandler
[2] => update
)
[4] => Array
(
[0] => post/delete
[1] => postHandler
[2] => delete
)
)
I want only the values like admin/login, LoginHandler,index,
post,PostHandler,index,
post/create,PostHandler,create and so on.So how to do that?
You can loop through the array to perform an action on each element:
foreach ( $your_array as $element ) {
if ( is_array( $element ) ) {
foreach ( $element as $sub_element ) {
// You can store this value in a variable, or output it in your desired format.
echo $sub_element . "<br />";
}
}
else {
echo $element . "<br />";
}
}
Related
I have two arrays, one called fetched_services and one called fetched_companies, they look like this:
fetched_services
(
[1] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 153
)
[1] => Array
(
[id] => 137
)
[2] => Array
(
[id] => 138
)
)
)
)
fetched_companies
(
[0] => stdClass Object
(
[services] => Array
(
[0] => 25
[1] => 102
)
)
)
What i want to achieve is to end up with an array like fetched_services but only having child_services with id of fetched_companies["services"].
What i have tried is this:
$services = [];
$isFound = false;
foreach ($fetched_services as $fetched_service) {
foreach ($fetched_service["child_services"] as $fetched_child_service) {
$fetched_service["child_services"] = [];
foreach ($fetched_companies as $fetched_company) {
if( (in_array($fetched_child_service["id"],$fetched_company->services)) ) {
$fetched_service["child_services"][] = $fetched_child_service;
$isFound = true;
}
}
if($isFound) {
$services[] = $fetched_service;
$isFound = false;
}
}
}
This outputs this:
Array
(
[0] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 116
)
)
)
[1] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 117
)
)
)
)
As you can see the resulting array have two arrays containing same id but the child_services are different.
What i want to end up with is this:
Array
(
[0] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 116
)
)
(
[0] => Array
(
[id] => 117
)
)
)
)
What am i doing wrong here? Thanks!
Your question is a bit unclear (I'll edit/delete this if/when you clarify, see my comment above), but you could probably make use of array_filter by only keeping services that are present in said list (via in_array):
$result = array_filter(
$fetched_services['child_services'],
fn(array $child_service): bool => in_array($child_service['id'], $fetched_companies[0]->services, true)
);
Demo
I'd like to know how to avoid dupplicate of element while using a foreach in foreach with multidimensional array ?
The first level of my array can have several item (here's just 2, but maybe I can have 7 level). I've a lot trouble with this. Then this ID is going to be used as a parameter in a sql request, but this is another story.
This is my array :
Array
(
[0] => Array
(
[0] => Array
(
[id] => 10
)
[1] => Array
(
[id] => 11
)
[2] => Array
(
[id] => 12
)
)
[1] => Array
(
[0] => Array
(
[id] => 11
)
[1] => Array
(
[id] => 12
)
)
)
This is my foreach loop :
foreach($dataListe as $listeDiff){
foreach($listeDiff as $$item){
// echo $item[0].'<br />';
echo "<pre>".print_r($item, true)."</pre>";
}
}
Result :
Array
(
[id] => 10
)
Array
(
[id] => 11
)
Array
(
[id] => 12
)
Array
(
[id] => 11
)
Array
(
[id] => 12
)
Wanted :
Array
(
[id] => 10
)
Array
(
[id] => 11
)
Array
(
[id] => 12
)
use array_unique()
$result = [];
foreach($dataListe as $listeDiff){
$result[] = $listeDiff;
}
$result = array_unique($result);
Following should work
$dataListe = array(
array(array('id'=>10),array('id'=>20),array('id'=>20),array('id'=>10),array('id'=>20)),
array(array('id'=>10),array('id'=>30),array('id'=>20),array('id'=>10),array('id'=>20))
);
$result = array();
foreach($dataListe as $listeDiff){
foreach($listeDiff as $item){
if(!(in_array($item, $result))){
$result[] = $item;
echo "<pre>".print_r($item, true)."</pre>";
}
}
}
sample out put
Array
(
[0] => Array
(
[id] => 10
)
[1] => Array
(
[id] => 20
)
[2] => Array
(
[id] => 30
)
)
This question already has answers here:
How to "flatten" a multi-dimensional array to simple one in PHP? [duplicate]
(23 answers)
Closed 5 years ago.
Here is my output: im using more foreach loop with different condition and values put into same array name $returnVal[]=
EX:
foreach($val1 as $vals)
{
$returnVal[]=$vals;
}
foreach($val2 as $vals)
{
$returnVal[]=$vals;
}
foreach($val3 as $vals)
{
$returnVal[]=$vals;
}
foreach($val4 as $vals)
{
$returnVal[]=$vals;
} `
Im getting Output:
Array
(
[0] => Array
(
[0] => 39705--COLUMBUS--LOWNDES--MS
[1] => 39702--COLUMBUS--LOWNDES--MS
[2] => 39710--COLUMBUS--LOWNDES--MS
[3] => 39701--COLUMBUS--LOWNDES--MS
)
[1] => Array
(
[0] => Array
(
[0] => 39705--COLUMBUS--LOWNDES--MS
[1] => 39702--COLUMBUS--LOWNDES--MS
[2] => 39710--COLUMBUS--LOWNDES--MS
[3] => 39701--COLUMBUS--LOWNDES--MS
)
)
[2] => Array
(
[0] => Array
(
[0] => 39705--COLUMBUS--LOWNDES--MS
[1] => 39702--COLUMBUS--LOWNDES--MS
[2] => 39710--COLUMBUS--LOWNDES--MS
[3] => 39701--COLUMBUS--LOWNDES--MS
)
[1] => Array
(
[0] => Array
(
[0] => 39705--COLUMBUS--LOWNDES--MS
[1] => 39702--COLUMBUS--LOWNDES--MS
[2] => 39710--COLUMBUS--LOWNDES--MS
[3] => 39701--COLUMBUS--LOWNDES--MS
)
)
[2] => 57038--JEFFERSON--UNION--SD
)
[3] => 57049--NORTH SIOUX CITY--UNION--SD
)
My Question: remove duplicate values and merge into one level
Push element if it is not exists in the result array, this will avoid duplicate entries. Try the code below,
$returnVal = [];
foreach($val1 as $vals)
{
if (!in_array($vals, $returnVal)) {
$returnVal[]=$vals;
}
} ..etc and so on
This question already has answers here:
Group array data on one column and sum data from another column
(5 answers)
Closed 1 year ago.
I have read a lot of answers here on SO but havent been able to sort this out.
I have multidimensional array that looks like this:
Array
(
[0] => Array
(
[0] =>
[1] => 655
)
[1] => Array
(
[0] => IT-82
[1] => 14
)
[2] => Array
(
[0] => IT-21
[1] => 5
)
[3] => Array
(
[0] => IT-82
[1] => 7
)
[4] => Array
(
[0] =>
[1] => 3
)
[5] => Array
(
[0] => IT-21
[1] => 4
)
[6] => Array
(
[0] =>
[1] => 3
)
[7] => Array
(
[0] => IT-21
[1] => 3
)
[8] => Array
(
[0] => IT-72
[1] => 7
)
[9] => Array
(
[0] => IT-75
[1] => 22
)
[10] => Array
(
[0] => IT-75
[1] => 3
)
)
I would like to sum the values according to the keys ending with a single array like:
Array
(
=> 661
IT-82 => 21
IT-21 => 12
IT-82 => 12
IT-72 => 7
IT-75 => 25
)
Tried with
foreach ($array as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
but this only returned the sum of all the values.
Any help appreciated.
Try:
$sumArray = array();
foreach ($array as $k=>$subArray) { //loop through array
if(isset($sumArray[$subArray[0]]))
$sumArray[$subArray[0]] += $subArray[1]; // set 0th index as key and 1st as value and add value to current index
else
$sumArray[$subArray[0]] = $subArray[1];
}
print_r($sumArray);
Output:
Array
(
[] => 661
[IT-82] => 21
[IT-21] => 12
[IT-72] => 7
[IT-75] => 25
)
I suppose it should be:
foreach ($array as $subArray) {
$sumArray[$subArray[0]] += $subArray[1];
}
username(
[0] => 'andrew';
[1] => 'teddy';
[2] => 'bear';
)
email(
[0] => 'andrew#andrew.com';
[1] => 'teddy#teddy.com';
[2] => 'bear#bear.com';
)
I got 2 Array coming in from post. I am processing this with PHP.
I would like to combine the array so it looks like this.
So I can use a loop on the array to insert a query on a database.
[1] => Array (
[0] => 'andrew';
[1] => 'andrew#andrew.com';
)
[2] => Array (
[0] => 'teddy';
[1] => 'teddy#teddy.com';
)
[3] => Array (
[0] => 'bear';
[1] => 'bear#bear.com';
)
Take a look at array_combine()
If that doesn't solve your problem, you can always just go with a simple loop:
foreach($usernameArray as $k=>$val)
{
if(array_key_exists($k, $emailArray))
{
$combinedArray[$k] = array($val, $emailArray[$k]);
}
}
You need something like:
$res = array ();
for($i=0;$i<count($username);$i++) {
$res[$i][0] = $username[$i];
$res[$i][1] = $email[$i];
}