Merge two different array data in one array php - php

I want to merge two different array data in one array, but i'm confuse how to use array_push in this case.
this is example of my data input:
["author"]=>
array(2) {
[0]=>
string(1) "John"
[1]=>
string(1) "Doe"
}
["title"]=>
array(2) {
[0]=>
string(1) "book a"
[1]=>
string(1) "book b"
}
And the result in one array that i mean, like this:
["books"]=>
array(2) {
[0] =>
array(2) {
["author"]=>
string(1) "John"
["title"]=>
string(1) "book a"
}
[1] =>
array(2) {
["author"]=>
string(1) "Doe"
["title"]=>
string(1) "book b"
}
}
I already try using this way but it just return 1 from each array:
$data['books'] = [];
array_push($data['books'], [
'author' => $data['author'],
'title' => $data['title']
]);
if (isset($data['books'])) {
foreach ($data['books'] as $k => $v) {
$data['books'][$k]['author'] = (int)$v['author'];
$data['books'][$k]['title'] = (int)$v['title'];
}
}
result:
["books"]=>
array(1) {
[0]=>
array(2) {
["author"]=>
int(1)
["title"]=>
int(1)
}
}

You have to transpose your arrays with the keys in mind.
function transpose(array $arr){
$transArr = [];
foreach($arr as $keyRow => $subArr) {
foreach($subArr as $keyCol => $value) {
$transArr[$keyCol][$keyRow] = $value;
}
}
return $transArr;
}
This function can be used universally for similar problems. The function comes from this class.
How to use:
$input = [
"author"=> ["John","Doe"],
"title" => ["book a","book b"],
];
$books = transpose($input);
echo '<pre>';
var_export($books);
Or if you want to use the class:
$books = tableArray::create($input)
->transpose()
->fetchAll()
;
Output:
array (
0 =>
array (
'author' => 'John',
'title' => 'book a',
),
1 =>
array (
'author' => 'Doe',
'title' => 'book b',
),
)
If "author" and "title" exist as two arrays, $ input must first be created like this:
$input = ['author' => $arrayAuthor, 'title' => $arrayTitle];

Related

PHP how to remove duplicate or equal array value and append the data in an array

