PHP - Array result - php

I have a big problem with a array and I want to group by key (or I don't now :( )
I get the links from mySQL and I do the following:
$lien2 = $links2['text'];
$lien2 = stripslashes($lien2);
$lien2 = htmlspecialchars($lien2);
$lien2 = nl2br($lien2);
preg_match_all('#http://.*?\.?([a-zA-Z0-9\-]+)(\.[a-zA-Z0-9]+)/[a-zA-Z0-9\/\*\-\?\&\%\=\,\.\;\#\_]+#i', $lien2, $lien2_result, PREG_SET_ORDER);
This is the array $lien2_result:
Array
(
[0] => Array
(
[0] => links1
[1] => A
)
[1] => Array
(
[0] => links2
[1] => B
)
)
Array
(
[0] => Array
(
[0] => links3
[1] => C
)
)
Array
(
[0] => Array
(
[0] => links4
[1] => B
)
)
Array
(
[0] => Array
(
[0] => links5
[1] => D
)
)
Array
(
[0] => Array
(
[0] => links6
[1] => E
)
)
and I want to get the following result:
A
links1
B
links2
links4
C
links3
D
links5
E
links6

I would adjust the query personally but if you are stuck with this resultset you could rewrite it with
foreach($lien2_result as $lien2){
foreach($lien2 as $item){
$arr[$item[1]][] = $item[0];
}
}
where print_r($arr) would result something like:
$arr = Array('A' => Array('links1'), 'B' => Array('links2','links4')); //and so on..
And actual printing the way you asked:
foreach($arr as $name => $value){
echo($name.'<br />');
foreach($value as $item){
echo($item.'<br />');
}
}
EDIT
Here's an example
http://sandbox.onlinephpfunctions.com/code/c0da893797cb2049e8346168b280a9f5b1fa145b

Related

Remove duplicate values from a multi-dimensional array and sum them

suppose I have an array like this :
Array
(
[0] => Array
(
[0] => A
[1] => 20
)
[1] => Array
(
[0] => B
[1] => 10
)
[2] => Array
(
[0] => G
[1] => 5
)
[3] => Array
(
[0] => A
[1] => 15
)
)
I would like to remove duplicate values and sum just a row of array :
What I want :
Array
(
[0] => Array
(
[0] => A
[1] => 35 // <= sum : 20 + 15
)
[1] => Array
(
[0] => B
[1] => 10
)
[2] => Array
(
[0] => G
[1] => 5
)
)
I've read this question before.
updated
while($row = $stmt->fetch()){
$arr = array(
'GoodMainCode'=>persian_sql_to_php($row['GoodMainCode']), // <= like A in the example
'title'=> persian_sql_to_php($row['GoodName']),
'author'=>persian_sql_to_php($row['moalef']),
'publisher'=>persian_sql_to_php($row['Nasher']),
'translator'=>persian_sql_to_php($row['Motarjem']),
'price'=>persian_sql_to_php($row['SellPrice1']),
'isbn'=>persian_sql_to_php($row['ISBN']),
'amount'=>persian_sql_to_php($row['Amount']), // <= if GoodMainCode is same key, I must sum it.
'year_of_publish'=>persian_sql_to_php($row['SaleChap']),
'period_print'=>persian_sql_to_php($row['NobateChap'])
);
array_push($mjson,$arr);
}
//added
foreach($mjson as $v){
if(!isset($result[$v['GoodMainCode']]))
$result[$v['GoodMainCode']] = $v;
else
$result[$v['GoodMainCode']]['amount'] += $v['amount'];
}
This should work for you:
Just loop through your array and check if in your $result array is a key with the letter of the current inner Array from $arr. If not add it to the $result array and initialize the second key with the number.
If there is already a key with this letter you can simply add the numbers together in this array. At the end I simply use array_values() to reindex the entire array.
<?php
foreach($arr as $v) {
if(!isset($result[$v[0]]))
$result[$v[0]] = $v;
else
$result[$v[0]][1] += $v[1];
}
$result = array_values($result);
print_r($result);
?>
output:
Array
(
[0] => Array
(
[0] => A
[1] => 35
)
//...
[2] => Array
(
[0] => G
[1] => 5
)
)

How to merge arrays with sort in PHP

Does anyone have an idea how to merge and sort array in PHP?
I have array "AAA". When I print:
Array
(
[AAA] => Array
(
[0] => Array
(
[0] => a
[1] => c
)
[1] => Array
(
[0] => b
)
)
)
How can I merge this array to get result like:
Array
(
[AAA] => Array
(
[0] => a
[1] => b
[2] => c
)
)
Thanks.
Try this code, i have tried this code this is working as you expect.
Example:-
<?php
$array = array('AAA' => array(array('0' => "a",'1' => "b"), array('0' => "b")));
//Before Sorting
echo "Before sorting****".'<pre>';
print_r($array);
echo '</pre>';
$my_array = array_merge($array['AAA'][0], $array['AAA'][1]);
//After Sorting
echo "After sorting****".'<pre>';
print_r($my_array);
echo '</pre>';
?>
Check Out the Demo
Output:-
Before sorting****
Array
(
[AAA] => Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => b
)
)
)
After sorting****
Array
(
[0] => a
[1] => b
[2] => b
)
You could first merge using array_merge and then sort using sort
$temp = array_merge($arr['AAA'][0], $arr['AAA'][1]);
sort($temp);
Use this.
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
$l = iterator_to_array($it, false);
var_dump($l);

