array has same key twice (php) - php

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++;
}

Related

PHP-SQL-Parser Why this lib parsed 'SIGNED' as a colref instead of reserved word?

Thanks for your help. So the question I'm asking is - Why https://github.com/greenlion/PHP-SQL-Parser parsed 'SIGNED' as a colref? -. Is that a bug or there is a reason for that?
I'm parsing an expression like "WHERE ( CAST(table1.BIGINTField AS SIGNED) - CAST(table2.BIGINTField AS SIGNED) ) <= 11". The reason for the cast is that mysql fires a warning when trying to do so without the cast. I was reading the docs and I found this way to fix that, casting the bigint fields to signed. I'm using the version 4.1.1 of PHP-SQL-PARSER, I got problems with the last version.
This is the result I have got so far:
["WHERE"]=> array(3) {
[0]=> array(4) {
["expr_type"]=> string(8) "function"
["base_expr"]=> string(4) "CAST"
["sub_tree"]=> array(1) {
[0]=> array(5) {
["expr_type"]=> string(10) "expression"
["base_expr"]=> string(51) "`table1`.`bigintfield` AS SIGNED"
["sub_tree"]=> array(3) {
[0]=> array(5) {
["expr_type"]=> string(6) "colref"
["base_expr"]=> string(41) "`table1`.`bigintfield`"
["no_quotes"]=> array(2) {
["delim"]=> string(1) "."
["parts"]=> array(2) {
[0]=> string(26) "table1"
[1]=> string(10) "bigintfield"
}
}
["sub_tree"]=> bool(false)
["position"]=> int(11)
}
[1]=> array(4) {
["expr_type"]=> string(8) "reserved"
["base_expr"]=> string(2) "AS"
["sub_tree"]=> bool(false)
["position"]=> int(53)
}
[2]=> array(5) {
["expr_type"]=> string(6) "colref"
["base_expr"]=> string(6) "SIGNED"
["no_quotes"]=> array(2) {
["delim"]=> bool(false)
["parts"]=> array(1) {
[0]=> string(6) "SIGNED"
}
}
["sub_tree"]=> bool(false)
["position"]=> int(56)
}
}
["alias"]=> bool(false)
["position"]=> int(11)
}
}
["position"]=> int(6)
} ...
Signed is a reserved word, I don't know why the sql parser say that it isn't.
Even though, Is there any different way to fix the problem facing arithmetical operations with bigints? I hope I'm not saying any foolishness haha.
Update: I almost forgot, I "fixed" it adding an if statement, performing the operation just when the value of the table1 is bigger than the value of the table2 but still.
Thanks for your help!
Sebastian C.

Merge two arrays when string is identical

I have two arrays that I want to merge. I created the first one through a for() loop so that it includes the last seven days (only three here to keep it short):
array(7) {
[0]=>
array(2) {
["created_at"]=>
string(10) "2017-08-15"
["errorCount"]=>
string(1) "0"
}
[1]=>
array(2) {
["created_at"]=>
string(10) "2017-08-16"
["errorCount"]=>
string(1) "0"
}
[2]=>
array(2) {
["created_at"]=>
string(10) "2017-08-17"
["errorCount"]=>
string(1) "0"
}
}
The other array includes data from a DB:
array(2) {
[0]=>
array(2) {
["created_at"]=>
string(10) "2017-08-15"
["errorCount"]=>
string(1) "4"
}
[1]=>
array(2) {
["created_at"]=>
string(10) "2017-08-16"
["errorCount"]=>
string(1) "12"
}
}
I want to merge these two together so that errorCount in the first array is overwritten whenever created_at is identical. I tried it with array_merge() directly but it only adds the rows of the second array to the end of the first one.
Any suggestions on how to solve this? Or is there a different way to approach it?
Filter result by following statement:
$finArray = array_values(array_combine(array_map(function ($value) {
return $value['created_at'];
}, $mergedArray), $mergedArray));

PHP Associative Arrays with Variable Key Names

I am trying to create an associative array with the keys being email addresses and the values being passwords. It is reading from an XML database to get the information. Here is my code:
$data = simplexml_load_file("Treasury.xml");
//Add in all passwords
for ($i = 0; $i < count($data->Member); $i++) {
$key = $data->Member[$i]->Email + '';
$USERS[$key] = $data->Member[$i]->Pin;
}
The problem comes in the for loop. It gets a correct count of the members (I had that print out) but the key is always being labeled as the number 0, resulting in only the last pin being stored in an array on length 1. Is there something syntactically that I am doing wrong?
Thanks in advance.
EDIT: I did a var_dump of the first user in the XML document. Here it is (Sorry for how long it is):
object(SimpleXMLElement)#4 (5) { ["Name"]=> string(19) "Mackenzie Daugherty" ["PC"]=> object(SimpleXMLElement)#2 (0) { } ["Email"]=> string(16) "dau53688#obu.edu" ["Pin"]=> string(4) "0000" ["Payments"]=> object(SimpleXMLElement)#3 (1) { ["Payment"]=> array(2) { [0]=> object(SimpleXMLElement)#5 (7) { ["Type"]=> string(4) "Dues" ["Description"]=> string(18) "Dues for Fall 2013" ["DateIssued"]=> string(7) "8/26/13" ["DateEnd"]=> string(6) "9/9/13" ["Owed"]=> string(2) "55" ["Paid"]=> string(2) "55" ["Plan"]=> object(SimpleXMLElement)#7 (5) { ["InPlan"]=> string(1) "0" ["PlanDescription"]=> object(SimpleXMLElement)#8 (0) { } ["Intervals"]=> string(1) "0" ["Completed"]=> string(1) "0" ["PerInterval"]=> string(1) "0" } } [1]=> object(SimpleXMLElement)#6 (7) { ["Type"]=> string(19) "Tiger Tunes Tickets" ["Description"]=> string(18) "Two Saturday Night" ["DateIssued"]=> string(7) "8/26/13" ["DateEnd"]=> string(7) "8/26/13" ["Owed"]=> string(2) "30" ["Paid"]=> string(2) "30" ["Plan"]=> object(SimpleXMLElement)#7 (5) { ["InPlan"]=> string(1) "0" ["PlanDescription"]=> object(SimpleXMLElement)#8 (0) { } ["Intervals"]=> string(1) "0" ["Completed"]=> string(1) "0" ["PerInterval"]=> string(1) "0" } } } } }
As clearly stated in the documentation that I'm sure you've been studying carefully, the PHP concatenation operator is ., not +.
Your code takes two operands, and attempts to perform arithmetic addition on them. Since they are not [meaningful] numbers, you end up with 0.
(I couldn't give a more detailed assessment without knowing the precise values of your operands, which you did not provide.)
Your code should read:
$key = $data->Member[$i]->Email . '';
// ^
// (is the concatenation necessary at all?
// isn't Email already a string?)
Make the same correction elsewhere.

how to have one array value, find a value in another array?

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);
?>

Comparing arrays in PHP - which order - string conversion errors

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;
}
}
}

Categories