I have an array with 4 data each, what i want to accomplish is to remove
value of duplicate/equal tag_id and put/append the tag_images together
of the same tag_id. I also used array_unique but i don't know where to put it.
My array has a tag_id,tag_slug,tag_color, and tag_images(array). The last 2 array have the same data except with tag_images, I want to merge those data as one and put the tag_images in an array.
example:
array(4) {
[0]=>
array(4) {
["tag_id"]=> int(25)
["tag_slug"]=> string(5) "green"
["tag_color"]=> string(7) "#81d742"
["tag_images"]=> array(1) {
[0]=> string(75) "http://localhost/mysite/wp-content/uploads/2018/08/long-sleeve-tee.jpg"
}
}
[1]=>
array(4) {
["tag_id"]=> int(23)
["tag_slug"]=> string(3) "red"
["tag_color"]=> string(7) "#dd3333"
["tag_images"]=> array(1) {
[0]=> string(69) "http://localhost/mysite/wp-content/uploads/2018/08/vneck-tee.jpg"
}
}
[2]=>
array(4) {
["tag_id"]=> int(23)
["tag_slug"]=> string(3) "red"
["tag_color"]=> string(7) "#dd3333"
["tag_images"]=> array(1) {
[0]=> string(66) "http://localhost/mysite/wp-content/uploads/2018/08/beanie.jpg"
}
}
}
Output:
array(4) {
[0]=>
array(4) {
["tag_id"]=>
int(25)
["tag_slug"]=>
string(5) "green"
["tag_color"]=>
string(7) "#81d742"
["tag_images"]=>
array(1) {
[0]=>
string(75) "http://localhost/mysite/wp-content/uploads/2018/08/long-sleeve-tee.jpg"
}
}
[1]=>
array(4) {
["tag_id"]=>
int(23)
["tag_slug"]=>
string(3) "red"
["tag_color"]=>
string(7) "#dd3333"
["tag_images"]=>
array(1) {
[0]=>
string(66) "http://localhost/mysite/wp-content/uploads/2018/08/beanie.jpg"
[1]=>
string(69) "http://localhost/mysite/wp-content/uploads/2018/08/vneck-tee.jpg"
}
}
}
You can use array_merge_recursive()
Or use this function from this answer
function my_array_merge(&$array1, &$array2) {
$result = Array();
foreach($array1 as $key => &$value) {
$result[$key] = array_merge($value, $array2[$key]);
}
return $result;
}
$array = my_array_merge($array1, array2);
print_r($array);
I'd iterate the main array and build a second one.
$mainArray = array
(
array("tag_id" => 25,
"tag_slug" => "green",
"tag_color" => "#81d742",
"tag_images" => array("http://localhost/mysite/wp-content/uploads/2018/08/long-sleeve-tee.jpg")
),
array("tag_id" => 23,
"tag_slug" => "red",
"tag_color" => "#dd3333",
"tag_images" => array("http://localhost/mysite/wp-content/uploads/2018/08/vneck-tee.jpg")
),
array("tag_id" => 23,
"tag_slug" => "red",
"tag_color" => "#dd3333",
"tag_images" => array("http://localhost/mysite/wp-content/uploads/2018/08/beanie.jpg")
)
);
$freshArray = array();
foreach ($mainArray as $value)
{
$key = array_search($value['tag_id'], array_column($freshArray, 'tag_id'));
if (false === $key)
$freshArray[] = $value;
else
$freshArray[$key]['tag_images'][] = $value['tag_images'][0];
}
var_dump($freshArray);
Output :
array (size=2)
0 =>
array (size=4)
'tag_id' => int 25
'tag_slug' => string 'green' (length=5)
'tag_color' => string '#81d742' (length=7)
'tag_images' =>
array (size=1)
0 => string 'http://localhost/mysite/wp-content/uploads/2018/08/long-sleeve-tee.jpg' (length=70)
1 =>
array (size=4)
'tag_id' => int 23
'tag_slug' => string 'red' (length=3)
'tag_color' => string '#dd3333' (length=7)
'tag_images' =>
array (size=2)
0 => string 'http://localhost/mysite/wp-content/uploads/2018/08/vneck-tee.jpg' (length=64)
1 => string 'http://localhost/mysite/wp-content/uploads/2018/08/beanie.jpg' (length=61)

Grouping array items based on string value