Recursive PHP Tree (permutations)

I'm looking to write a function which creates all permutation of a list of arrays (The list is dynamical). Now I found 2 articles, http://dannyherran.com/2011/06/finding-unique-array-combinations-with-php-permutations/ and Finding cartesian product with PHP associative arrays. But I don't want to store them as multiple arrays, I want to add each array to each possibility so I can use them later.
In fact I want to multiply each array with the other.
For example:
$array = array(
array(
1,
2
),
array(
'A',
'B',
'C'),
array(
'I',
'II')
);
In this form:
Array
(
[0] => Array
(
[0] => 1
[1] => Array
(
[0] => Array
(
[0] => A
[1] => Array
(
[0] => I
[1] => II
)
)
[1] => Array
(
[0] => B
[1] => Array
(
[0] => I
[1] => II
)
)
[2] => Array
(
[0] => C
[1] => Array
(
[0] => I
[1] => II
)
)
)
)
[1] => Array
(
[0] => 2
[1] => Array
(
[0] => Array
(
[0] => A
[1] => Array
(
[0] => I
[1] => II
)
)
[1] => Array
(
[0] => B
[1] => Array
(
[0] => I
[1] => II
)
)
[2] => Array
(
[0] => C
[1] => Array
(
[0] => I
[1] => II
)
)
)
)
)
I think this big example made my problem clear. For this type of array I created a function:
foreach ($array[1] as $value) {
$return1[] = array($value, $array[2]);
}
foreach ($array[0] as $value) {
$return[] = array($value, $return1);
}
print_r($return);
Now I want to create this function inside a recursive function (so it becomes dynamical) but I got stuck. I wanted to pass the amount of arrays to the function and then iterate.
function createTree($array, $loops=3){
$b = $array[$loops-2];
foreach ($b as $v) {
$return[] = array($v, createTree($return, $loops-1));
}
print_r($return);
}
Maybe there is a total other solution to multiply the arrays? But the function which isn't recursive is easy for me, but making it recursive...
Thanks for your help
function createTree($array){
switch(count($array)) {
case 0:
die('Illegal argument.');
case 1:
return $array[0];
default:
$lastArray = array_pop($array);
$subArray = createTree($array);
foreach ($lastArray as $item) {
$return[] = array($item, $subArray);
}
return $return;
}
}
var_dump(createTree(array_reverse($array)));

Create a multidimensional array in PHP by separating each line based off a specific value

