I have an array like this
Array
(
[1] => Array
(
[name] => 123
[id] => 105740727
[email] =>fghfhfh
[phrases_relevant] => 123
[searches_id] => 105740727
)
[2] => Array
(
[name] => porshe
[id] => 105713889
[email] => fghfghf
[phrases_relevant] => porshe
[searches_id] => 105713889
)
[3] => Array
(
[name] => porshe
[id] => 105713889
[email] => fghfghf
[phrases_relevant] => merce
[searches_id] => 105713889
)
I need group this group via value. Output array should looks like below. dimension second and third has same searches_id
[0] => Array
(
[email] => fghfghf
[projects]=>
[porshe] => [porshe, merce]
[1] => ...
edit;
my tried;
foreach ($results as $key => $result) {
$testArray[]['projects'][$result['name']][] = $result['phrases_relevant'];
but this insert one phrases;
You need to sort first by searches_id then apply loop,
function sortByOrder($a, $b)
{
return $a['searches_id'] - $b['searches_id'];
}
usort($myArray, 'sortByOrder');
foreach ($myArray as $key => $value) {
$result[$value['searches_id']]['email'] = $value['email'];
$result[$value['searches_id']]['projects'][] = $value['phrases_relevant'];
}
$result = array_values($result); // reset keys used for array generation
Working demo.
You can use foreach
$res = [];
foreach($arr as $key => $value){
array_key_exists($value['id'], $res) ?
($res[$value['id']]['phrases_relevant'] = $res[$value['id']]['phrases_relevant'].','.$value['phrases_relevant'])
:
($res[$value['id']] = ['email' => $value['email'],'phrases_relevant' => $value['phrases_relevant']]);
}
print_r(array_values($res))
Live Demo
Related
I have this array:
Array (
[0] => Array ( [cast_id] => 45 [character] => Evelyn Salt [credit_id] => 52fe4555c3a368484e054141 [gender] => 1 [id] => 11701 [name] => Angelina Jolie [order] => 0 [profile_path] => /6tocswK39SrSjZIRDaTpVyPxDz8.jpg )
[1] => Array ( [cast_id] => 3 [character] => Theodore Winter [credit_id] => 52fe4555c3a368484e05409f [gender] => 2 [id] => 23626 [name] => Liev Schreiber [order] => 1 [profile_path] => /qFn3npmqd1qaYOk6yohmi3FbPhc.jpg )
[2] => Array ( [cast_id] => 4 [character] => Darryl Peabody [credit_id] => 52fe4555c3a368484e0540a3 [gender] => 2 [id] => 5294 [name] => Chiwetel Ejiofor [order] => 2 [profile_path] => /fwaEgwYJdzGyBmGcHiH13vobQii.jpg )
[3] => Array ( [cast_id] => 10 [character] => Oleg Vasilyevich Orlov [credit_id] => 52fe4555c3a368484e0540bb [gender] => 2 [id] => 7107 [name] => Daniel Olbrychski [order] => 3 [profile_path] => /nfqJ8xiVNyBQQhnYRkwJzl3iS7s.jpg ))
I need to search for names where gender = 2.
Now I use this code:
function searcharray($value, $gender, $array) {
foreach ($array as $a => $val) {
if ($val[$gender] == $value) {
return $val['name'];
}
}
return null;
}
$iro = searcharray('2', gender, $array);
But it gives me just the first one: Liev Schreiber.
I need all of them separated with ",".
Try like this
function searcharray($value, $gender, $array) {
$arrNames = [];
foreach ($array as $a => $val) {
if ($val[$gender] == $value) {
$arrNames[]= $val['name'];
}
}
return $arrNames;
}
This is because you put a return statement in the middle of a cycle. This way, the first match will terminate the function execution.
For this to work you should first declare an empty results array and push in it every matching value. After every element of the array has been checked and the foreach finishes you can return the imploded array to get all the names separated by comma.
With all this in mind the correct function will be the following:
function searcharray($value, $gender, $array) {
$results = [];
foreach ($array as $a => $val) {
if ($val[$gender] == $value) {
array_push($results, $val['name']);
}
}
return implode(',', $results);
}
$iro = searcharray('2', gender, $array);
My form produces a 3 dimensional array. I would like to transform this array into a 2 dimensional one.
I tried this with no success:
$_rows = array();
foreach ($_contacts as $name => $_arr) {
foreach ($_arr as $key => $val) {
$_rows[] = array ($name => $val);
}
}
Data Source:
[_contacts] => Array
(
[name] => Array
(
[0] => foo
[1] => bar
)
[phone] => Array
(
[0] => 012345
[1] => 098765
)
[email] => Array
(
[0] => mail.com
[1] => yahoo.com
)
)
Desired output:
Array
(
[0] => Array
(
[name] => foo
[phone] => 012345
[email] => mail.com
)
[1] => Array
(
[name] => bar
[phone] => 098765
[email] => yahoo.com
)
)
Any thoughts were I went wrong?
$_rows = array();
foreach ($_contacts as $name => $_arr) {
foreach ($_arr as $key => $val) {
$_rows[$key][$name] = $val;
}
}
how can i count an element if it appears more than once in the same array?
I already tried with array_count_values, but it did not work, is it beacuse i got more than one key and value in my array?
This is my output from my array (restlist)
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla)
[1] => Array ( [restaurant_id] => 32144 [title] => test5)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
[3] => Array ( [restaurant_id] => 32144 [title] => test5)
[4] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
)
I want it to count how many times the same element appears in my array and then add the counted value to my newly created 'key' called hits in the same array.
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla [hits] => 1)
[1] => Array ( [restaurant_id] => 32144 [title] => test5 [hits] => 2)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 [hits] => 2)
)
This is how i tried to do what i wanted.
foreach ($cooltransactions as $key)
{
$tempArrayOverRestaurants[]= $key['restaurant_id'];
}
$wordsRestaruants = array_count_values($tempArrayOverRestaurants);
arsort($wordsRestaruants);
foreach ($wordsRestaruants as $key1 => $value1)
{
$temprestaurantswithhits[] = array(
'restaurant_id' => $key1,
'hits' => $value1);
}
foreach ($restlistas $key)
{
foreach ($temprestaurantswithhits as $key1)
{
if($key['restaurant_id'] === $key1['restaurant_id'])
{
$nyspisestedsliste[] = array(
'restaurant_id' => $key['restaurant_id'],
'title' => $key['title'],
'hits' => $key1['hits']);
}
}
}
I know this is probably a noob way to do what i want but i am still new at php..I hope you can help
Just try with associative array:
$input = array( /* your input data*/ );
$output = array();
foreach ( $input as $item ) {
$id = $item['restaurant_id'];
if ( !isset($output[$id]) ) {
$output[$id] = $item;
$output[$id]['hits'] = 1;
} else {
$output[$id]['hits']++;
}
}
And if you want to reset keys, do:
$outputWithoutKeys = array_values($output);
How to arrange this array by last inner index ( 0, 1, 2 ) and get the value of the last inner index as the value of each second index:
Array
(
[text] => Array
(
[grid] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
[image] => Array
(
[0] =>
[1] =>
[2] =>
)
[align] => Array
(
[0] => left
[1] => right
[2] => left
)
[title] => Array
(
[0] =>
[1] =>
[2] =>
)
[content] => Array
(
[0] =>
[1] =>
[2] =>
)
)
)
And have the results as below:
Array
(
[text] => Array
(
[0] => Array
(
[grid] => 3
[image] =>
[align] => left
[title] =>
[content] =>
)
[1] => Array
(
[grid] => 4
[image] =>
[align] => right
[title] =>
[content] =>
)
[2] => Array
(
[grid] => 5
[image] =>
[align] => left
[title] =>
[content] =>
)
)
)
This will do the work
function restructure($arr){
$newArr = array();
foreach($arr as $k => $v){
foreach($v as $k1 => $v1){
foreach($v1 as $k2 => $v2){
$newArr[$k][$k2][$k1] = $v2;
}
}
}
return $newArr;
}
As SiGanteng suggested, i dont see other ways than a for/foreach loop:
function buildArray($source, $key = false)
{
// Build the new array
$new_array = array();
// Define groups
$groups = $key === false ? array_keys($source) : array($key);
foreach($groups AS $group)
{
// Get the keys
$keys = array_keys($array[$group]);
// Count the values
$num_entries = count($array[$group][$keys[0]]);
for($i = 0; $i < $num_entries; $i++)
{
foreach($keys AS $key)
{
$new_array[$group][$i][$key] = $array[$group][$key][$i];
}
}
}
return $new_array;
}
This allow you to define the key you need to build; If not specified, the function build the array for every key.
This should work.
function formatit($arr) {
$new = array();
foreach($arr as $k=>$v) {
foreach($v as $k1=>$v1) {
$new[$k1][$k] = $v1;
}
}
return $new;
}
Tested. Call it as
$arr['text'] = formatit($arr['text']);
http://ideone.com/rPzuR
Hello I have POST array that looks like this,
Array (
[email_address] => Array (
[0] => simon#simonainley.info
[1] => simon2#simonainley.info
)
[firstname] => Array (
[0] => Simon
[1] => Simon2
)
[surname] => Array (
[0] => Ainley
[1] => Ainley2
)
[companies_company_id] => NULL,
[save_user] => Save User
)
I wanting to create an new array where I would get the first email_address, firstname, and surname into an array, deal with that data and then proceed to the next email-address, firstname and surname.
Is this possible? I have tried this code,
$newArray = array();
foreach($_POST as $key => $value) {
$newArray[] = $value;
}
however that code just produces this,
Array (
[0] => Array (
[0] => simon#simonainley.info
[1] => simon2#simonainley.info
)
[1] => Array (
[0] => Simon
[1] => Simon2
) [2] => Array ( [0] => Ainley [1] => Ainley2 ) [3] => [4] => Save User ) 1
What do I need to do?
$count = count($_POST['firstname']);
$result = array();
for ($i = 0; $i <= $count; $i ++) {
$result[] = array(
'email_address' => $_POST['email_address'][0],
'firstname' => $_POST['firstname'][0],
'lastname' => $_POST['lastname'][0]
);
}
or (if the numeric indices have any meaning)
$result = array();
foreach (array_keys($_POST['email_address']) as $index) {
$result[$index] = array(
'email_address' => $_POST['email_address'][$index],
'firstname' => $_POST['firstname'][$index],
'lastname' => $_POST['lastname'][$index]
);
}
You could try:
foreach($_POST as $key=>$value)
{
$count=0;
foreach($value as $val)
{
$newArray[$count++]=Array($key=>$val);
}
}
You only need to re-order the elements into the $_POST array:
$users = array();
foreach($_POST as $key => $values) {
if (is_array($values)) {
foreach($values as $index => $value) {
$users[$index][$key] = $value;
}
}
}
With the data you provided, it will give you this in the $users array:
[0] => Array
(
[email_address] => simon#simonainley.info
[firstname] => Simon
[surname] => Ainley
)
[1] => Array
(
[email_address] => simon2#simonainley.info
[firstname] => Simon2
[surname] => Ainley2
)
Elements in $_POST that are not an array are ignored and filtered out.