I am trying to group an array by sport. It could be n number of sports. Finally, then create a new array with it. Is there an efficient way to this without going overkill?
$sports = [
['sport' => 'soccer', 'id' => 97487];
['sport' => 'soccer', 'id' => 244800];
['sport' => 'soccer', 'id' => 258740];
['sport' => 'basketball', 'id' => 147884];
['sport' => 'baseball', 'id' => 222240];
['sport' => 'baseball', 'id' => 222245];
];
Initial array:
array(6) {
[0]=>
array(2) {
["sport"]=>
string(6) "soccer"
["id"]=>
int(97487)
}
[1]=>
array(2) {
["sport"]=>
string(6) "soccer"
["id"]=>
int(244800)
}
[2]=>
array(2) {
["sport"]=>
string(6) "soccer"
["id"]=>
int(258740)
}
[3]=>
array(2) {
["sport"]=>
string(10) "basketball"
["id"]=>
int(147884)
}
[4]=>
array(2) {
["sport"]=>
string(8) "baseball"
["id"]=>
int(222240)
}
[5]=>
array(2) {
["sport"]=>
string(8) "baseball"
["id"]=>
int(222245)
}
}
Desired results:
array(3)
{
[0]=>
array(3) {
[0]=>
array(2) {
["sport"]=>
string(6) "soccer"
["id"]=>
int(97487)
}
[1]=>
array(2) {
["sport"]=>
string(6) "soccer"
["id"]=>
int(244800)
}
[2]=>
array(2) {
["sport"]=>
string(6) "soccer"
["id"]=>
int(258740)
}
}
[1]=>
array(1) {
[0]=>
array(2) {
["sport"]=>
string(10) "basketball"
["id"]=>
int(147884)
}
}
[2]=>
array(2) {
[0]=>
array(2) {
["sport"]=>
string(8) "baseball"
["id"]=>
int(222240)
}
[1]=>
array(2) {
["sport"]=>
string(8) "baseball"
["id"]=>
int(222245)
}
}
}
You can group the array like so:
$sports = [
['sport' => 'soccer', 'id' => 97487],
['sport' => 'soccer', 'id' => 244800],
['sport' => 'soccer', 'id' => 258740],
['sport' => 'basketball', 'id' => 147884],
['sport' => 'baseball', 'id' => 222240],
['sport' => 'baseball', 'id' => 222245]
];
// we will build an array here where the key is the sport
// name and the value is an array of objects pertaining
// to that sport i.e. 'basketball' => [bb1, bb2, ...]
$array = array();
// now consider every sport object in your original array
foreach($sports as $key => $item)
{
if (array_key_exists($item['sport'], $array)) {
// we encountered this same sport in the past so we
// know $array['sportName'] already exists and can
// push right to it
$array[$item['sport']][] = $item;
} else {
// we have never seen this sport before and now must
// insert the sport into $array['sportName'] = []
// and push this sport object to it
$array[$item['sport']] = [$item];
}
}
// since $array's keys are the names of the sports themselves, but
// you want the keys to be numeric, this will build a new array
// from just the values of $array which at this point contains
// grouped arrays of sport objects
$result = array_values($array);
// print the results for good measure :)
print_r($result);
This works by looping over your sports array and building a second array of [sportName => arrayOfThatSport]. Inside the for loop, we are checking to see if a given sport already exists in this array. If it does, great, add the sport to the corresponding array for that sport. Otherwise, create a new array for that sport available at $array['sportName'] = [sportObject]. If you consider several iterations of the loop you will see that we're either always adding to an existing $array['sportName'] array, or creating a new array at this position whenever we encounter a new sport we've never seen before. This gives us a final array similar to: [ "sport
: [...], "sport2": [...] ] but in your case you want a numeric array not an associative array, hence the call to array_values.
This is my suggested solution.
$groupedSports = [];
foreach ($sports as $item) {
if (empty($groupedSports[$item['sport']])) {
$groupedSports[$item['sport']] = [];
}
$groupedSports[$item['sport']][] = $item;
}
Short explanation:
line 1: we initialize an empty array
line 2: we start a loop on the original array
line 3-5: if it's the first occurrence for the current sport, we initialize an empty array for that sport (avoid warnings)
line 6: we assign current item to the appropriate array

add elements of associative array with same key