This is the array I have:
Array
(
[0] => name:string:255
[1] => weight:integer
[2] => description:string:255
[3] => age:integer
)
I want it to look like this
NewArray
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
[1] => Array
(
[0] => weight
[1] => integer
[2] => Array
(
[0] => description
[1] => string
[2] => 255
[3] => Array
(
[0] => age
[1] => integer
)
Or better yet, I would prefer this result. Making two separate arrays based off the number of groups.
NewArray1
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
[1] => Array
(
[0] => description
[1] => string
[2] => 255
)
NewArray2
(
[0] => Array
(
[0] => weight
[1] => integer
[1] => Array
(
[0] => age
[1] => integer
)
I have tried exploding and implode and foreaching but I'm not quite getting the result I want.
Use array_reduce() to build a new array while iterating over the old, splitting it up by type:
$array = [
'name:string:255',
'weight:integer',
'description:string:255',
'age:integer',
];
$result = array_reduce($array, function(&$result, $item) {
$parts = explode(':', $item, 3);
$result[$parts[1]][] = $parts;
return $result;
}, []);
Try this:
<?php
$array = array("name:string:255", "weight:integer", "description:string:255", "age:integer");
function map_func($value) {
return explode(':', $value);
}
$newArray = array_map(map_func, $array);
echo "Output 1:\n";
print_r($newArray);
$sorted = array();
foreach($newArray as $el)
$sorted[$el[1]][] = $el;
echo "Output 2:\n";
print_r($sorted);
Output:
Output 1:
Array
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
)
[1] => Array
(
[0] => weight
[1] => integer
)
[2] => Array
(
[0] => description
[1] => string
[2] => 255
)
[3] => Array
(
[0] => age
[1] => integer
)
)
Output 2:
Array
(
[string] => Array
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
)
[1] => Array
(
[0] => description
[1] => string
[2] => 255
)
)
[integer] => Array
(
[0] => Array
(
[0] => weight
[1] => integer
)
[1] => Array
(
[0] => age
[1] => integer
)
)
)
I doubt the following is the best solution but it does return what you wanted.
$array = array(
"0" => "name:string:255",
"1" => "weight:integer",
"2" => "description:string:255",
"3" => "age:integer"
);
foreach ($array as $key => $val) {
foreach (explode(':', $val) as $part) {
$new_array[$key][] = $part;
}
}
print_r($new_array);
above returns the following.
Array
(
[0] => Array
(
[0] => name
[1] => string
[2] => 255
)
[1] => Array
(
[0] => weight
[1] => integer
)
[2] => Array
(
[0] => description
[1] => string
[2] => 255
)
[3] => Array
(
[0] => age
[1] => integer
)
)
So you have a 1-dimentional array of strings ($arr01) . strings are separated by :
and you need to have a 2-dimentional array ($arr02) where the second dimension is an array of strings composed by splitting the initial set of strings based on their separator character :
$arr01 = array("name:string:255", "weight:integer", "description:string:255", "age:integer");
$arr02 = array(array());
for($i=0; $i<sizeof($arr01); $i++) {
$arr02[$i] = explode(":",$arr01[$i]);
}
display the two arrays....
echo "array 01: <br>";
for($i=0; $i<sizeof($arr01); $i++) {
echo "[".$i."] ".$arr01[$i]."<br>";
}
echo "<br><br>";
echo "array 02: <br>";
for($i=0; $i<sizeof($arr01); $i++) {
echo "[".$i."] ==> <br>";
for($j=0; $j<sizeof($arr02[$i]); $j++) {
echo " [".$j."] ".$arr02[$i][$j]." <br>";
}
}
echo "<br><br>";

php unique multidimensional array by keeping entry with the highes value from one dimension?

I have another array unique question in the endless list of questions about them.
I can imagine this problem is quite simple to solve but I simply do not come on it.
Just because there are so many questions on this subject i wasn't able to find anything useful in this case.
the array:
Array
(
[0] => Array
(
[0] => blabla values
[1] => 91.181818181818
)
[1] => Array
(
[0] => blabla same values
[1] => 95.333333333333
)
[2] => Array
(
[0] => blabla other values
[1] => 86
)
[3] => Array
(
[0] => blabla other values
[1] => 92.5
)
[4] => Array
(
[0] => blabla same values
[1] => 88.5
)
)
I want to unique the array by the first array dimension and only keep the entry with the highest value from the second.
Maybe in MYSQL this would be no big deal but at the moment i am not able to implement something like that in php.
desired output array would be:
Array
(
[0] => Array
(
[0] => blabla values
[1] => 91.181818181818
)
[1] => Array
(
[0] => blabla same values
[1] => 95.333333333333
)
[2] => Array
(
[0] => blabla other values
[1] => 92.5
)
)
Has anyone a clever idea?
<?php
$list = array(
array('blabla values',91.181818181818),
array('blabla same values', 95.333333333333),
array('blabla other values', 86),
array('blabla other values', 92),
array('blabla same values', 88.5),
);
$result = array();
foreach ($list as $item)
{
$key = $item[0];
$value = $item[1];
if (!isset($result[$key]) || $result[$key][1] < $value)
{
$result[$key] = $item;
}
}
$result = array_values($result);
print_r($result);
the output:
Array
(
[0] => Array
(
[0] => blabla values
[1] => 91.1818181818
)
[1] => Array
(
[0] => blabla same values
[1] => 95.3333333333
)
[2] => Array
(
[0] => blabla other values
[1] => 92
)
)
usort($arr, function ($a, $b){
return $a[1] - $b[1];
});
$out = array();
foreach ($arr as $key => $value){
$out[$value[0]] = $value[1];
}
$arr = array_map(NULL, array_keys($out), $out);
Output:
Array
(
[0] => Array
(
[0] => blabla same values
[1] => 95.333333333333
)
[1] => Array
(
[0] => blabla other values
[1] => 86
)
[2] => Array
(
[0] => blabla values
[1] => 91.181818181818
)
)

Categories