if i have the following two arrays im PHP:
First array ($array1):
array(2) {
[0]=>
array(2) {
["movie_id"]=>
int(31)
["city"]=>
string(6) "london"
}
[1]=>
array(2) {
["movie_id"]=>
int(34)
["city"]=>
string(6) "berlin"
}
}
Second array ($array2):
array(2) {
[0]=>
array(2) {
["id"]=>
int(3)
["movie_id"]=>
int(31)
}
[1]=>
array(2) {
["id"]=>
int(4)
["movie_id"]=>
int(34)
}
}
How can i loop through the second array ($array2) use that movie_id to look through the first array, and insert the city, where the movie_id matches?
Im finding this very confusing?
hopefully i would end up with the 2nd array including an extra key with the city where the movie_id matches the first one?
Thank you!
Do you want something similar?
<?php
foreach($array2 as $key=>$value){
if($key=='movie_id' && $value==$array1[$key]){
$array2['city']= $array1['city'];
}
}
print_r($array2);
?>
Related
i got a multidimensional array that stores data for a specific products.But when the program removes it I receive new data with product that got quantity (ex. 1) and same product with quantity (-1).How can i remove from array both of them.
array(3) {
[0]=>
array(6) {
["I_ART"]=>
string(14) "Еспресо"
["N_ART"]=>
int(405)
["C_ART"]=>
string(5) "1.900"
["Q_ART"]=>
string(7) "-1.0000"
["MASA"]=>
int(5)
["POR"]=>
int(2)
}
[1]=>
array(6) {
["I_ART"]=>
string(8) "Фреш"
["N_ART"]=>
int(363)
["C_ART"]=>
string(5) "4.100"
["Q_ART"]=>
string(6) "2.0000"
["MASA"]=>
int(5)
["POR"]=>
int(1)
}
[2]=>
array(6) {
["I_ART"]=>
string(14) "Еспресо"
["N_ART"]=>
int(405)
["C_ART"]=>
string(5) "1.900"
["Q_ART"]=>
string(6) "1.0000"
["MASA"]=>
int(5)
["POR"]=>
int(1)
}
}
In this array for example i want to remove the array key 0 and 2.Also the compare must be by N_ART number and Quantity difference 1 negative and 1 positive.
The expected output is
array(1) {
[1]=>
array(6) {
["I_ART"]=>
string(8) "Фреш"
["N_ART"]=>
int(363)
["C_ART"]=>
string(5) "4.100"
["Q_ART"]=>
string(6) "2.0000"
["MASA"]=>
int(5)
["POR"]=>
int(1)
}
}
Here's my suggestion:
$newItems = [];
// Here you iterate over your source array and sum quantities under same `N_ART`
foreach ($sourceArray as $item) {
$id = $item['N_ART'];
if (!isset($newItems[$id])) {
$newItems[$id] = $item;
} else {
$newItems[$id]['Q_ART'] += $item['Q_ART'];
}
}
// Here you filter array so as to remove items where `Q_ART` is 0 or negative
$newItems = array_filter($newItems, function ($item) { return 0 < $item['Q_ART']; });
im reading in some data and want to sort it into an array.
To be more specific, im reading in an obj model and i want to sort all edges into an array. Each edge element then should exist of an array of all faces which use this edge. So after sorting, each edge should have 2 entries existing of the two faces which use them. But after the actual sorting most edges occur twice with the same key and a single face as an entry. Here in some more details what i mean :
First Face i read in is
array(4) {
[0]=>
string(1) "f"
[1]=>
string(1) "1"
[2]=>
string(1) "2"
[3]=>
string(2) "3
which sorts the edges 1_2 , 2_3 and 1_3 into this array (value is the face which uses this edge, face number 0 )
array(3) {
["1_2"]=>
array(1) {
[0]=>
int(0)
}
["2_3"]=>
array(1) {
[0]=>
int(0)
}
["1_3"]=>
array(1) {
[0]=>
int(0)
}
When the second face is read
array(4) {
[0]=>
string(1) "f"
[1]=>
string(1) "1"
[2]=>
string(1) "3"
[3]=>
string(2) "4"
the resulting edge array looks like this :
array(6) {
["1_2"]=>
array(1) {
[0]=>
int(0)
}
["2_3"]=>
array(1) {
[0]=>
int(0)
}
["1_3"]=>
array(1) {
[0]=>
int(0)
}
["1_3"]=>
array(1) {
[0]=>
int(1)
}
["3_4"]=>
array(1) {
[0]=>
int(1)
}
["1_4"]=>
array(1) {
[0]=>
int(1)
}
}
the edge 1_3 is used by both faces, but how the heck does the key "1_3" appear twice in a single array with different values?! i expected it to look like this
["1_3"]=>
array(2) {
[0]=>
int(0)
[0]=>
int(1)
}
i do not have a clue why or how this happens, because as far as i know keys should be unique. when giving out the element "1_3" i then only recieve the latest entry ( int(1) ).
full sorting code looks like this
if ($line_temp[0]=="f"){
for ($j=0;$j<3;$j++){
$line_temp2 = explode("//",$line_temp[$j+1]);
$data["f"][$face_number][$j] = $line_temp2[0];
}
if ($data["f"][$face_number][0]<$data["f"][$face_number][1]){
$data["e"][$data["f"][$face_number][0]."_".$data["f"][$face_number][1]][]=$face_number;
} else {
$data["e"][$data["f"][$face_number][1]."_".$data["f"][$face_number][0]][]=$face_number;
}
if ($data["f"][$face_number][1]<$data["f"][$face_number][2]){
$data["e"][$data["f"][$face_number][1]."_".$data["f"][$face_number][2]][]=$face_number;
} else {
$data["e"][$data["f"][$face_number][2]."_".$data["f"][$face_number][1]][]=$face_number;
}
if ($data["f"][$face_number][0]<$data["f"][$face_number][2]){
$data["e"][$data["f"][$face_number][0]."_".$data["f"][$face_number][2]][]=$face_number;
} else {
$data["e"][$data["f"][$face_number][2]."_".$data["f"][$face_number][0]][]=$face_number;
}
var_dump($data["e"]);
var_dump($data["e"]["1_3"]);
$face_number++;
}
I get this array, but I want to show me KEY and all subitem of that KEY bellow, I dont want every time to repear a key, I need that key shows only one and all others entity that has same key under this one.
array(2) {
[0]=>
array(1) {
["A"]=>
string(2) "Test1"
}
[1]=>
array(1) {
["A"]=>
string(2) "Test1"
}
}
I want something like this:
array(2) {
[0]=>
array(1) {
["A"]=>
string(2) "Test1",
string(2) "Test2"
}
}
use the following:
$return = array_merge_recursive($array, ...);
in your case:
$return = array_merge_recursive($array[0], $array[1]);
but I am guessing you'll want a more dynamic solution, I will draft that up now.
$return = call_user_func_array('array_merge_recursive', $array);
I var_dumped two arrays, the top on is the array coming in, $new_array, while the other one is a preexisting array $current_array:
// New Array
array(3) {
["Contributor"]=>
array(2) {
[0]=>
string(13) "edit_carousel"
[1]=>
string(13) "read_carousel"
}
["Editor"]=>
array(1) {
[0]=>
string(16) "delete_mini_feed"
}
["Author"]=>
array(3) {
[0]=>
string(11) "edit_blocks"
[1]=>
string(12) "edit_blockss"
[2]=>
string(12) "edit_blockss"
}
}
// Preexisting
array(3) {
["Contributor"]=>
array(2) {
[0]=>
string(13) "edit_carousel"
[1]=>
string(13) "read_carousel"
}
["Editor"]=>
array(4) {
[0]=>
string(16) "delete_mini_feed"
[1]=>
string(15) "edit_mini_feeds"
[2]=>
string(23) "edit_private_mini_feeds"
[3]=>
string(15) "edit_mini_feeds"
}
["Author"]=>
array(3) {
[0]=>
string(11) "edit_blocks"
[1]=>
string(12) "edit_blockss"
[2]=>
string(12) "edit_blockss"
}
}
I am trying to do something like: var_dump(array_intersect_assoc($current_array, $new_array)); to see whats different in the current array as opposed to the new array and generate an array of "differences" keeping the structure intact.
The issue is:
Is the order of arrays to be compared right? compare old to new and get an array of whats different in old. or should it be compare new to old?
Doing this, results in: Array to string conversion notice, but also prints out an array which is below.
I cant tell if these are: "these are whats not in old, but in new" or "the are whats not in new but in old" .... (It should say: these are not whats in old but in new).
array(3) {
["Contributor"]=>
array(2) {
[0]=>
string(13) "edit_carousel"
[1]=>
string(13) "read_carousel"
}
["Editor"]=>
array(4) {
[0]=>
string(16) "delete_mini_feed"
[1]=>
string(15) "edit_mini_feeds"
[2]=>
string(23) "edit_private_mini_feeds"
[3]=>
string(15) "edit_mini_feeds"
}
["Author"]=>
array(3) {
[0]=>
string(11) "edit_blocks"
[1]=>
string(12) "edit_blockss"
[2]=>
string(12) "edit_blockss"
}
}
The php function array_intersect_assoc() should return everything in the first array (aka $current_array) that exists in the second array (aka $new_array).
The issue that you are running into is that array_intersect_assoc() doesn't preform the key comparison recursively. It's only comparing the first level of keys (Contributor, Editor and Author).
Here's more information about the recursive issue. PHP Question: how to array_intersect_assoc() recursively
Your problem is that you are trying to perform array_intersect on a multidimensional array, but the function does string comparison of elements, resulting in array to string conversion error.
As you have the same keys in both arrays, the simplest solution is just to foreach through and to compare subsequent arrays(and you rather need array_diff if you want difference between the elements)
foreach($array_1 as $index => $sub_array) {
$sub_array_2 = $array_2[$index];
$diff = array_diff($sub_array, $sub_array_2);
// do something with diff
}
UPDATE
If You want to get everything that's not in array_1 but in array_2:
$result = [];
# lets find if there's any new elements
$keys_1 = array_keys($array_1);
$keys_2 = array_keys($array_2);
$diff = array_diff($keys_1, $keys_2);
if(!empty($diff)) {
foreach($diff as $key) {
if(isset($array_2[$key])) {
# it's in array_2
$result[$key] = $array_2[$key];
}
}
}
# now get difference between shared elements
$intersection = array_intersect($keys_1, $keys_2);
foreach($intersection as $key) {
$element_1 = $array_1[$key];
$element_2 = $array_2[$key];
$diff = array_diff($element_1, $element_2);
if(sizeof($diff)) {
if(!isset($result[$key]) ||!is_array($result[$key]) ) {
$result[$key] = array($diff);
} else {
$result[$key][] = $diff;
}
}
}
I have 2 Arrays in 2 variables and both of them containing exactly the same values in the fields (in the 1st array = "image_id"-field and in the 2nd array = "ID-field").
I need to compare the 2 fields and would like to output the imagepath string of the 1st array (if the "ID"-field of 1st array and the field of 2nd array are equal)
Something like this:
if "2146" from 1st multi-array is equal to "2146" from 2nd multi-array, then echo apple.jpg..
But how does that work? Its really freakin me out the last days.. thanks in advance for your replies.
$multidimensional_array1:
array(4) {
[0]=>
string(9) "apple.jpg"
["imagepath"]=>
string(9) "apple.jpg"
[1]=>
string(4) "2146"
["image_id"]=>
string(4) "2146"
}
array(4) {
[0]=>
string(10) "ananas.jpg"
["imagepath"]=>
string(10) "ananas.jpg"
[1]=>
string(4) "2037"
["image_id"]=>
string(4) "2037"
}
array(4) {
[0]=>
string(8) "nuts.jpg"
["imagepath"]=>
string(8) "nuts.jpg"
[1]=>
string(4) "2024"
["image_id"]=>
string(4) "2024"
}
$multidimensional_array2:
array(2) {
[0]=>
string(4) "2146"
["ID"]=>
string(4) "2146"
}
array(2) {
[0]=>
string(4) "2037"
["ID"]=>
string(4) "2037"
}
array(2) {
[0]=>
string(4) "2024"
["ID"]=>
string(4) "2024"
}
As long as the arrays have the same keys, length and order, you can iterate over one and and pick values from both.
$len = count($arr1);
for ($i = 0; $i < $len; $i++)
{
if ($arr1[$i]['image_id'] == $arr2[$i]['ID'])
{
// output $arr1[$i]['imagepath']
}
}
If the information is from two tables in the same database, you would be better off by just joining the tables together. If the arrays are not ordered the same or not of the same length (so that $i might reference different elements in both arrays), use one as a lookup table:
$lookup = array();
foreach ($arr2 as $element)
{
$lookup[$element['ID']] = $element;
}
foreach ($arr1 as $element)
{
if (isset($lookup[$element['image_id']]))
{
// output $element['imagepath']
}
}
foreach($multidimensional_array1 as $arr1){
foreach($multidimensional_array2 as $arr2){
if($arr2['id']==$arr1['image_id']){
echo $arr1['imagepath'];
}
}
}
Note: The larger the arrays become the longer this will take.