I have associative array like this
$min_stats=
array(2)
{
[0]=> array(3)
{
["minute"]=> object(MongoInt64)#13 (1)
{
["value"]=> string(8) "10"
}
["add"]=> object(MongoInt64)#14 (1)
{
["value"]=> string(1) "9"
}
["tag"]=> object(MongoInt64)#15 (1)
{
["value"]=> string(1) "4"
}
}
[1]=> array(3)
{
["minute"]=> object(MongoInt64)#13 (1)
{
["value"]=> string(8) "11"
}
["add"]=> object(MongoInt64)#14 (1)
{
["value"]=> string(1) "9"
}
["tag"]=> object(MongoInt64)#15 (1)
{
["value"]=> string(1) "5"
}
}
} Array
Now I want to combine all the key-value pair in the array to get one array.
The resulting array should be
array(11)
{
["minute"]=> int(21)
["add"]=> int(18)
["tag"]=> int(9)
} Array
so for what I have written is
foreach ($min_stats as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
but its not giving me correct results, I don't know what I'm doing wrong?
The $value is coming as 2 means its just counting the elements in the subarray.
Thanks in advance for the help.
You can do it like this:
$finalArr = array();
foreach($array as $k1 => $v1){
foreach($v1 as $k2=>$v2){
if(!isset($finalArr[$k2])){
$finalArr[$k2] = 0;
}
$finalArr[$k2] += $v2['value'];
}
}
print_r($finalArr);
Output:
Array
(
[minute] => 21
[add] => 18
[tag] => 9
)
Easy copy array for testing:
$array = array(
array(
'minute' => array(
'value' => '10'
),
'add' => array(
'value' => '9'
),
'tag' => array(
'value' => '4'
),
),
array(
'minute' => array(
'value' => '11'
),
'add' => array(
'value' => '9'
),
'tag' => array(
'value' => '5'
),
)
);

Associative array inside array in PHP

After fetching a result from the database, and preparing the array for the JSON enconde, I face a dilemma on how to reference the array 'data' inside the main array.
The "[0]" by far is an error of my logic...
while ($row =$result->fetch()) {
$name = $row['country'];
$id = $row['id']
$username = $row['username'];
$subtotal = $row['subtotal'];
if ( /* If $id exist in array row['series']}*/ ) {
///?????/////
/// Add array to 'DATA'
////?????////
$rows['series']["$id"]["$name"]['data'][]=array("$username", $subtotal);
}
else {
$rows['series'][]= array('id' => "$id", 'name' => "$name", 'data' => array("$username", $subtotal));
}
The vardump show as follow:
array(1) {
["series"]=>
array(2) {
[0]=>
array(3) {
["id"]=>
string(7) "hn"
["name"]=>
string(8) "HN"
["data"]=>
array(2) {
[0]=>
string(17) "GK_5"
[1]=>
string(5) "86040"
}
}
[1]=>
array(3) {
["id"]=>
string(7) "hn"
["name"]=>
string(8) "HN"
["data"]=>
array(2) {
[0]=>
string(17) "GK_8"
[1]=>
string(5) "20358"
}
}
}
}
But I want to add the last item with same id/name like this:
array(1) {
["series"]=>
array(2) {
[0]=>
array(3) {
["id"]=>
string(7) "hn"
["name"]=>
string(8) "HN"
["data"]=>
array(2) {
[0]=>
string(17) "GK_5"
[1]=>
string(5) "86040"
}
array(2) {
[0]=>
string(17) "GK_8"
[1]=>
string(5) "20358"
}
}
}
}
The most natural way would be to change your data structure a bit, so that the series array is indexed by the row ID instead of an arbitrary running index. The would allow you to rewrite your loop to (using mysqli syntax as an example):
$stmt->bind_result($id, $country, $username, $subtotal);
$series = array();
while ( $stmt->fetch() ) {
$series[$id]['country'] = $country;
$series[$id]['data'][] = array(
'username' => $username,
'subtotal' => $subtotal,
);
}
which will give you a data structure like:
$series = array(
'hn' => array(
'country' => 'HN',
'data' => array(
0 => array(
'username' => 'GK_5',
'subtotal' => 86040
),
1 => array(
'username' => 'GK_8',
'subtotal' => 20358
),
// ...
)
),
// ...
);
If you need the data in the exact format shown in your post, you can of course loop over this array with foreach to transform it, e.g.:
$rows = array();
foreach ( $series as $id => $record ) {
$record['id'] = $id;
$rows['series'][] = $record;
}

How can you make a multidimensional array unique? [duplicate]

This question already has answers here:
Filter/Remove rows where column value is found more than once in a multidimensional array
(4 answers)
Closed 9 months ago.
I've got a multidimensional array setup like the following:
array(
[0]=>
array(
["name"]=> "Foo"
["slug"]=> "Bar"
)
[1]=>
array(
["name"]=> "Foo"
["slug"]=> "Bar"
)
[2]=>
array(
["name"]=> "Test 1"
["slug"]=> "test-1"
)
[3]=>
array(
["name"]=> "Test 2"
["slug"]=> "test-2"
)
[4]=>
array(
["name"]=> "Test 3"
["slug"]=> "test-3"
)
)
What would be the best way to search through the area for duplicates values in "name" and remove them, so that each value in the multidimensional array is unique?
Thanks in advance!
You can use an associative array.
$temp_array = array();
foreach ($array as &$v) {
if (!isset($temp_array[$v['name']]))
$temp_array[$v['name']] =& $v;
}
This creates a temporary array, using $v['name'] as the key. If there is already an element with the same key, it is not added to the temporary array.
You can convert the associative array back to a sequential array, using
$array = array_values($temp_array);
Example code and output: http://codepad.org/zHfbtUrl
Since everyone given alternatives, here's a solution to the problem at-hand. Sometimes we have to work with the data we have, not re-arrange it the way we like it. That being said, this will remove all sub-sequent entries from the array that are duplicates.
$array = Array(
Array(
'name' => 'Test 3',
'slug' => 'test-3'
),
Array(
'name' => 'Foo',
'slug' => 'Bar'
),
Array(
'name' => 'Foo',
'slug' => 'Bar'
),
Array(
'name' => 'Test 1',
'slug' => 'test-1'
),
Array(
'name' => 'Test 2',
'slug' => 'test-2'
),
Array(
'name' => 'Test 3',
'slug' => 'test-3'
),
);
var_dump($array);
for ($e = 0; $e < count($array); $e++)
{
$duplicate = null;
for ($ee = $e+1; $ee < count($array); $ee++)
{
if (strcmp($array[$ee]['name'],$array[$e]['name']) === 0)
{
$duplicate = $ee;
break;
}
}
if (!is_null($duplicate))
array_splice($array,$duplicate,1);
}
var_dump($array);
Which will look like this:
array(6) {
[0]=>
array(2) {
["name"]=>
string(6) "Test 3"
["slug"]=>
string(6) "test-3"
}
[1]=>
array(2) {
["name"]=>
string(3) "Foo"
["slug"]=>
string(3) "Bar"
}
[2]=>
array(2) {
["name"]=>
string(3) "Foo"
["slug"]=>
string(3) "Bar"
}
[3]=>
array(2) {
["name"]=>
string(6) "Test 1"
["slug"]=>
string(6) "test-1"
}
[4]=>
array(2) {
["name"]=>
string(6) "Test 2"
["slug"]=>
string(6) "test-2"
}
[5]=>
array(2) {
["name"]=>
string(6) "Test 3"
["slug"]=>
string(6) "test-3"
}
}
array(4) {
[0]=>
array(2) {
["name"]=>
string(6) "Test 3"
["slug"]=>
string(6) "test-3"
}
[1]=>
array(2) {
["name"]=>
string(3) "Foo"
["slug"]=>
string(3) "Bar"
}
[2]=>
array(2) {
["name"]=>
string(6) "Test 1"
["slug"]=>
string(6) "test-1"
}
[3]=>
array(2) {
["name"]=>
string(6) "Test 2"
["slug"]=>
string(6) "test-2"
}
}
$array = array(
0 => array(
"name"=> "Foo",
"slug"=> "Bar"
),
1 => array(
"name"=> "Foo",
"slug"=> "Bar"
),
2 => array(
"name"=> "Test 1",
"slug"=> "test-1"
),
3 => array(
"name"=> "Test 2",
"slug"=> "test-2"
),
4 => array(
"name"=> "Test 3",
"slug"=> "test-3"
)
);
function array_unique_by_key (&$array, $key) {
$tmp = array();
$result = array();
foreach ($array as $value) {
if (!in_array($value[$key], $tmp)) {
array_push($tmp, $value[$key]);
array_push($result, $value);
}
}
return $array = $result;
}
array_unique_by_key($array, "name");
Just looking at your particular case, I would recommend using a hash table instead of a 2-dimensional array. If you use your "name" as the key in the hash, each entry would be unique.
Is there a specific need for the multidimensional array?
function multi_array_unique_by_value($array, $colon = '')
{
$ret_array = array();
$has_array = array();
foreach($array as $item)
{
$item_array = (array)$item;
if(!in_array($item_array[$colon], $has_array))
{
array_push($ret_array, $item);
array_push($has_array, $item_array[$colon]);
}
}
return $ret_array;
}
You can give your array here and give a colon name for making unique.
On this code, you have multidimensonal array, we foreach that array, which column index for us, we pushing that column values. And when same value, its not adding return array.
So this solution for array_unique for 1 coloumn.
Much simpler solution for your multidimensional array.
$unique = array_map('unserialize', array_unique(array_map('serialize', $array)));
echo "<pre>";
print_r($unique);

Categories